简体   繁体   中英

Issue with physics bodies when subclassing SKSpriteNode?

I am having an issue while subclassing 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). 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. What am I doing incorrectly?

Thanks!

Wouldn't it be easier just to set collisionBitMask to 0 instead of setting entire physicsBody to 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;

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM