我正在使用spritekit创建游戏。
用户控制在球幕上上下击球的球拍,每次球击中球拍时,得分计数器增加1。我想做的是当用户的分数等于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
消息,但场景未添加另一个球。这是为什么?我完全错过了什么吗?任何帮助表示赞赏。
答案 0 :(得分:0)
尝试使用动作执行选择器调用,例如;
-(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]];
}