您的位置:首页 > 移动开发 > Cocos引擎

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

2014-12-03 19:08 288 查看
local MainScene = class("MainScene", function()
return display.newScene("MainScene")
end)
local CURRENT_MODULE_NAME = ...
local Queue = import("./Queue", CURRENT_MODULE_NAME)
function MainScene:ctor()

local draw_node = CCDrawNode:create()
self:addChild(draw_node)

local sp = display.newSprite("res/res.png", 0, 0)
self:addChild(sp)
self.myQueue = Queue.new(100)

self.layer = display.newLayer()
self.layer:setTouchEnabled(true)
self.layer:setTouchMode(cc.TOUCH_MODE_ONE_BY_ONE)
self.layer:addNodeEventListener(cc.NODE_TOUCH_EVENT, function(event)
if event.name == "began" then
sp:setPosition(event.x,event.y)
return true
elseif event.name == "moved" then
draw_node:drawDot(ccp(event.x, event.y), 3, ccc4f(220, 10, 10, 222))
local point = ccp(event.x, event.y)

self.myQueue:enQueue(point)
print("ENQUEUE: "..point.x.." ,  "..point.y)
elseif event.name == "ended" then
end
end)
self:addChild(self.layer)

CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(function(dt)
if self.point == nil and self.myQueue:isEmpty() == false then
self.point = self.myQueue:deQueue()
print("DEQUEUE"..self.point.x.." ,  "..self.point.y)
end

if self.point ~= nil then
if math.floor(self.point.x) - math.floor(sp:getPositionX()) < 0 then
self.x= -1
elseif math.floor(self.point.x) - math.floor(sp:getPositionX()) > 0  then
self.x= 1
else
self.x = 0
end

if math.floor(self.point.y) - math.floor(sp:getPositionY()) < 0 then
self.y=-1
elseif math.floor(self.point.y) - math.floor(sp:getPositionY()) > 0  then
self.y= 1
else
self.y= 0
end

local spX = sp:getPositionX() + self.x
local spY = sp:getPositionY() + self.y

sp:setPosition(ccp(spX, spY))

if math.floor(spX) == math.floor(self.point.x) and math.floor(spY) == math.floor(self.point.y) then
self.point = nil
end
end
end, 0.01, false)
end

function MainScene:onEnter()
end

function MainScene:onExit()
end

return MainScene


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

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

效果图片
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: