如何在cocos2d android中与其他对象相撞时删除对象(sprite)?

时间:2013-05-22 06:08:48

标签: android 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!"));
        }
    }

}

1 个答案:

答案 0 :(得分:3)

像这样添加射弹与船的交叉代码

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()。宽度,                         。target.getContentSize()高度);

        System.out.println("projectiles to delete continue... : " + volume);


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




                {
                for (CCSprite ship : _ships)
                {
                        CGRect shipRect = CGRect.make(ship.getPosition().x - (ship.getContentSize().width/2),
                                ship.getPosition().y - (ship.getContentSize().height/2),
                                    ship.getContentSize().width,
                                ship.getContentSize().height);
                        System.out.println("ships to delete continue... : " + volume);
                              if (CGRect.intersects(targetRect, shipRect))
                                {

                                  System.out.println("ships intersected:)... : " + volume);
                                     shipsToDelete.add(ship);
                                }


                    if (targetsToDelete.size() > 0)
                        shipsToDelete.add(ship);
                }



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

            CCDirector.sharedDirector().replaceScene(Gameoverlayer.scene("You Win!"));
        }
    }

}

并在addsomeothercode()方法中添加精灵,并通过给出代码

删除精灵
public void spriteMoveFinished(Object sender)
{
    CCSprite sprite = (CCSprite)sender;


    if (sprite.getTag() == 1)
    {  if (sprite.getTag() == 1)
        _ships.remove(sprite);
    }