博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cocos2d-x笔记(十一)Lua发展飞机战争-5- 让飞机动
阅读量:6939 次
发布时间:2019-06-27

本文共 1439 字,大约阅读时间需要 4 分钟。

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

在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)	endend

3.分别实现3个触摸事件

local touchBeginPoint = nilfunction onTouchBegan(x, y)	touchBeginPoint = {x = x, y = y}	return trueendfunction 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)	endendfunction 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 bend
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5039490.html,如需转载请自行联系原作者
你可能感兴趣的文章
SQL Mon 介绍
查看>>
yii2 对字段 自动加一 或 减一
查看>>
nginx负载均衡分配策略有哪些?
查看>>
localStorage
查看>>
简单的Asp.net mvc 里动态生成Linq的Ef查询扩展
查看>>
java.lang.reflect.InvocationTargetException
查看>>
完善(用户调研反馈+自评+典型用户与场景)
查看>>
elasticsearch接口开发(新)
查看>>
How to configure your MyInbox webpart automatically ?
查看>>
Linux : centOS 与 Ubuntu 安装 Nginx
查看>>
Django&Admin站点&调整站点信息
查看>>
POJ2125 Destroying The Graph
查看>>
详细的App推广前的准备工作
查看>>
15年1月的每天小程序
查看>>
多选插件multiselect.js
查看>>
Mysql基本用法-存储引擎-04
查看>>
使用GregorianCalendar模拟实现查看当前月的日历
查看>>
linux下的视频音频播放器终极解决方案
查看>>
让程序跨进网络时代——使用C语言获取百度源代码
查看>>
egret 精简游戏项目
查看>>