2013-03-13 78 views
0

我有一个游戏,用户在开始时收集不同类型的对象和一个值为0的标签。每次用户收集一个对象(通过触摸它),它应该使得分数=当前分数+1;我尝试了下面的代码,但是当我点击对象时它崩溃了。Cocos2d项目中的得分系统

这是在屏幕上放一个0我的分数标签代码:

score = 0; 
scoreLabel1 = [CCLabelTTF labelWithString:@"0" fontName:@"Times New Roman" fontSize:33]; 
scoreLabel1.position = ccp(240, 160); 
[self addChild:scoreLabel1 z:1]; 

这是无效的功能,我打电话我每次碰的对象:

- (void) addScore 
{ 
    score = score + 1; 
    [scoreLabel1 setString:[NSString stringWithFormat:@"%@", score]]; 
} 

这是我放置触摸物体的代码的实际部分:

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self ccTouchesMoved:touches withEvent:event]; 

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

for (Apple in self.appleArray) 
{ 
    if (CGRectContainsPoint(Apple.boundingBox, location)) 
    { 
     [self addScore]; 
     Apple.visible = NO;    
    } 
} 

其他所有工作e除了分数。还有一种方法可以让苹果消失,而不是通过apple.visible = false使其不可见?因为这种方式苹果仍然存在但不可见,我想摆脱它。

希望有人能帮助!

如果您有任何问题,请告诉我。

谢谢。

这是我画的苹果:

-(id) init 
    { 
     // always call "super" init 
     // Apple recommends to re-assign "self" with the "super's" return value 

    if((self=[super init])) { 
    isTouchEnabled_ = YES; 
    self.appleArray = [CCArray arrayWithCapacity:20]; 

     for (int i = 0; i < 5; i++) { 

      Apple = [CCSprite spriteWithFile:@"Apple4.png"]; 
      [self addChild:Apple]; 
      [appleArray addObject:Apple]; 
     } 
    [Apple removeFromParentAndCleanup:true]; 
    [self scheduleUpdate]; 

    } 
    return self; 
} 

而这正是在屏幕被更新:

-(void) update: (ccTime) dt 
{ 
for (int i = 0; i < 5; i++) { 

    Apple = ((CCSprite *)[appleArray objectAtIndex:i]); 
    if (Apple.position.y > -250) { 
     Apple.position = ccp(Apple.position.x, Apple.position.y - (Apple.tag*dt)); 
    } 
} 

}

+0

如果你想删除而不是使不可见,使用方法'[object removeFromParentAndCleanup:true]'。也为了将来,请不要在一个问题中提出两个问题。而是写下两个问题。 – dqhendricks 2013-03-13 21:27:44

+0

嗨dghendricks,谢谢你的帮助。我试过[苹果removeFromParentAndCleanup:真],它似乎没有工作。苹果仍然会留在那里,但看不见,所以当我继续按下它时,它会继续为分数标签添加一个点。 – 2013-03-14 20:13:03

+0

如果你调用'[apple removeFromParentAndCleanup:true]',那么苹果就没有办法留在屏幕上了。要么你看到一个不同的苹果,你实际上并没有调用'[apple removeFromParentAndCleanup:true]',或者你正在存储对苹果的引用(一个数组?),并触发基于苹果的不同对象的点击不管是否在屏幕上的位置属性。 – dqhendricks 2013-03-14 20:15:35

回答

1

几件事情在这里。在setScore中,你的格式被破坏并且会导致崩溃(%@需要一个NSObject *)。尝试:

[scoreLabel1 setString:[NSString stringWithFormat:@"%i", score]]; 

此外,for循环的语法很奇怪。尝试

for (Apple *anyApple in self.appleArray) 
{ 
    if (CGRectContainsPoint(anyApple.boundingBox, location)) 
    { 
     if (anyApple.visible) { 
      [self addScore]; 
      anyApple.visible = NO; 
     }   
    } 
} 
+0

嘿,谢谢你的帮助。它现在是战船。有没有办法让苹果消失在屏幕上,而不是使可见=否; ? 由于这种方式苹果设置为隐形,但它仍然在屏幕上,所以如果我一直按它,它会继续增加1的分数。我希望苹果一旦被点击就被销毁。 – 2013-03-13 22:24:15

+0

见编辑。但是,我不知道你的应用程序,并不确定你为什么要保持对象。 – YvesLeBorg 2013-03-13 22:51:44

+0

你的意思是[object removeFromParentAndCleanup:true]?对象是苹果?它不工作,当我这样做,我不想保持对象周围。我希望它被删除 – 2013-03-13 23:05:19

0
score = score + 1; 
[scoreLabel1 setString:[NSString stringWithFormat:@"%@", score]]; 

请仔细阅读本:String Format Specifiers

%@ - Objective-C的对象,印刷(如果可用)由descriptionWithLocale:返回的字符串,或说明,否则。也可与CFTypeRef对象一起使用,返回CFCopyDescription函数的结果。

如果您的score是一个对象,则不能以这种方式“增加”它的值。如果它是一个int或一个float,则使用错误的格式说明符。

+0

噢好吧我明白了,非常感谢您的帮助!我现在有点工作。赞赏。 – 2013-03-14 20:17:50