quick-cocos2d-x(lua) 拖动精灵使其在屏幕移动的效果实现

(1)新建类TouchLayer

local TouchLayer=class("TouchLayer", function()
    return display.newNode()
end)


function TouchLayer:ctor(params)
-- self._func = params.func
player = params
    self:init()
end


function TouchLayer:init()
-- body
local colorLayer=display.newColorLayer(ccc4(23,222,21,0))
self:addChild(colorLayer, 0)


-- 触摸事件
colorLayer:setTouchSwallowEnabled(true)
colorLayer:setTouchEnabled(true)
colorLayer:setTouchMode(cc.TOUCH_MODE_ONE_BY_ONE)
--cc.TOUCH_MODE_ALL_AT_ONCE 是多点触摸
--注册触摸事件
colorLayer:addNodeEventListener(cc.NODE_TOUCH_EVENT, function(event)

   if event.name == "began" then
print("beganMask")
return true
   elseif event.name == "moved" then
local x1 = event.x
print(x1)
local y1 = event.y
local x2 = event.prevX
local y2 = event.prevY
print(x2)
local x = x1 - x2
local y = y1 - y2


if player:getPositionX() + x > 33 and player:getPositionX() + x < 287 and player:getPositionY() + y > 40 and player:getPositionY() + y < 440 then
player:setPosition(ccp(player:getPositionX() + x, player:getPositionY() + y))
end

print("movedMask")
elseif event.name == "ended" then
print("endedMask")
return true


end
return false
end)
end
return TouchLayer


(2)在另一个类中调用

local touchlayer=TouchLayer.new(player)-------》player移动
self:addChild(touchlayer)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值