cocos2d-x-3.0 alpha1与C++11练习五:飞镖忍者,如何检测碰撞(2)
来源:未知 责任编辑:责任编辑 发表时间:2014-01-20 07:51 点击:次
p>
p>
p>修改onTouchesEnded(),把新子弹加入到子弹数组中,并设置其tag为2:
p>
p>
p>...
p> projectile->runAction(Sequence::create(
p> MoveTo::create(realDuration, realDest)
p> ,[this](Node *sender){
p> <span style="color: #ff6600;">this->removeChild(sender);
p> _projectiles->removeObject(sender);</span>
p> }
p> , NULL));
p> <span style="color: #ff6600;">projectile->setTag(2);
p> _projectiles->addObject(projectile);
p></span>
p>
p>
p>2,在每桢中碰撞检测
p>下面的update()函数用于检测每帧的碰撞,并从游戏中删除碰撞中的子弹和怪物。
p>请在HelloWorldScene.h中声明,在HelloWorldScene.cpp中定义:
p>
p>
p> void update(float delta);
p>...
p> void HelloWorld::update(float delta){
p> cocos2d::Array *projectilesToDelete = Array::create();
p>
p> Object *obj1 = NULL;
p> Object *obj2 = NULL;
p>
p> CCARRAY_FOREACH(_projectiles, obj1){
p> Sprite *projectile = (Sprite *)obj1;
p> Array *targetsToDelete = Array::create();
p> CCARRAY_FOREACH(_targets, obj2){
p> Sprite *target = (Sprite *)obj2;
p> if (projectile->getBoundingBox().intersectsRect(target->getBoundingBox())) {
p> targetsToDelete->addObject(target);
p> }
p> }
p>
p> CCARRAY_FOREACH(targetsToDelete, obj2){
p> Sprite *target = (Sprite *)obj2;
p> _targets->removeObject(target);
p> this->removeChild(target);
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>