移动 摇杆的创建

function ControlLayer:createDir(  )
--加入摇杆背景
self.dirBg = display.newSprite("#joyStick.png")
self:addChild(self.dirBg)
self.dirBg:setPosition(self.dirBg:getContentSize().width*0.5 + 20, 
self.dirBg:getContentSize().height*0.5 + 20)
--摇杆球
self.dir = display.newSprite("#joyStickCenter.png")
self.dirBg:addChild(self.dir)
self.orginPoint = cc.p(self.dirBg:getContentSize().width*0.5, self.dirBg:getContentSize().height*0.5)
self.dir:setPosition(self.orginPoint)

self.dir:setTouchEnabled(true)
-- 添加touch事件
self.dir:addNodeEventListener(cc.NODE_TOUCH_EVENT, function ( event )
return self:onDirTouch(event)
end)
end
--搖桿的touch事件
function ControlLayer:onDirTouch( _event)
if _event.name == "began" then
self.startPoint = cc.p(_event.x, _event.y)
return true
elseif _event.name == "moved" then
local R = self.dirBg:getContentSize().width*0.5
local r = self.dir:getContentSize().width*0.5
local dt = R - r

local distance = math.sqrt(math.pow((_event.x - self.startPoint.x), 2) +
math.pow((_event.y - self.startPoint.y), 2))
--圈外
if distance > dt then
--1.拿到方向
local vec = cc.pSub(cc.p(_event.x, _event.y), self.orginPoint)
--2.方向单位化
local normalize = cc.pNormalize(vec)
--3 距离
local length = cc.pMul(normalize, dt)
--4 安装到原点
local pos = cc.pAdd(length, self.orginPoint)
self.dir:setPosition( pos)



else -- 圈里

self.dir:setPosition(self.dirBg:convertToNodeSpace(cc.p(_event.x, _event.y)))
end
elseif _event.name == "ended" then
self.dir:setPosition(self.orginPoint)
end
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值