Search code examples
androidgame-physicscocos2d-android

how to increase the score in cocos2d android when two sprites gets deleted


I have a sprite(image) when it collide with another score should be increament by 10, related code is given in the update method, but instead of this it increases randomly. here's my code.

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);
            myscore += 10;
            showLabel(myscore);


        }

Solution

  • After increasing the myscore by 10 you need to update it like this

    myscore += 10; 
    updateTable(myscore); 
    showLabel(myscore); 
    addTarget();