【问题标题】:Sprite not keeping track of count雪碧不记录计数
【发布时间】:2014-12-22 22:03:35
【问题描述】:

我使用的以下代码是当我在场景中的球数达到零时结束游戏。现在,当我输入代码时,我没有收到任何错误,但它并没有做任何事情,比如数球并对数做出反应。我对精灵套件相当陌生,所以我知道这是一个对其他人来说很容易的问题,但这是第一次对场景中某个精灵的计数做出反应,所以请帮忙。

SKSpriteNode *ball;
int numberOfBalls = 3;

@implementation EasyScene

-(void)didBeginContact:(SKPhysicsContact *)contact {
    if ([self isGameWon]) {
    EasyEndGameScene *end = [EasyEndGameScene sceneWithSize:self.size];
    [self.view presentScene:end transition:[SKTransition doorsCloseHorizontalWithDuration:1]];
}
}

-(BOOL)isGameWon {
int numberOfBalls = 3;
for (SKNode* node in self.children) {
    if ([node.name isEqual: ball]) {
        numberOfBalls = 0;
    }
}
return numberOfBalls =0;
}

- (void) addBalls:(CGSize)size {
for (int i = 0; i < 3; i++) {
    //create brick sprite from image
    SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball-image"];
    ball.name = @"ball";
    //resize balls
    ball.size = CGSizeMake(self.size.width/5.5, self.size.width/5.5);
    //position and space out balls
    int xPos = size.width/3 * (i+.5);
    int yPos = self.size.height - (self.size.width/7);
    ball.position = CGPointMake(xPos, yPos);
    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.size.width/2];
    ball.physicsBody.categoryBitMask = ballCategory;
    ball.physicsBody.contactTestBitMask = bottomEdgeCategory;

    [self addChild:ball];

}
}

在下面的代码中,我去掉了全局变量并测试了@"ball" 的指定名称的所有节点,如果没有找到则返回 YES:

   static const uint32_t ballCategory   = 1;       //00000000000000000000000000000001
   static const uint32_t edgeCategory  = 2;       //00000000000000000000000000000010
   static const uint32_t bottomEdgeCategory = 4;       //00000000000000000000000000000100

   SKSpriteNode *ball;

   @implementation EasyScene

  -(void)didBeginContact:(SKPhysicsContact *)contact {

   //create placeholder for the "non ball" object
   SKPhysicsBody *notTheBall;
   SKPhysicsBody *theBall;

    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
    notTheBall = contact.bodyB;
    theBall = contact.bodyA;
     } else {
    notTheBall = contact.bodyA;
    theBall = contact.bodyB;
     }

    if (notTheBall.categoryBitMask == bottomEdgeCategory) {
    NSLog(@"hit bottom edge");
    //        SKAction *playSFX = [SKAction playSoundFileNamed:@"gameover.mp3" waitForCompletion:NO];
    //        [self runAction:playSFX];
     //        EasyEndGameScene *end = [EasyEndGameScene sceneWithSize:self.size];
     //        [self.view presentScene:end transition:[SKTransition doorsCloseHorizontalWithDuration:1]];
    //        [GameState sharedInstance].score = 0;
    //        [gameMusic pause];
    [theBall.node removeFromParent];
    }

    if ([self isGameWon]) {
    EasyEndGameScene *end = [EasyEndGameScene sceneWithSize:self.size];
    [self.view presentScene:end transition:[SKTransition doorsCloseHorizontalWithDuration:1]];
    }
    }

    -(BOOL)isGameWon {
    unsigned count = 0;
    for (SKNode* node in self.children)
    if ([node.name isEqual:ball])
        count++;
    return count == 0;
    }

    - (void) addBalls:(CGSize)size {
    for (int i = 0; i < 3; i++) {
    //create brick sprite from image
    SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball-image"];
    ball.name = @"ball";
    //resize balls
    ball.size = CGSizeMake(self.size.width/5.5, self.size.width/5.5);
    //position and space out balls
    int xPos = size.width/3 * (i+.5);
    int yPos = self.size.height - (self.size.width/7);
    ball.position = CGPointMake(xPos, yPos);
    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.size.width/2];
    ball.physicsBody.categoryBitMask = ballCategory;
    ball.physicsBody.contactTestBitMask = bottomEdgeCategory;

    [self addChild:ball];

    }
    }

【问题讨论】:

  • 设置断点以验证代码是否按预期执行

标签: ios objective-c sprite-kit skspritenode


【解决方案1】:
  1. 删除ball 全局变量,因为它不需要。

  2. 测试指定名称@"ball"的所有节点,如果没有找到则返回YES

    -(BOOL)isGameWon {
        for (SKNode* node in self.children)
           if ([node.name isEqualToString:@"ball"])
               return NO;
        return YES;
    }
    

【讨论】:

  • 好的,当一个球离开时,比赛结束了。不过,这已经取得了一些进展。但我确实需要它只在我还剩 0 个球时才结束。有什么想法吗?
  • @NicholasWhitley 好的,我已经更新了我的答案,我相信它应该现在可以工作了......请告诉我。
  • 好吧,我认为我做得对,但当第一个球击中它时,它仍然只是结束游戏。我添加了我拥有的代码,以便您查看它是否有任何错误。 (对不起,有点乱,编辑搞砸了)。谢谢@Droppy
  • 你需要用修改后的代码更新你的问题(它可以在其他代码之后)。
  • 把它放在问题中。 @Droppy
【解决方案2】:

我觉得应该是

return numberOfBalls == 0

在你的 isgameWon 方法中

你忘记了一个额外的 =

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多