关于cocos2dx虚拟摇杆的lua实现
2016-07-25 18:14
706 查看
看到网上不少虚拟摇杆的代码,但是都是在c++里面实现,非常不方便,所以我自己把c++的代码移植到lua里面
local VitrualController = class("VitrualController", cc.Layer)
function VitrualController:ctor(app, name)
if self.onCreate then self:onCreate() end
end
function VitrualController:onTouchesEnded(touches, event )
self:_deactive()
end
function VitrualController:onTouchesBegan(touches, event )
self.start_pos = cc.p(touches[1]:getLocation())
self:_active(self.start_pos)
end
function VitrualController:onTouchesMove(touches, event )
local pos = cc.p(touches[1]:getLocation())
local distance = cc.pGetDistance(self.start_pos,pos)
local direction = cc.pNormalize(cc.pSub(pos,self.start_pos))
self:_update(direction,distance)
end
function VitrualController:onCreate()
self.joystick = cc.Sprite:create( "res/Joystick_03.png")
self.joystick:setScale(0.2,0.2)
self.joystick_bg = cc.Sprite:create( "res/Back_04.png")
self.joystick_bg:setScale(0.3,0.3)
self:addChild(self.joystick_bg)
self:addChild(self.joystick)
local listener = cc.EventListenerTouchAllAtOnce:create()
listener:registerScriptHandler(function(...) self:onTouchesBegan(...) end,cc.Handler.EVENT_TOUCHES_BEGAN )
listener:registerScriptHandler(function(...) self:onTouchesEnded(...) end,cc.Handler.EVENT_TOUCHES_ENDED )
listener:registerScriptHandler(function(...) self:onTouchesMove(...) end,cc.Handler.EVENT_TOUCHES_MOVED )
local eventDispatcher = self:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self)
end
function VitrualController:_active(pos)
self.joystick:setPosition(pos)
self.joystick_bg:setPosition(pos)
self.joystick:setVisible(true)
self.joystick_bg:setVisible(true)
end
function VitrualController:_deactive(pos)
self.joystick:setVisible(false)
self.joystick_bg:setVisible(false)
end
function VitrualController:_update(direction,distance)
print("seayoung udpate",direction.x,direction.y,distance)
local start = cc.p(self.joystick_bg:getPosition())
if distance < 32 then
self.joystick:setPosition(cc.pAdd(start , (cc.pMul(direction ,distance))))
else if distance > 96 then
self.joystick:setPosition(cc.pAdd(start , (cc.pMul(direction ,64))))
else
self.joystick:setPosition(cc.pAdd(start , (cc.pMul(direction ,32))))
end
end
end
return VitrualController
local VitrualController = class("VitrualController", cc.Layer)
function VitrualController:ctor(app, name)
if self.onCreate then self:onCreate() end
end
function VitrualController:onTouchesEnded(touches, event )
self:_deactive()
end
function VitrualController:onTouchesBegan(touches, event )
self.start_pos = cc.p(touches[1]:getLocation())
self:_active(self.start_pos)
end
function VitrualController:onTouchesMove(touches, event )
local pos = cc.p(touches[1]:getLocation())
local distance = cc.pGetDistance(self.start_pos,pos)
local direction = cc.pNormalize(cc.pSub(pos,self.start_pos))
self:_update(direction,distance)
end
function VitrualController:onCreate()
self.joystick = cc.Sprite:create( "res/Joystick_03.png")
self.joystick:setScale(0.2,0.2)
self.joystick_bg = cc.Sprite:create( "res/Back_04.png")
self.joystick_bg:setScale(0.3,0.3)
self:addChild(self.joystick_bg)
self:addChild(self.joystick)
local listener = cc.EventListenerTouchAllAtOnce:create()
listener:registerScriptHandler(function(...) self:onTouchesBegan(...) end,cc.Handler.EVENT_TOUCHES_BEGAN )
listener:registerScriptHandler(function(...) self:onTouchesEnded(...) end,cc.Handler.EVENT_TOUCHES_ENDED )
listener:registerScriptHandler(function(...) self:onTouchesMove(...) end,cc.Handler.EVENT_TOUCHES_MOVED )
local eventDispatcher = self:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self)
end
function VitrualController:_active(pos)
self.joystick:setPosition(pos)
self.joystick_bg:setPosition(pos)
self.joystick:setVisible(true)
self.joystick_bg:setVisible(true)
end
function VitrualController:_deactive(pos)
self.joystick:setVisible(false)
self.joystick_bg:setVisible(false)
end
function VitrualController:_update(direction,distance)
print("seayoung udpate",direction.x,direction.y,distance)
local start = cc.p(self.joystick_bg:getPosition())
if distance < 32 then
self.joystick:setPosition(cc.pAdd(start , (cc.pMul(direction ,distance))))
else if distance > 96 then
self.joystick:setPosition(cc.pAdd(start , (cc.pMul(direction ,64))))
else
self.joystick:setPosition(cc.pAdd(start , (cc.pMul(direction ,32))))
end
end
end
return VitrualController
相关文章推荐
- cocos2dx-lua制作游戏五维图
- Cocos2dx 截屏功能 3.2以上版本
- cocos2dx C++绑定lua
- Cocos2d-x优化中关于背景图片优化
- cocos2dx3.x物理引擎的碰撞检测
- cocos2d-android - 1.地图动态加载原理(世界地图的实现)
- Cocos Creator脚本开发(一)
- cocos2d-x中shader的使用
- cocos2d, Box2D
- cocos2d - JS 自定义事件(广播)
- cocos2d - JS Rect 相关
- Cocos Creator 视频学习笔记
- CocosCreator上手
- cocos原创
- cocosCreator的一些使用功能
- cocos2d - JS 点击事件
- cocos
- cocos2d - JS Sprite 镜像反转显示 ( Flipped )
- 基于Spine3.3的换装(纸娃娃,avatar)功能
- Cocos2d-x教程之环境搭建