cocos2d-x笔记(十一)Lua发展飞机战争-5- 让飞机动
2015-12-11 16:33
507 查看
然后在飞机上已被添加到游戏,下一步是让它动起来。主要是为了应对触摸事件。
在C++通过重写ccTouchBegan()、ccTouchMoved()、ccTouchEnded()三个函数来响应触摸事件。
在Lua器中就能够了。
1.先设置该图层能够触摸。然后注冊响应函数onTouch
2.onTouch函数有3个參数,第一个是事件的类型(began、moved、ended),后面两个參数就不用多说了。
3.分别实现3个触摸事件
4.在onTouchMoved函数中出现了一个没见过的函数PlaneLayer.containsTouchLocation(x,y)。这种方法用来推断想触摸点是否在飞机的位置上。
在C++通过重写ccTouchBegan()、ccTouchMoved()、ccTouchEnded()三个函数来响应触摸事件。
在Lua器中就能够了。
1.先设置该图层能够触摸。然后注冊响应函数onTouch
gameLayer:setTouchEnabled(true) gameLayer:registerScriptTouchHandler(onTouch)
2.onTouch函数有3个參数,第一个是事件的类型(began、moved、ended),后面两个參数就不用多说了。
function onTouch(eventType, x, y) if eventType == "began" then return onTouchBegan(x, y) elseif eventType == "moved" then return onTouchMoved(x, y) else return onTouchEnded(x, y) end end
3.分别实现3个触摸事件
local touchBeginPoint = nil function onTouchBegan(x, y) touchBeginPoint = {x = x, y = y} return true end function onTouchMoved(x, y) if PlaneLayer.alive() and PlaneLayer.containsTouchLocation(x,y) then --local offset = ccpSub(ccp(touchBeginPoint['x'],touchBeginPoint['y']),ccp(x,y)) --local toPoint = ccpAdd(ccp(PlaneLayer.getPlane():getPositionX(),PlaneLayer.getPlane():getPositionY()),offset) PlaneLayer.moveTo(x,y) end end function onTouchEnded(x, y) end
4.在onTouchMoved函数中出现了一个没见过的函数PlaneLayer.containsTouchLocation(x,y)。这种方法用来推断想触摸点是否在飞机的位置上。
function containsTouchLocation(x,y) local planeRect = plane:boundingBox() planeRect.origin.x = planeRect.origin.x - 15 planeRect.origin.y = planeRect.origin.y - 15 planeRect.size.width = planeRect.size.width + 30 planeRect.size.height = planeRect.size.height + 30 local b = planeRect:containsPoint(ccp(x,y)) return b end
相关文章推荐
- cocos2dx 学习笔记之摄像头与3D精灵的移动
- Cocos2d-x 脚本语言Lua基本语法
- cocos2d-js 学习笔记 --安装调试(1)
- cocos2dx - v2.3.3编辑器骨骼动画
- cocos2dx3.1.1+cocosstudio+lua问题总结
- Cocos2D-Android-1之源码详解:22.TileMapTest
- 【Cocos2d-x 3.x】 调度器Scheduler类源码分析
- 【cocos2d-x 3D游戏开发】2: 2D基础回顾---理解CCMenu类的实现, 实现点击放大的菜单按钮
- COCOS2D-3.9 ApplicationProtocol 分析
- 关于cocos2dx的背景音乐和音效(3)
- cocos2d-x mruby学习笔记 CCUI库的一个小BUG
- cocos2d js 给sprite增加按钮点击事件
- C#窗体应用程序实现cocos2dx json文件自动生成lua文件
- Cocos学习笔记——Menu
- IOS版Cocos2d-x2.2使用移动广告聚合平台集成插屏广告教程
- cocos2d-x v2 和 v3 对照手册
- AppUtils 常用函数总结(Cocos2d-x 3.9)
- cocos2dx - v2.3.3编辑器简单使用及不同分辨率适配
- Cocos2D-X shader(二) OpenGL渲染管线
- cocos2d-x3.0 相对布局(一)