[英]Issue with physics bodies when subclassing SKSpriteNode?
I am having an issue while subclassing SKSpriteNode. 我在子类化SKSpriteNode时遇到了问题。 Here's my implementation of the subclass:
这是我的子类实现:
@implementation SSSquirrel
- (id) init {
if (self = [super init]) {
// Create Squirrel and physics body
SKSpriteNode *squirrel = [SKSpriteNode spriteNodeWithImageNamed:@"squirrelNormal"];
squirrel.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:squirrel.size.width/2 - 10];
squirrel.physicsBody.dynamic = NO;
squirrel.physicsBody.affectedByGravity = NO;
squirrel.physicsBody.restitution = 0.8;
squirrel.physicsBody.friction = 0.1;
squirrel.zPosition = 1.0;
[self addChild:squirrel];
}
return self;
}
@end
Then I am creating a squirrel in my main game scene as so: 然后我在我的主要游戏场景中创建了一只松鼠:
SSSquirrel *squirrel1 = [[SSSquirrel alloc] init];
squirrel1.position = CGPointMake(offset, self.frame.size.height - offset);
[self addChild:squirrel1];
Everything is created just fine. 一切都创造得很好。 However during gameplay I try "suspending" the squirrels physics body when a touch begins, then re-enable it when the touch ends (tempSquirrelBody is a SKPhysicsBody instance variable for the scene).
然而,在游戏过程中,我尝试在触摸开始时“暂停”松鼠物理体,然后在触摸结束时重新启用它(tempSquirrelBody是场景的SKPhysicsBody实例变量)。 Basically I'm trying to give the illusion that the squirrel is moving towards the user and over other objects while the user touches the screen.
基本上我试图给出一种错觉,即当用户触摸屏幕时,松鼠正朝着用户和其他物体移动。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
tempSquirrelBody = squirrel1.physicsBody;
squirrel1.physicsBody = nil;
CGPoint newPosition = squirrel1.position;
newPosition.x -= 60;
newPosition.y += 60;
squirrel1.position = newPosition;
squirrel1.texture = [SKTexture textureWithImageNamed:@"squirrelUp"];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch ends */
for (UITouch *touch in touches) {
CGPoint newPosition = squirrel1.position;
newPosition.x += 60;
newPosition.y -= 60;
squirrel1.position = newPosition;
squirrel1.texture = [SKTexture textureWithImageNamed:@"squirrelNormal"];
squirrel1.physicsBody = tempSquirrelBody;
}
}
This unfortunately is not working; 不幸的是,这不起作用; the physics body is not suspended and the squirrel collides with other objects when the physicsbody is supposed to be set to nil.
当物理体被设置为零时,物理体不会被悬浮,松鼠会与其他物体发生碰撞。 When I move the code from my SSSquirrel class to the game scene it works though.
当我将代码从我的SSSquirrel类移动到游戏场景时,它可以工作。 What am I doing incorrectly?
我做错了什么?
Thanks! 谢谢!
Wouldn't it be easier just to set collisionBitMask
to 0
instead of setting entire physicsBody
to nil
? 将
collisionBitMask
设置为0
而不是将整个physicsBody
设置为nil
会不会更容易?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
squirrel1.physicsBody.collisionBitMask = 0;
CGPoint newPosition = squirrel1.position;
newPosition.x -= 60;
newPosition.y += 60;
squirrel1.position = newPosition;
squirrel1.texture = [SKTexture textureWithImageNamed:@"squirrelUp"];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch ends */
for (UITouch *touch in touches) {
CGPoint newPosition = squirrel1.position;
newPosition.x += 60;
newPosition.y -= 60;
squirrel1.position = newPosition;
squirrel1.texture = [SKTexture textureWithImageNamed:@"squirrelNormal"];
squirrel1.physicsBody.collisionBitMask = 0xFFFFFFFF;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.