cocos2d-x 精灵(如子弹)的去除

举例代码如下:

 //游戏子弹与怪物的碰撞检测,当发生碰撞时从游戏中去除相碰撞的子弹与怪物

    //getEemyArray()为定义在PlayScene类中得到怪物数组的函数,  下面语句体在子弹类中Bullet 继承 CCSprite

    for(int i = 0; i < PlayScene::sharedPlay()->getEnemyArray()->count(); i ++)

    {

        Enemy* enemy = (Enemy*) PlayScene::sharedPlay()->getEnemyArray()->objectAtIndex(i);

        if(this->boundingBox().intersectsRect(enemy->boundingBox()))

        {

            PlayScene::sharedPlay()->getEnemyArray()->removeObject(enemy); //从怪物数组中去除对象

            PlayScene::sharedPlay()->removeChild(enemy, true); //从游戏的当前层上去除相撞怪物

            PlayScene::sharedPlay()->removeChild(this, true); //从游戏当前层上去除相撞子弹 this代表当前Bullet

        }

    }





//判断主角与怪物碰撞

    for(int i = 0; i < PlayScene::sharedPlay()->getEnemyArray()->count(); i++)

    {

        Enemy* enemy = (Enemy*)PlayScene::sharedPlay()->getEnemyArray()->objectAtIndex(i);

        if(this->boundingBox().intersectsRect(enemy->boundingBox()))

        {

            PlayScene::sharedPlay()->getEnemyArray()->removeObject(enemy); //从数组中移除

            PlayScene::sharedPlay()->removeChild(enemy,this); //从当前层中移除

           

        }

    }

 

你可能感兴趣的:(cocos2d-x)