1

为了学习 cocos2d-android 游戏引擎,我开始使用本教程开发游戏,http://dan.clarke.name/2011/05/how-to-make-a-simple-android-game-with-cocos2d-pt2- %E2%80%93-rotating-turrets/增加了 3 艘船以使其更具吸引力并固定在其上的大炮,如果火球从船上落下,船将被烧毁并以此为概念编码了,但是当火球落在船上时,它并没有被摧毁(删除),我尝试了很多方法,但找不到解决方案,谁能告诉我为什么船没有被摧毁?这是我的编码。

public void update(float dt)
 {
LinkedList<CCSprite> projectilesToDelete = new LinkedList<CCSprite>();

for (CCSprite projectile : _projectiles)
{
    CGRect projectileRect = CGRect.make(projectile.getPosition().x - (projectile.getContentSize().width / 2.0f),
                                        projectile.getPosition().y - (projectile.getContentSize().height / 2.0f),
                                        projectile.getContentSize().width,
                                        projectile.getContentSize().height);




    LinkedList<CCSprite> targetsToDelete = new LinkedList<CCSprite>();

    for (CCSprite target : _targets)
    {
        CGRect targetRect = CGRect.make(target.getPosition().x - (target.getContentSize().width),
                                        target.getPosition().y - (target.getContentSize().height),
                                        target.getContentSize().width,
                                        target.getContentSize().height);

        if (CGRect.intersects(projectileRect, targetRect))
            targetsToDelete.add(target);
    }

    LinkedList<CCSprite> shipsToDelete = new LinkedList<CCSprite>();
    for (CCSprite ship : _ships)
     {
            CGRect shipRect = CGRect.make(ship.getPosition().x - (ship.getContentSize().width),
                                         ship.getPosition().y - (ship.getContentSize().height),
                                         ship.getContentSize().width,ship.getContentSize().height);


            for (CCSprite target : _targets)
              {
                    CGRect targetRect = CGRect.make(target.getPosition().x - (target.getContentSize().width),
                                                                              target.getPosition().y -
                                                                            (target.getContentSize().height),
                                                            target.getContentSize().width,target.getContentSize().height);


                 if (CGRect.intersects(targetRect, shipRect))
                    {
                            shipsToDelete.add(ship);
                            break;
                    }
            }                      
     }

    for (CCSprite target : targetsToDelete)
    {
        _targets.remove(target);
        removeChild(target, true);
    }

    if (targetsToDelete.size() > 0)
        projectilesToDelete.add(projectile);

    for (CCSprite ship : shipsToDelete)
    {
        _ships.remove(ship);
        removeChild(ship, true);
    }
}

for (CCSprite projectile : projectilesToDelete)
{
    _projectiles.remove(projectile);
    removeChild(projectile, true);

    if (_shipDestroyed > 0)
    {
        _shipDestroyed = 0;
        CCDirector.sharedDirector().replaceScene(Gameoverlayer.scene("You Win!"));
    }
}
4

0 回答 0