1

我正在尝试在 cocs2d 中制作评分系统,但我在评分中遇到了一些问题,我已经在 .m 中编写了这些

if (CGRectContainsPoint(bug.boundingBox, location)) {
        [self removeChild:bug cleanup: YES];
        score += 1;
        NSLog(@"%i", score);
    }

我已经在 .h 中声明了分数

@interface GameScene : CCLayer {
        NSMutableArray *bugs;
        int score;

}

但是如果我触摸精灵,我会得到 1 分并且精灵会被移除,但是当我触摸精灵所在的地方时,每次我触摸那里我都会得到 +1 分。

我希望你明白..谢谢,,

4

3 回答 3

0

我怀疑,从场景图中删除后,您需要从错误数组中删除该错误。

于 2012-07-09T14:52:45.020 回答
0

您的源代码较少。但是,您可以使用将在接口中声明的整数变量。在实现中将整数变量设置为 0(在 init 方法中),当您创建新的错误时,将整数值再增加一并将错误精灵标签设置为整数值,如下所示:

// Interface
@interface yourScene:CCLayer {
    int bugsCount; // add new variable for counting current bugs
}

// implementation
@implementation
-(id) init {   

    if( (self=[super init])) {

        // Your code in init

        // integer value for counting current bugs
        bugsCount = 0;

    }

    return self;
}

-(void) addNewBug {

    // your code of adding bugs
    CCSprite *bug = [CCSprite spriteWithFile:@"bug.png"];
    bugsCount++;
    bug.tag = bugsCount; // Your bug sprite tag should be equal current bugsCount value
    bug.position = ccp(x,y);
    [self addChild:bug];

}

在 ccTouchEnded 方法中,只需执行 for 循环来检测选择了哪些错误,如下所示:

-(void) ccTouchesEnded:(NSSet*)touches withEvent:(id)event {

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location];
    location = [self convertToNodeSpace:location];

    // Detecting bug by touch
    for (i=1; i <= bugsCount; i++) {

        // get bug sprite by tag (from bugsCount idea)
        CCNode *bug = [self getChuldByTag:i];
        if (CGRectContainsPoint(bug.boundingBox, location)) {

            // add new score (now this will work)
            score += 1;
            NSLog(@"%i", score);

            // remove bug from scene
            [self removeChildByTag:1 cleanup:YES];

            // decrease number of bugs
            bugsCount--;
        }
    }

}
于 2012-07-09T19:41:25.303 回答
0

这是一种应该适合您的简单方法(当然可能更有效,但我决定保持简单)

创建一个名为 Bug 的类,其中包含一些属性,其中例如一个名为 'killed' 的 BOOL,当一个 bug 被杀死时,你将其设置为 true。让 bug 引用 sprite,或扩展 sprite 类,以便您知道它在哪里以及是否被触摸。

现在,当你按下一个 bug 时,一定要检查它是否被杀死,如果是,不要为它打分。

将您所有的错误对象添加到您的错误数组中,而不是您的精灵。您可以通过错误对象访问精灵。当您的错误被杀死时,重新使用它们以节省内存(即将它们从屏幕上移开并让它们稍后再次出现,再次将“killed”属性设置为false)

快乐的寻虫!

于 2012-07-09T14:59:42.390 回答