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

参考网址: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. 编译运行结果如图:


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
public abstract class FlyingObject { public static final int LIFE = 0; //活着呢 public static final int DEAD = 1; //死了的(先爆破) public static final int REMOVE = 2; //删除了(爆破后删除) protected int state = LIFE; //当前状态(默认活着) protected int width; //宽 protected int height; //高 protected int x; //x坐标 protected int y; //y坐标 /** 无参构造方法 */ public FlyingObject(){ } /**专门给小敌机、大敌机、小蜜蜂提供的构造方法 */ public FlyingObject(int width,int height){ this.width = width; this.height = height; Random rand = new Random(); x = rand.nextInt(World.WIDTH-width); //x:0到(窗口-小敌机的宽)之间的随机数 y = -height; //y:负的小敌机的高 } /** 专门给英雄机、子弹、天空提供的构造方法 */ public FlyingObject(int width,int height,int x,int y){ this.width = width; this.height = height; this.x = x; this.y = y; } /** 读取图片 */ public static BufferedImage loadImage(String fileName){ try{ BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName)); return img; }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(); } } /** 飞行物移动了 */ public abstract void step(); public abstract BufferedImage getImage(); /** 判断是否活着 */ public boolean isLife(){ return state==LIFE; } /** 判断是否死了的 */ public boolean isDead(){ return state==DEAD; } /** 判断是否删除的 */ public boolean isRemove(){ return state==REMOVE; } /** 画对象 g:画笔 */ public void paintObject(Graphics g){ g.drawImage(getImage(), x, y, null); } /** 检测飞行物是否越界 */ public abstract boolean outOfBounds(); /** 敌人与子弹/英雄机的碰撞 this:敌人 other:子弹或英雄机 */ public boolean hit(FlyingObject other){ int x1 = this.x-other.width; //x1:敌人的x int x2 = this.x+this.width; //x2:敌人的x int y1 = this.y-other.height; //y1:敌人的y int y2 = this.y+this.height; //y2:敌人的y int x = other.x; //x:子弹的x int y = other.y; //y:子弹的y return x>=x1 && x=y1 && y<=y2; } public void goDead(){ state = DEAD; //修改当前状态为死了的 } }
好的,我来为您讲解一下。 在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、付费专栏及课程。

余额充值