我有一排小行星在屏幕左侧飞向我的角色。如果他们打了我的角色游戏。如果它没有击中我的角色并到达屏幕的末尾,我希望得分系统在这种情况下将分数增加1。我正在使用xcode 5.我怎样才能实现这一目标。此代码显示了小行星的移动
double curTime = CACurrentMediaTime();
if (curTime > _nextAsteroidSpawn) {
//NSLog(@"spawning new asteroid");
float randSecs = [self randomValueBetween:0.20 andValue:1.0];
_nextAsteroidSpawn = randSecs + curTime;
float randY = [self randomValueBetween:0.0 andValue:self.frame.size.height];
float randDuration = [self randomValueBetween:2.0 andValue:10.0];
SKSpriteNode *asteroid = [_asteroids objectAtIndex:_nextAsteroid];
_nextAsteroid++;
if (_nextAsteroid >= _asteroids.count) {
_nextAsteroid = 0;
}
[asteroid removeAllActions];
asteroid.position = CGPointMake(self.frame.size.width+asteroid.size.width/2, randY);
asteroid.hidden = NO;
CGPoint location = CGPointMake(-self.frame.size.width-asteroid.size.width, randY);
SKAction *moveAction = [SKAction moveTo:location duration:randDuration];
SKAction *doneAction = [SKAction runBlock:(dispatch_block_t)^() {
//NSLog(@"Animation Completed");
asteroid.hidden = YES;
}];
SKAction *moveAsteroidActionWithDone = [SKAction sequence:@[moveAction, doneAction ]];
[asteroid runAction:moveAsteroidActionWithDone withKey:@"asteroidMoving"];
答案 0 :(得分:0)
只需更改完成的操作完成块:
SKAction *doneAction = [SKAction runBlock:(dispatch_block_t)^() {
//NSLog(@"Animation Completed");
asteroid.hidden = YES;
++myAwesomeScore;
}];
我无法看到你在小行星与玩家发生碰撞时如何摧毁小行星,但是如果你只是隐藏它们而不是完全删除它们(因此仍然会击中runBlock
。然后只需修改它有以下几点:
SKAction *doneAction = [SKAction runBlock:(dispatch_block_t)^() {
//NSLog(@"Animation Completed");
asteroid.hidden = YES;
if (asteroid.isNotDead) ++myAwesomeScore;
}];