2014-10-30 70 views
0

我正在使用spritekit创建游戏。无法将节点添加到我的场景(Sprite Kit)

用户控件敲球上下屏幕桨,每一次球由一个打桨比分计数器递增。当用户的分数等于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

尝试动作调用执行选择,例如;

-(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]]; 
    } 
+0

太感谢你了! – Surfero 2014-10-30 21:35:32

+0

欢迎您,继续编码:) – 2014-10-30 22:49:17