首页 > 代码库 > quick cocos2d-x 实现划线轨迹,精灵匀速跟随移动的效果

quick cocos2d-x 实现划线轨迹,精灵匀速跟随移动的效果

 1 local MainScene = class("MainScene", function() 2     return display.newScene("MainScene") 3 end) 4 local CURRENT_MODULE_NAME = ... 5 local Queue = import("./Queue", CURRENT_MODULE_NAME) 6 function MainScene:ctor() 7  8     local draw_node = CCDrawNode:create() 9     self:addChild(draw_node)10 11     local sp = display.newSprite("res/res.png", 0, 0)12     self:addChild(sp)13     self.myQueue = Queue.new(100)14 15     self.layer = display.newLayer()16     self.layer:setTouchEnabled(true)17     self.layer:setTouchMode(cc.TOUCH_MODE_ONE_BY_ONE)18     self.layer:addNodeEventListener(cc.NODE_TOUCH_EVENT, function(event)19         if event.name == "began" then20             sp:setPosition(event.x,event.y)21             return true22         elseif event.name == "moved" then23             draw_node:drawDot(ccp(event.x, event.y), 3, ccc4f(220, 10, 10, 222))24             local point = ccp(event.x, event.y)25 26             self.myQueue:enQueue(point)27             print("ENQUEUE: "..point.x.." ,  "..point.y)28         elseif event.name == "ended" then29         end30     end)31     self:addChild(self.layer)32 33     CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(function(dt)34         if self.point == nil and self.myQueue:isEmpty() == false then35             self.point = self.myQueue:deQueue()36             print("DEQUEUE"..self.point.x.." ,  "..self.point.y)37         end    38 39         if self.point ~= nil then40             if math.floor(self.point.x) - math.floor(sp:getPositionX()) < 0 then41                 self.x= -142             elseif math.floor(self.point.x) - math.floor(sp:getPositionX()) > 0  then43                 self.x= 144             else45                 self.x = 046             end47 48             if math.floor(self.point.y) - math.floor(sp:getPositionY()) < 0 then49                 self.y=-150             elseif math.floor(self.point.y) - math.floor(sp:getPositionY()) > 0  then51                 self.y= 152             else53                 self.y= 054             end55 56             local spX = sp:getPositionX() + self.x57             local spY = sp:getPositionY() + self.y58 59             sp:setPosition(ccp(spX, spY))60 61             if math.floor(spX) == math.floor(self.point.x) and math.floor(spY) == math.floor(self.point.y) then62                 self.point = nil63             end64         end65     end, 0.01, false)66 end67 68 function MainScene:onEnter()69 end70 71 function MainScene:onExit()72 end73 74 return MainScene

代码还有一个队列,直接拿我以前写的一个Queue。地址在:http://www.cnblogs.com/vokie/p/4110003.html

效果就是画出轨迹线路,精灵可以沿着轨迹线路匀速运动起来。代码基于quick 2.2.5

效果图片

quick cocos2d-x 实现划线轨迹,精灵匀速跟随移动的效果