0

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


        }
samm
  • 482
  • 5
  • 14

2 Answers2

3

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

myscore += 10; 
updateTable(myscore); 
showLabel(myscore); 
addTarget(); 
DD.
  • 973
  • 2
  • 10
  • 32
1

Your score increase should be within the if:

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);
    }
}
HalR
  • 11,411
  • 5
  • 48
  • 80
  • thanks for your reply, i have placed the code for it inside the "if" statement only. @HaIR – samm May 17 '13 at 05:42