Cocos2dx3.2 Crazy Tetris 游戏输入(键盘事件,重力事件,触摸屏事件)

Cocos2dx3.2 Crazy Tetris 游戏输入(键盘事件,重力事件,触摸屏事件),第1张

概述游戏基本的逻辑功能之前已经完成,之后的问题就是如何去控制游戏中的方块。在电脑上,很显然就是使用键盘最为直接,而在手机上,考虑上要让 *** 作方便,这里采用的是用重力感应控制左右移动,点击屏幕进行方块的旋转。 下面,就是加入这些事件的方法: 加入键盘事件,需要重写方法: virtual void onKeyPressed(cocos2d::EventKeyboard::KeyCode keyCode, c

游戏基本的逻辑功能之前已经完成,之后的问题就是如何去控制游戏中的方块。在电脑上,很显然就是使用键盘最为直接,而在手机上,考虑上要让 *** 作方便,这里采用的是用重力感应控制左右移动,点击屏幕进行方块的旋转。

下面,就是加入这些事件的方法:

加入键盘事件,需要重写方法:

virtual voID onKeypressed(cocos2d::EventKeyboard::KeyCode keyCode,cocos2d::Event* event);virtual voID onkeyreleased(cocos2d::EventKeyboard::KeyCode keyCode,cocos2d::Event* event);

加入重力感应事件,需要重写方法:

virtual voID onacceleration(cocos2d::acceleration* acc,cocos2d::Event* unused_event);

加入触摸屏事件,需要重写方法:

virtual bool ontouchBegan(cocos2d::touch *touch,cocos2d::Event *unused_event);virtual voID ontouchmoved(cocos2d::touch *touch,cocos2d::Event *unused_event);virtual voID ontouchended(cocos2d::touch *touch,cocos2d::Event *unused_event);virtual voID ontouchCancelled(cocos2d::touch *touch,cocos2d::Event *unused_event);

很显然,一个是按键点击方法,一个是按键释放方法。其中的KeyCode就是被按的那个键的编码可以在EventKeyboard::KeyCode类中找到键盘上每个键的定义。

这里的方法中,我是在键盘按下后,给方块一个左右(x)方向的速度,而在送开时,将速度置为0。如下:

if(keyCode == EventKeyboard::KeyCode::KEY_A || keyCode == EventKeyboard::KeyCode::KEY_left_ARROW){	//点击A或←按键事件	currentBlock->getPhysicsBody()->setVeLocity(Vec2(-60.0f,currentBlock->getPhysicsBody()->getVeLocity().y));}else if(keyCode == EventKeyboard::KeyCode::KEY_W || keyCode == EventKeyboard::KeyCode::KEY_UP_ARROW){	currentBlock->getPhysicsBody()->setAngularVeLocity(-5.0f);	isRotating = true;}

如上是按下按下向左按钮的事件。用setVeLocity设置速度,y方向用它自己的速度,x方向设为60

然后就是按向上的按钮将有旋转事件:

else if(keyCode == EventKeyboard::KeyCode::KEY_W || keyCode == EventKeyboard::KeyCode::KEY_UP_ARROW){	currentBlock->getPhysicsBody()->setAngularVeLocity(-5.0f);	isRotating = true;}

setAngularVelocoty设置其角速度。

紧接着,便是在按钮点击释放事件中将这些力恢复:

voID GameVIEw::onkeyreleased(EventKeyboard::KeyCode keyCode,Event* event){	currentBlock->getPhysicsBody()->setVeLocity(Vec2(0,(isAccelerate && currentBlock->getPhysicsBody()->getVeLocity().y < -50.0f)				?currentBlock->getPhysicsBody()->getVeLocity().y + 50.0f:currentBlock->getPhysicsBody()->getVeLocity().y));	currentBlock->getPhysicsBody()->setAngularVeLocity((isRotating && currentBlock->getPhysicsBody()->getAngularVeLocity() <= -5.0f)				?currentBlock->getPhysicsBody()->getAngularVeLocity() + 5.0f : currentBlock->getPhysicsBody()->getAngularVeLocity());		isAccelerate = false;	isRotating = false;}

里面那两行代码,分别是恢复左右移动和旋转的方法。

对于加入重力感应事件,主要是其参数accelerationacc,通过这个可以判断手机旋转的方向。他有四个变量xyztimestamp。其中xyz很容易想象,当我们手中平端着手机时,平面上是x轴和y轴,垂直方向是z轴。

这里,用z轴可以判断手机是朝上还是朝下(躺着玩手机)。用x轴判断重力的左右偏移。代码如下:

voID GameVIEw::onacceleration(acceleration* acc,Event* unused_event){	if(acc->x < -0.2f && fabs(currentBlock->getPhysicsBody()->getVeLocity().x) > -15.0f )	{		if(acc->z < 0)		{			currentBlock->getPhysicsBody()->setVeLocity(Vec2(-50.0f,currentBlock->getPhysicsBody()->getVeLocity().y));		}		else		{			currentBlock->getPhysicsBody()->setVeLocity(Vec2(50.0f,currentBlock->getPhysicsBody()->getVeLocity().y));		}	}	else if(acc->x > 0.2f && currentBlock->getPhysicsBody()->getVeLocity().x < 15.0f)	{		if(acc->z < 0)		{			currentBlock->getPhysicsBody()->setVeLocity(Vec2(50.0f,currentBlock->getPhysicsBody()->getVeLocity().y));		}		else		{			currentBlock->getPhysicsBody()->setVeLocity(Vec2(-50.0f,currentBlock->getPhysicsBody()->getVeLocity().y));		}	}}

最后说触摸屏事件,这里重写的一共有四个,ontouchBegan是触摸事件开始,ontouchmoved是触摸移动,ontouchended是触摸事件结束。ontouchChancelled,这个,我也不太清楚,网上查了一下,我的理解是,既然是取消,和结束差不多,但你主动取消了,不就是结束。所以,这个可能是被动取消了吧,比如来了个电话什么的。不去深究他了先。

其实可以想象,点击旋转就是一个按钮事件,所以只需要点击开始和点击结束就可以。

因此,有如下代码:

bool GameVIEw::ontouchBegan(cocos2d::touch *touch,cocos2d::Event *unused_event){	currentBlock->getPhysicsBody()->setAngularVeLocity(-5.0f);	isRotating = true;	return true;}

触摸开始时,设置其旋转速度,并且将标识置为true

然后,触摸结束时:

voID GameVIEw::ontouchended(cocos2d::touch *touch,cocos2d::Event *unused_event){	currentBlock->getPhysicsBody()->setAngularVeLocity((isRotating && currentBlock->getPhysicsBody()->getAngularVeLocity() <= -5.0f)					?currentBlock->getPhysicsBody()->getAngularVeLocity() + 5.0f : currentBlock->getPhysicsBody()->getAngularVeLocity());	isRotating = false;}

根据标识旋转速度回复。


关于制作游戏相关其他博客的目录,我放在 利用Cocos2dx3.2制作重力版俄罗斯方块(Crazy Tetris) 总结

以上是内存溢出为你收集整理的Cocos2dx3.2 Crazy Tetris 游戏输入(键盘事件,重力事件,触摸屏事件)全部内容,希望文章能够帮你解决Cocos2dx3.2 Crazy Tetris 游戏输入(键盘事件,重力事件,触摸屏事件)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1031127.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存