简体   繁体   中英

How to find the whether the sprite is collide with the other sprite in cocos2d-android?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM