1效果图:

这个是《Cocos2d-X by Example Beginner's Guide》上的第一个例子,我稍微重构了下代码。是一个简单的IPad上的双人游戏,把球射入对方的球门就得分了。当然我这是在Windows上的截图,用鼠标模拟手指的触摸。这游戏做的还不行,最大速度不能控制,很容易乱窜,但作为一个简单的入门游戏还不错。

2.cocos2d-x Touch 事件

我建了一个Player的类,就是上图中的两个白色可以触摸移动的物体。需要继承CCTargetedTouchDelegate,然后主要重写ccTouchBegan,ccTouchMoved,ccTouchEnded 3个方法。

#ifndef _PLAYER_H_
#define _PLAYER_H_ #include "cocos2d.h" USING_NS_CC; class Player : public CCSprite, public CCTargetedTouchDelegate//1. need inherit CCTargetedTouchDelegate
{ public:
Player(void);
virtual ~Player(void); /** 2.Overwrite some virtual function @{ */
virtual void onEnter();
virtual void onExit();
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);
virtual void touchDelegateRetain();
virtual void touchDelegateRelease();
/** @} */ }; #endif

在ccTouchMoved里主要限制了白色物体的范围。还有是设置了向量,等碰撞的时候用。

void Player::ccTouchMoved(CCTouch* touch, CCEvent* event)
{ CCAssert(m_state == kPlayerStateGrabbed, "Player - Unexpected state!");
CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); CCPoint tap = touch->getLocation();
CCPoint nextPosition = tap;
//keep player in court
nextPosition = ccp(max(nextPosition.x, radius()), max(nextPosition.y, radius()));
nextPosition = ccp(min(nextPosition.x, screenSize.width - radius()) ,
min(nextPosition.y, screenSize.height - radius())); //keep player in its area
if (getPositionY() < screenSize.height * 0.5f) {
nextPosition.y = min(nextPosition.y, screenSize.height / 2 - radius());
} else {
nextPosition.y = max(nextPosition.y, screenSize.height / 2 + radius());
} setNextPosition(nextPosition);
setVector(ccp(tap.x - getPositionX(), tap.y - getPositionY())); //setPosition(nextPosition);
}

3.谈谈球与桌面的摩擦

这游戏没有用到物理引擎,球与桌面的摩擦,就是简单地把球的向量乘以0.98,这样物体最终看起来就停止了。非常的简单。

4.球的碰撞

这块可能是最难的部分,首先还是看最简单的碰撞。

4.1球与边的碰撞

仔细看上面那动态图,球碰到左右边的时候,其实它在Y轴上的速度方向是不变的,只是在X轴上的速度方向变负了。

void Ball::collisionWithSides(){

	if(_nextPosition.x < radius()){
_nextPosition.x = radius();
setVector(ccp(getVector().x * -0.8, getVector().y)) ;//when collision with left and right side,the x direction will change
//and every hits will slow down the ball, so x * -0.8
//SimpleAudioEngine::sharedEngine()->playEffect("hit.wav");
} if (_nextPosition.x > _screenSize.width - radius()) {
_nextPosition.x = _screenSize.width - radius();
setVector(ccp(getVector().x * -0.8, getVector().y)) ;
//SimpleAudioEngine::sharedEngine()->playEffect("hit.wav");
}
}

代码中的x * -0.8就是这样的效果。方向变反,速度变慢。

球与上下边的碰撞也是同样的道理,在Y轴上的速度方向变反,变慢。X轴上不变,当然还要考虑这是在不进球的情况下。

void Ball::collisionWithTop(){

	if (_nextPosition.y > _screenSize.height - radius()) {
if (getPosition().x < _screenSize.width * 0.5f - GOAL_WIDTH * 0.5f ||
getPosition().x > _screenSize.width * 0.5f + GOAL_WIDTH * 0.5f) {
_nextPosition.y = _screenSize.height - radius();
setVector(ccp(getVector().x , getVector().y * -0.8)) ;//when collision with top or bottom side,the y direction will change
//and every hits will slow down the ball, so y * -0.8
//SimpleAudioEngine::sharedEngine()->playEffect("hit.wav");
}
}
}

4.2球与玩家控制的白色物体碰撞

什么时候两个球会碰撞?

这还是简单的就是两个球之间的距离小于两个球的半径相加。


碰撞后红球的方向是什么?

两个球碰撞后,红球移动的方向其实就是红球和白球圆点之间的连线。

碰撞后红球移动的速度?

首先为了尽可能地跟真实情况相同,碰撞后红球移动的速度应该跟原来红球的速度和白球移动的速度都有关。

讲了这么多,下面的代码应该可以理解了,数学万岁!

void Ball::collisionWithPlayer(Player* player){
float squared_radii = pow(player->radius() + radius(), 2);
CCPoint playerNextPosition = player->getNextPosition();
CCPoint playerVector = player->getVector();
CCPoint ballVector = getVector(); float diffx = _nextPosition.x - player->getPositionX();
float diffy = _nextPosition.y - player->getPositionY(); float distance1 = pow(diffx, 2) + pow(diffy, 2);// (x1 - x2)2 + (y1 - y2)2 if (distance1 <= squared_radii) {// a collision happen float mag_ball = pow(ballVector.x, 2) + pow(ballVector.y, 2);
float mag_player = pow (playerVector.x, 2) + pow (playerVector.y, 2); float force = sqrt(mag_ball + mag_player);
float angle = atan2(diffy, diffx); setVector(ccp(force * cos(angle), force * sin(angle))); //SimpleAudioEngine::sharedEngine()->playEffect("hit.wav");
}
}

5.项目下载

注:这项目是基于cocos2d-x 2.1.3。

下载地址:http://www.waitingfy.com/?attachment_id=611

文章地址:http://www.waitingfy.com/?p=608

cocos2d-x Touch 事件应用的一个例子的更多相关文章

  1. 移动web开发之touch事件

    前面的话 iOS版Safari为了向开发人员传达一些特殊信息,新增了一些专有事件.因为iOS设备既没有鼠标也没有键盘,所以在为移动Safari开发交互性网页时,常规的鼠标和键盘事件根本不够用.随着An ...

  2. Andriod 从源码的角度详解View,ViewGroup的Touch事件的分发机制

    转自:xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/21696315) 今天这篇文章主要分析的是Android的事件分发机制, ...

  3. angularjs的touch事件

    angularJs没有touch事件.这里提供一个touch指令. ngTouch.js "use strict"; angular.module("ngTouch&qu ...

  4. Android Touch事件原理加实例分析

    Android中有各种各样的事件,以响应用户的操作.这些事件可以分为按键事件和触屏事件.而Touch事件是触屏事件的基础事件,在进行Android开发时经常会用到,所以非常有必要深入理解它的原理机制. ...

  5. View,ViewGroup的Touch事件的分发机制

    原帖地址:http://blog.csdn.net/xiaanming/article/details/21696315 ViewGroup的事件分发机制 我们用手指去触摸Android手机屏幕,就会 ...

  6. Touch事件详解及区别,触屏滑动距离计算

    移动端有四个关于触摸的事件,分别是touchstart.touchmove.touchend.touchcancel(比较少用), 它们的触发顺序是touchstart-->touchmove- ...

  7. React-Native系列Android——Touch事件原理及状态效果

    Native原生相比于Hybrid或H5最大长处是具有流畅和复杂的交互效果,触摸事件便是当中重要一项,包括点击(Click).长按(LongClick).手势(gesture)等. 以最简单常见的点击 ...

  8. C# 关于委托和事件的妙文:通过一个例子详细介绍委托和事件的作用;Observer模式简介

    委托和事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去的人每次见 ...

  9. Android Touch事件传递机制 二:单纯的(伪生命周期)

    转载于:http://blog.csdn.net/yuanzeyao/article/details/38025165 在前一篇文章中,我主要讲解了Android源码中的Touch事件的传递过程,现在 ...

随机推荐

  1. 解决apache启动问题:httpd: Could not reliably determine the server's fully

    httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for Se ...

  2. php数组array,知道键名如何提取键值

    如果是知道键值,需要查找键名,直接使用array_search()即可.现在反过来,如何操作?   (1)array_walk_recursive()加自定义函数查找   $arr=array( 'n ...

  3. 如何彻底卸载sql server 2012

    好不容易装上了sql server 2012数据库,可是却不能连接本地的数据库,后来发现缺少一些服务,于是决定重新安装,但是卸载却很麻烦,如果卸载不干净的话,重新安装会出问题,所以下面就总结一些方法: ...

  4. 谈谈文件增量同步算法:RSYNC和CDC

    谈谈文件增量同步算法:RSYNC和CDC 分类: 数据同步 增量备份 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在研究文件的增量同步问题,着重研究了文件差异编码部分,因为这个其实是文件 ...

  5. 美国TJX公司 - MBA智库百科

    美国TJX公司 - MBA智库百科 TJX公司总部设在美国波士顿,在北美地区和许多欧洲国家开有连锁分店,仅在美国就有2500多家分店. TJX Companies, Inc. 是美国和全世界的服装和家 ...

  6. Mac 上开启一个简单的服务器

    终端输入命令: python -m SimpleHTTPServer 会开启一个端口为8000的本地服务器.

  7. SharePoint 2013 Designer 自己定义操作菜单

    众所周知,我们在SharePoint的二次开发中,常常会加入ECB菜单或者Ribbon菜单,通常我们会採取Feature的方式去加入一个Xml,或者採取JavaScript的方式.当然.除此之外,还能 ...

  8. codefirst初尝试

    Code First 约定 借助 CodeFirst,可通过使用 C# 或Visual Basic .NET 类来描述模型.模型的基本形状可通过约定来检测.约定是规则集,用于在使用 Code Firs ...

  9. 如何利用Github Pages展示自己写的项目

    接触github很久了,自己搭建过hexo博客,但是对于web项目托管github pages感觉很懵,所以在此总结分享给有需要的亲们. 教程开始: 1.创建一个新库 2.给库命名 3.创建新库后点击 ...

  10. CSS3 grayscale滤镜图片变黑白实例页面

    在网站加入友情链接的LOGO时,因为不同logo颜色的问题,和主题色调可能产生冲突, 我选择用CSS3滤镜让logo变黑白,hover时变回原本的彩色 CSS代码: .gray { -webkit-f ...