Search code examples
androidcocos2d-android

How to make object(sprite) deleted when collided with other object in cocos2d android?


To learn cocos2d-android game engine, i started developing game using this tutorial, http://dan.clarke.name/2011/05/how-to-make-a-simple-android-game-with-cocos2d-pt2-%E2%80%93-rotating-turrets/ and added 3 ships in order to make it more attractive and cannons fixed on to it, if fireball falls from the sky on ship, ship will be burnt and destroyed by keeping this as the concept did the coding, but when fireballs falls on the ship it isnt getting destroyed(deleted), i tried in many ways but could not get the solution can anybody tell why the ships is not destroyed please? here's my coding for it.

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

}

Solution

  • Add the code for intersection of projectile verses ship like this

    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);

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

    }

    and add the sprite in addsomeothercode() method, and delete the sprite by giving the code as

    public void spriteMoveFinished(Object sender)
    {
        CCSprite sprite = (CCSprite)sender;
    
    
        if (sprite.getTag() == 1)
        {  if (sprite.getTag() == 1)
            _ships.remove(sprite);
        }