cocos2d-x笔记(十一)Lua发展飞机战争-5- 让飞机动

简介:

然后在飞机上已被添加到游戏,下一步是让它动起来。主要是为了应对触摸事件。

在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






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5039490.html,如需转载请自行联系原作者

相关文章
|
4月前
|
Linux 数据安全/隐私保护 iOS开发
【教程】使用ipagurd打包与混淆Cocos2d-x的Lua脚本
本文将介绍如何使用ipagurd工具对Cocos2d-x中的Lua脚本进行打包与混淆,以及在iOS应用开发中的实际应用。我们将以Cocos2d-x-2.2.1 samples中的HelloLua为例,详细展示整个处理流程,并提供相应的代码案例演示。
|
Windows
在VsCode上调试Cocos2d-x lua项目
在VsCode上调试Cocos2d-x lua项目
751 0
|
C++
cocos2d-x lua-binding:将lua-binding结果引入到项目中使用
cocos2d-x lua-binding:将lua-binding结果引入到项目中使用
96 0
|
测试技术 Linux Android开发
如何使用ZEROBRANE STUDIO远程调试COCOS2D-X的LUA脚本(转)
http://www.cocos2d-x.org/docs/manual/framework/native/v2/lua/lua-remote-debug-via-zerobrane/zh ZeroBrane Studio做为一个轻量级的Lua IDE,因为它支持跨平台(支持Windows、Mac和Linux)和支持真机调试(Andorid、IPhone和IPad),所以经常被用来调试Lua。
1619 0
Cocos2d-x Lua中实例:帧动画使用
<h3><span style="font-weight: normal;"><span style="font-size:14px;">下面我们通过一个实例介绍一下帧动画的使用,这个实例如下图所示,点击Go按钮开始播放动画,这时候播放按钮标题变为Stop,点击Stop按钮可以停止播放动画。</span></span></h3> <p align="center"><span style="f
1508 0
Cocos2d-x Lua中帧动画
<p><span style="font-size:14px;"><a target="_blank" name="OLE_LINK53"></a>帧动画就是按一定时间间隔、一定的顺序、一帧一帧地显示帧图片。我们的美工要为精灵的运动绘制每一帧图片,因此帧动画会由很多帧组成,按照一定的顺序切换这些图片就可以了。</span></p> <p><span style="font-size:14px;
1617 0
Cocos2d-x Lua中实例:特效演示
<span style="font-size:14px;">下面我们通过一个实例介绍几个特效的使用,这个实例如下图所示,上图是一个操作菜单场景,选择菜单可以进入到下图动作场景,在下图动作场景中点击Go按钮可以执行我们选择的特性动作,点击Back按钮可以返回到菜单场景。<br></span><div style="text-align: center;"><img src="http://img
1679 0
Cocos2d-x Lua中网格动作
<span style="font-size:14px;">GridAction它有两个主要的子类Grid3DAction和TiledGrid3DAction,TiledGrid3DAction系列的子类中会有瓦片效果,如下图所示是Waves3D特效(Grid3DAction子类),如后图所示是WavesTiles3D特效(TiledGrid3DAction子类),比较这两个效果我们会看到瓦片
1179 0