【发布时间】:2014-04-14 00:04:12
【问题描述】:
我正在开发一款游戏,我希望每次将某些内容添加到屏幕上时它都会计数。我设置了NSMutableArrays 来包含我添加到屏幕的内容。有 6 个数组,每个数组每 x 秒生成一个项目。这是我为评分设置的:
if (!_scoreLabel) {
_scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"DIN Condensed"];
_scoreLabel.fontSize = 40;
_scoreLabel.position = CGPointMake(self.frame.size.width - 20, self.frame.size.height - 40);
_scoreLabel.fontColor = [SKColor colorWithHue:0 saturation:0 brightness:1 alpha:0.5];
_scoreLabel.zPosition = 2.0;
[self addChild:_scoreLabel];
}
//add each debris array
_scoreLabel.text = [NSString stringWithFormat:@"%d", _debris.count + _debris2.count + _debris3.count + _debris4.count + _debris5.count + _debris6.count];
此代码在玩游戏的最初一段时间内运行良好(从_debris1 - _debris3)然后当debris4 和其他人开始生成时,得分不会正确地加 1 加 1。而是开始添加分数,(如果那很混乱:对于游戏的第一部分,它每添加一个碎片到游戏中就添加1,然后当4和其他人来时,它开始添加3,7,3 , (随机数),得分到_scoreLabel。
这样做有什么理由吗? 是否可以让NSMutabaleArray 包含所有这些数组作为子数组?我应该以不同的方式计算这个分数吗?
如果需要,这是一个示例 spawnDebris 方法:
-(void)spawnDebris {
SKSpriteNode * debris = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:@"debris1.png"] size:CGSizeMake(70, 70)];
debris.zPosition = 1.0;
debris.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];
debris.physicsBody.allowsRotation = NO;
debris.physicsBody.categoryBitMask = CollisionDebris;
RandomPosition = arc4random() %300;
RandomPosition = RandomPosition + 20;
debris.position = CGPointMake (RandomPosition, self.size.height + 40);
[_debris addObject:debris]; //_debris is the NSMutableArray
[self addChild:debris];
//next Spawn:
[self runAction:[SKAction sequence:@[
[SKAction waitForDuration:deb1Time],
[SKAction performSelector:@selector(spawnDebris) onTarget:self],
]]];
if (_dead == YES) {
[self removeAllActions];
}
if (debris.position.y > 568) {
[self removeFromParent];
}
提前致谢。 如果需要任何其他信息,请告诉我。
【问题讨论】:
-
如何简单地将评分从 1 更改为更高的数字(如 3)或其他什么,这样用户就无法判断它何时随机变化?
-
按数组计数@ShenHutah
-
关于最后一个 if 语句:不应该是 [debris removeFromParent];而不是 [self removeFromParent]; ?
标签: ios objective-c sprite-kit