主人公能够放子弹了,虽然子弹看起来很美,但是怎么样来打到妖怪?

在这一章我们介绍一下最简单的碰撞检测方法去实现它。

首先第一个,我们有必要保存每个妖怪和子弹的指针,来够追踪他们的位置。

在这个游戏中我们增加两个tag标志去辨别CCNode对象是子弹还是妖怪。tag == 1表示他是一个妖怪,tag == 2 表示他是一个子弹。CCNode有一个m_nTag属性,我们可以使用getTag()/setTag()来访问它,CCSprite是CCNode的子类,我们可以利用这个。

先在HellowWorld类中添加两个成员变量来存储,在HelloWorldScene.h里声明一下。

    // cpp with cocos2d-x
protected:
cocos2d::CCArray *_targets;
cocos2d::CCArray *_projectiles;

译注:原文中使用的CCMutableArray……但是在新版本中已经被直接废弃了,我们现在使用CCArray,他也是可变长数组,没有模板功能,使用时需要强制转换。

同时,在HelloWorldScene.h里声明HelloWorld的构造函数和析构函数,在HelloWorldScene.cpp里进行初始化:

HelloWorld::~HelloWorld()
{
//cocos2d定义的宏,它等价于以下代码
//if (_targets)
//{
// _targets->release();
// _targets = NULL;
//}
CC_SAFE_RELEASE_NULL(_targets);
CC_SAFE_RELEASE_NULL(_projectiles); // 记得把析构函数声明为虚函数
} HelloWorld::HelloWorld():_targets(NULL)
,_projectiles(NULL)
{
_targets = new CCArray();
_projectiles = new CCArray();
}

然后修改一下addTarget方法,在方法的最后,设置妖怪的tag,并且加入到数组里:

    // cpp with cocos2d-x
// Add to targets array
target->setTag();
_targets->addObject(target);

同理在 ccTouchesEnded 方法最后,设置子弹的tag并且加入到数组

    // cpp with cocos2d-x
// 设置tag并且加入到数组
projectile->setTag();
_projectiles->addObject(projectile);

然后还要修改一下spriteMoveFinished方法,因为在他们被从父节点中移除的同时,也需要将他们从数组中移除。

// cpp with cocos2d-x
void HelloWorld::spriteMoveFinished(CCNode* sender)
{
CCSprite *sprite = (CCSprite *)sender;
this->removeChild(sprite, true); // 从 tag 判断类型从对应的数组中移除
if (sprite->getTag() == )
{
_targets->removeObject(sprite);
}
else if (sprite->getTag() == )
{
_projectiles->removeObject(sprite);
}
}

下面的update方法将检查碰撞,并且移除碰撞的子弹和妖怪。

// cpp with cocos2d-x
void HelloWorld::update(float dt)
{
CCArray* projectilesToDelete = new CCArray();
CCObject* pobject; // cocos2d定义的宏,提供方便的只读遍历CCARRAY写法
CCARRAY_FOREACH(_projectiles, pobject)
{
CCSprite* projectile = (CCSprite*)pobject;
CCRect pRect = CCRect(projectile->getPosition().x - projectile->getContentSize().width/,
projectile->getPosition().y - projectile->getContentSize().height/,
projectile->getContentSize().width,
projectile->getContentSize().height); CCArray* targetsToDelete = new CCArray();
CCObject* tobject;
CCARRAY_FOREACH(_targets, tobject)
{
CCSprite* target = (CCSprite*)tobject;
CCRect tRect = CCRect(target->getPosition().x - target->getContentSize().width/,
target->getPosition().y - target->getContentSize().height/,
target->getContentSize().width,
target->getContentSize().height); // 碰撞测试
if( pRect.intersectsRect( tRect ) )
{
targetsToDelete->addObject( target );
}
} // 移除被击中的目标
CCARRAY_FOREACH(targetsToDelete, tobject)
{
CCSprite* target = (CCSprite*)tobject;
_targets->removeObject(target);
this->removeChild(target, true);
} // 记录击中目标的子弹
if(targetsToDelete->count() > )
{
targetsToDelete->addObject(projectile);
} /* 由于我们是用的 new CCArray() 而非 CCArray::create()
获得的数组对象,所以需要手动调用release */
targetsToDelete->release();
} // 移除击中目标的子弹
CCARRAY_FOREACH(projectilesToDelete, pobject)
{
CCSprite* projectile = (CCSprite*)pobject;
_projectiles->removeObject(projectile, true);
this->removeChild(projectile, true);
} projectilesToDelete->release();
}

OK~最后我们在HelloWorld::init方法最后添加一行代码,让游戏在每秒钟60次的刷新画面时调用HelloWorld::update方法

        this->scheduleUpdate();

大功告成!生成运行一下,看看是不是一打一个准!

Chapter5 – 碰撞检测的更多相关文章

  1. javascript动画系列第三篇——碰撞检测

    前面的话 前面分别介绍了拖拽模拟和磁性吸附,当可视区域内存在多个可拖拽元素,就出现碰撞检测的问题,这也是javascript动画的一个经典问题.本篇将详细介绍碰撞检测 原理介绍 碰撞检测的方法有很多, ...

  2. 使用 JavaScript 和 canvas 做精确的像素碰撞检测

    原文地址:Pixel accurate collision detection with Javascript and Canvas 译者:nzbin 我正在开发一个需要再次使用碰撞检测的游戏.我通常 ...

  3. JavaScript动画-碰撞检测

    ▓▓▓▓▓▓ 大致介绍 碰撞检测是指在页面中有多个元素时,拖拽一个元素会出现碰撞问题,碰撞检测是以模拟拖拽和磁性吸附中的范围限定为基础的 效果:碰撞检测 ▓▓▓▓▓▓ 碰撞检测 先来看看碰撞检测的原理 ...

  4. Unity3d刚体Rigidbody与碰撞检测Collider

    做了一个碰撞的小Demo,用一个球去撞击一堵墙,结果在球和墙都设置了刚体和碰撞体的情况下,球穿过了墙.移动球的位置,球有时能穿过墙,有时会被墙阻挡. 对于球穿过了墙,这个问题,在网上找了一下答案,基本 ...

  5. UIDynamic(重力行为+碰撞检测)

    一.重力行为 说明:给定重力方向.加速度,让物体朝着重力方向掉落 1.方法 (1)UIGravityBehavior的初始化 - (instancetype)initWithItems:(NSArra ...

  6. [Unity3D插件]2dToolKit系列三 碰撞检测功能的实现以及障碍物的随机摆放

    貌似有一段时间没更新2dtoolkit系列了,这段时间一直在忙着其他事情,今天开始继续这个插件系列的教程,网上搜索,貌似关于这个插件的教程无非还是跟官方的教程很类似,有的甚至都没有自己照着亲手实践一遍 ...

  7. javascript中矩形的碰撞检测---- 计算碰撞部分的面积

    今天在做一个拖拽改变元素排序的东西的时候,在做被拖动元素同时碰撞到两个元素时,究竟应该与哪个元素交换位置的问题上,纠结到崩溃,实在是想不到别的办法去做了,只能去想办法计算碰撞的面积. 这应该不是最合适 ...

  8. 写一些封装part1 (事件绑定移除,圆形矩形碰撞检测)

    var EventHandle = { addEvent:function(ele,type,handle){ if (ele.addEventListener) { ele.addEventList ...

  9. windows编程:画线,简单的碰撞检测,简单的帧率锁定

    #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <windowsx.h> #include <mmsy ...

随机推荐

  1. c# 获取客户端ip

    针对 HttpContext.Current.Request.UserHostAddress 如果客户使用的是代理,将获取不到真是的ip 要想透过代理服务器取得客户端的真实IP地址,就要使用 Requ ...

  2. USACO Section 1.3 Wormholes 解题报告

    题目 题目描述 在一个二维平面上有N个点,这N个点是(N/2)个虫洞的端点,虫洞的特点就是,你以什么状态从某个端点进去,就一定会以什么状态从另一端的端点出来.现在有一头牛总是沿着与X轴正方向平行的直线 ...

  3. Mac linux 安装memcached服务 用法

    今天在Mac上安装memcached服务的时候 由于安装memcached之前需要安装libevent依赖包 所以使用brew install libevent 安装过程中报错 Warning: Yo ...

  4. MyEclipse报错 Building workspace has encountered a problem Errors occurred during the build 的2种解决方法

    1: Building workspace has encountered a problem Errors occurred during the build 如果报错这个 那么有可能是jar包,报 ...

  5. Counting Islands II

    Counting Islands II 描述 Country H is going to carry out a huge artificial islands project. The projec ...

  6. 有关sqlitedrop数据库重建比delete方式来清空数据库更加有效率

    今天浏览stackoverflow 发现一个有趣的问题: which was more preferable as performance wise and without error cause t ...

  7. php正则表达式手册

    (http://deerchao.net/tutorials/regex/regex.htm)转载:作者:deerchao php的正则表达式很强大,学好了的确有很大的用处,但是正则表达式的规则很繁琐 ...

  8. Struts2--默认Action

    何时使用:  访问的网页不存在, 显示错误页面, 或者显示主页. 有时存在一种情况, 就是找不到对应action 可以在struts.xml里设置一个默认的action <?xml versio ...

  9. C#设置word段落首行缩进为0

    PublicVar.m_WordApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = ; PublicVar.m_WordApp.S ...

  10. C#平衡树(AVLTree)

    参考:http://www.cnblogs.com/skywang12345/p/3577479.html using System; using System.Collections.Generic ...