【问题标题】:Can't add node to my scene (Sprite Kit)无法将节点添加到我的场景(Sprite Kit)
【发布时间】:2014-10-31 15:54:53
【问题描述】:

我正在使用 spritekit 创建游戏。

用户控制一个在屏幕上上下敲击球的球拍,每次球击中球拍,分数计数器都会加一。我想做的是当用户的分数等于 5 时将第二个球添加到游戏中,但是无论出于何种原因,当我尝试将第二个球添加到场景中时,它都没有被添加。

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

SKPhysicsBody *firstBody, *secondBody


if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
    firstBody = contact.bodyA;
    secondBody = contact.bodyB;
}
else {
    firstBody = contact.bodyB;
    secondBody = contact.bodyA;
}

// if a body in the scene makes contact with the paddle
// shoot the ball back up
if ((firstBody.categoryBitMask & ballCategory) != 0 && (secondBody.categoryBitMask & paddleCategory) != 0) {
    // move ball up
    [firstBody applyImpulse:CGVectorMake(arc4random() % 60 + 20, arc4random() % 80 + 50)];

    // increment score
    self.score++;

    // update score
    self.deathLabel.text = [NSString stringWithFormat:@"%i", self.score];

    // if the game score is 5, add another ball to the scene
    if (self.score == 5) {
        NSLog(@"test");
        [self addChild:ball2];
        }
    }
}

'ball2' 在尝试将其添加到场景之前已被预初始化。运行游戏并获得 5 分时,正在调用 NSLog 消息,但场景没有添加另一个球。这是为什么?我完全错过了什么吗?任何帮助表示赞赏。

【问题讨论】:

    标签: ios objective-c sprite-kit


    【解决方案1】:

    例如尝试用action调用执行选择器;

    -(void)secondBall
    {
        SKSpriteNode *secondBall = [SKSpriteNode spriteNodeWithImageNamed:@"secondBall"];
        secondBall.position = yourPosition;
        [self addChild:secondBall];
    }
    
    //Your contact
    if (self.score == 5) {
            [self runAction:[SKAction performSelector:@selector(secondBall) onTarget:self]];
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      相关资源
      最近更新 更多