zoukankan      html  css  js  c++  java
  • 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

    效果图片

  • 相关阅读:
    文本域光标操作(选、添、删、取)的jQuery扩展
    jQuery插件,将内容插入到光标处
    onmouseout,mouseover经过子元素也触发的问题解决方案
    【M4】非必要不提供default 构造方法
    【M3】绝对不要以多态方式处理数组
    100亿个数字找出最大的10个
    【M2】最好使用C++转型操作符
    【M26】限制某个class所能产生的对象数量
    理解extern
    变量的属性
  • 原文地址:https://www.cnblogs.com/vokie/p/4141006.html
Copyright © 2011-2022 走看看