我有一个游戏,用户在开始时收集不同类型的对象和一个值为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));
}
}
}
如果你想删除而不是使不可见,使用方法'[object removeFromParentAndCleanup:true]'。也为了将来,请不要在一个问题中提出两个问题。而是写下两个问题。 – dqhendricks 2013-03-13 21:27:44
嗨dghendricks,谢谢你的帮助。我试过[苹果removeFromParentAndCleanup:真],它似乎没有工作。苹果仍然会留在那里,但看不见,所以当我继续按下它时,它会继续为分数标签添加一个点。 – 2013-03-14 20:13:03
如果你调用'[apple removeFromParentAndCleanup:true]',那么苹果就没有办法留在屏幕上了。要么你看到一个不同的苹果,你实际上并没有调用'[apple removeFromParentAndCleanup:true]',或者你正在存储对苹果的引用(一个数组?),并触发基于苹果的不同对象的点击不管是否在屏幕上的位置属性。 – dqhendricks 2013-03-14 20:15:35