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" then
20             sp:setPosition(event.x,event.y)
21             return true
22         elseif event.name == "moved" then
23             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" then
29         end
30     end)
31     self:addChild(self.layer)
32 
33     CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(function(dt)
34         if self.point == nil and self.myQueue:isEmpty() == false then
35             self.point = self.myQueue:deQueue()
36             print("DEQUEUE"..self.point.x.." ,  "..self.point.y)
37         end    
38 
39         if self.point ~= nil then
40             if math.floor(self.point.x) - math.floor(sp:getPositionX()) < 0 then
41                 self.x= -1
42             elseif math.floor(self.point.x) - math.floor(sp:getPositionX()) > 0  then
43                 self.x= 1
44             else
45                 self.x = 0
46             end
47 
48             if math.floor(self.point.y) - math.floor(sp:getPositionY()) < 0 then
49                 self.y=-1
50             elseif math.floor(self.point.y) - math.floor(sp:getPositionY()) > 0  then
51                 self.y= 1
52             else
53                 self.y= 0
54             end
55 
56             local spX = sp:getPositionX() + self.x
57             local spY = sp:getPositionY() + self.y
58 
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) then
62                 self.point = nil
63             end
64         end
65     end, 0.01, false)
66 end
67 
68 function MainScene:onEnter()
69 end
70 
71 function MainScene:onExit()
72 end
73 
74 return MainScene

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

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

效果图片

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。