当两个精灵被删除时,如何在cocos2d android中增加分数

时间:2013-05-17 05:31:17

标签: android game-physics cocos2d-android

我有一个精灵(图像)当它与另一个分数碰撞时应该增加10,相关代码在更新方法中给出,但是它会随机增加。 这是我的代码。

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


        }

2 个答案:

答案 0 :(得分:3)

将myscore增加10后,你需要像这样更新它

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

答案 1 :(得分:1)

你的分数增加应该在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);
    }
}