如何检查精灵是否被触摸,并在与cocos2d-android中的其他精灵相撞时将其删除

时间:2013-05-22 06:50:25

标签: android cocoa-touch sprite cocos2d-android

要学习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!"));
    }
}

0 个答案:

没有答案