精灵没有跟踪计数

时间:2014-12-22 13:23:39

标签: ios objective-c sprite-kit skspritenode

我正在使用的以下代码是当我在场景中的球数达到零时结束游戏。现在,当我输入代码时,我没有收到任何错误,但它没有做任何事情,因为计算球并对该计数作出反应。我对精灵套件相当新,所以我明白这是一个容易让别人感到困扰的问题,但这是第一次对场景中某个精灵的计数做出反应,所以请帮忙。

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];

    }
    }

2 个答案:

答案 0 :(得分:1)

我认为应该是

return numberOfBalls == 0
你的isgameWon方法中的

你忘记了额外的=

答案 1 :(得分:1)

  1. 删除ball全局变量,因为它不需要。

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

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