我在didBeginContact
委託方法動態添加SKSpriteNode
:無法動態添加SKSpriteNode?
這裏是我的代碼
-(void)adBall
{
SKSpriteNode *obj = [[SKSpriteNode alloc]initWithImageNamed:@"ball.png"];
obj.physicsBody = [SKPhysicsBody bodyWithTexture:obj.texture size:obj.texture.size];
obj.position = CGPointMake(100, 100);
obj.name = OBSTACLE_KEY;
obj.physicsBody.categoryBitMask = BallCategory1;
obj.physicsBody.contactTestBitMask = GreenLineCategory | RedLineCategory ;;
obj.physicsBody.collisionBitMask = GreenLineCategory | RedLineCategory ;;
obj.physicsBody.affectedByGravity = YES;
obj.physicsBody.dynamic = YES;
obj.physicsBody.friction = 0.0f;
obj.physicsBody.linearDamping = 0.0f;
obj.physicsBody.restitution = 1.0f;
obj.physicsBody.allowsRotation = NO;
[obj.physicsBody applyForce:CGVectorMake(200, 300)];
}
-(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 ([firstBody.node.name isEqual: OBSTACLE_KEY] && ([secondBody.node.name isEqual: BLACKLINE_KEY]))
{
[self adBall];
}
}
,但不能出現在屏幕上,它只是表明在球底部和消失。
如果在didMoveToView
方法中加上[self adBall];
則比工作正常。
因此,普爾茲告訴我我在這裏錯了嗎?
THX。我在那裏添加行,但它在我在didMoveToView委託方法中調用方法時起作用。不在didBeginContact中工作。 – LanWanMan 2015-01-06 15:49:49