飞机大战之六:敌机爆炸(碰撞检测)

参考网址:http://blog.csdn.net/jackystudio/article/details/11917875


源码下载地址:点击打开链接

关于svn的简单使用:点击打开链接


1.修改以上篇章中的错误遗漏(不然运行会报错)

在 BulletLayer.cpp AddBullet 中添加 this->m_pAllBullet->addObject(bullet);

在 EnemyLayer.cpp enemy1Blowup中 添加一个参数 NULL,

即 CCSequence *sequence = CCSequence::create(animate,removeEnemy1,NULL);


2. 对GameLayer的修改

在 GameLayer.h 文件中的 class 中 增加成员函数 void update(float delta);

在 GameLayer.cpp 实现该成员函数

void GameLayer::update(float delta)
{
    CCArray *bulletsToDelete = CCArray::create();
    bulletsToDelete->retain();
    CCObject *bt, *et;

    CCARRAY_FOREACH(this->bulletLayer->m_pAllBullet, bt)
    {
        CCSprite *bullet = (CCSprite*)bt;
        CCArray *enemy1sToDelete = CCArray::create();
        enemy1sToDelete->retain();
        int a = this->enemyLayer->m_pAllEnemy1->capacity();

        CCARRAY_FOREACH(this->enemyLayer->m_pAllEnemy1, et)
        {
            Enemy *enemy1 = (Enemy*)et;
            if(bullet->boundingBox().intersectsRect(enemy1->getBoundingBox()))
            {
                if(enemy1->getLife() == 1)
                {
                    enemy1->loseLife();
                    bulletsToDelete->addObject(bullet);
                    enemy1sToDelete->addObject(enemy1);
                }
                else ;
            }
        }
        CCARRAY_FOREACH(enemy1sToDelete, et)
        {
            Enemy *enemy1 = (Enemy*)et;
            this->enemyLayer->enemy1Blowup(enemy1);
        }
        enemy1sToDelete->release();
    }
    CCARRAY_FOREACH(bulletsToDelete, bt)
    {
        CCSprite *bullet = (CCSprite*)bt;
        this->bulletLayer->RemoveBullet(bullet);
    }

    bulletsToDelete->release();
}

并在 GameLayer::init() 中添加  this->scheduleUpdate();

3. 编译运行结果如图:


好的,我来为您讲解一下。 在Java后端飞机大战游戏中,检测我方飞机与敌机的碰撞需要进行以下步骤: 1. 首先,您需要在飞机和敌机类中定义一个Rectangle对象来表示它们的边界框。这个边界框可以用来检测碰撞。 2. 在游戏循环中,您需要遍历所有的敌机,并检测它们是否与我方飞机相撞。您可以使用Java中的intersects()方法来检测两个矩形是否相交。 3. 如果发现敌机与我方飞机相撞,您可以在游戏中处理这个碰撞事件,例如播放爆炸动画或者减少生命值。 下面是一个简单的代码示例: ``` // 在飞机和敌机类中定义边界框 public class Plane { private int x; private int y; private int width; private int height; private Rectangle boundingBox; // 边界框 // 构造函数 public Plane(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; this.boundingBox = new Rectangle(x, y, width, height); } // 判断与另一个矩形是否相交 public boolean intersects(Rectangle other) { return boundingBox.intersects(other); } // 其他方法... } // 在游戏循环中检测碰撞 public void gameLoop() { while (running) { // 遍历敌机 for (Enemy enemy : enemies) { // 检测敌机与我方飞机是否相撞 if (enemy.intersects(myPlane.boundingBox)) { // 处理碰撞事件 handleCollision(); } } // 其他游戏循环逻辑... } } // 处理碰撞事件 public void handleCollision() { // 播放爆炸动画或者减少生命值等操作... } ``` 希望这些讲解能够对您有所帮助,如果您有其他问题,请随时问我。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值