3

I have searched alot in order to make the sprite(_player) detected and collide with the other sprite(target) and being removed after the collision through out google and stackoverflow site, but could not get the solution in cocos2d-android, i'm able to find lots of information on cocos2d-iphone not on android, even i have compared my code with iphone code but could not make out. this is how i had done.

public class GameLayer extends CCColorLayer
 {
protected LinkedList<CCSprite> _targets;
protected LinkedList<CCSprite> _projectiles;
protected int _projectilesDestroyed;
protected CCSprite _player;
protected CCSprite _nextProjectile;

public static CCScene scene()
{
    CCScene scene = CCScene.node();
    CCColorLayer layer = new GameLayer(ccColor4B.ccc4(255, 255, 255, 255));

    scene.addChild(layer);

    return scene;
}


protected GameLayer(ccColor4B color)
{
    super(color);

    this.setIsTouchEnabled(true);

    _targets = new LinkedList<CCSprite>();
    _projectiles = new LinkedList<CCSprite>();
    _projectilesDestroyed = 0;

    CGSize winSize = CCDirector.sharedDirector().displaySize();

    _player = CCSprite.sprite("Player2.png");
    _player.setPosition(CGPoint.ccp(_player.getContentSize().width / 2.0f, winSize.height / 2.0f));

    addChild(_player);


    this.schedule("gameLogic", 1.0f);
    this.schedule("update");
}

update method

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



            _player = CCSprite.sprite("Player2.png");
            _player.setPosition(CGPoint.ccp(50,100));
            CGRect playerRect = CGRect.make(_player.getPosition().x - (_player.getContentSize().width / 2.0f),
                    _player.getPosition().y - (_player.getContentSize().height / 2.0f),
                    _player.getContentSize().width,
                    _player.getContentSize().height);

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


            else if (CGRect.intersects(playerRect, targetRect))
                _player.remove(_player);
            removeChild(_player, true);

        }

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

        if (targetsToDelete.size() > 0)
            projectilesToDelete.add(projectile);
    }
DD.
  • 973
  • 2
  • 10
  • 32
  • Are you using Dan Clarke example or else ? – Akarsh M May 24 '13 at 12:49
  • yes i'm using that tutorial only @user2078315 – DD. May 24 '13 at 12:57
  • So, the code is working fine when the two sprite collides. What error u got ? – Akarsh M May 27 '13 at 05:14
  • i'm not getting any error, the sprites are crossing each other but not getting collided and delete @user2078315 – DD. May 27 '13 at 05:21
  • I have also download the code ... And works fine in my case .. I'll check it again with ur code and ASAP ... – Akarsh M May 27 '13 at 05:49
  • there is not problem in the downloaded code it works fine, but i have added seperate ship sprite with cannon, when the fireball falls on the ship, ship is not deleted@user2078315 – DD. May 27 '13 at 06:08

1 Answers1

0

To find out the solution for this answer it took a week:( When a new sprite is added in the game first it should be declared in addsomefunction() method, my mistake was i declared the value of ship(sprite) in constructor not in the addTarget() and to remove the sprite after collision the code has to be given in public void spriteMoveFinished(Object sender) as

if (sprite.getTag() == 25)
_ships.remove(sprite);

since i had Target(sprite) in this method got confused like where to add the code.

DD.
  • 973
  • 2
  • 10
  • 32