1
votes

I am trying to mirror my SKSpriteNode using SKAction *mirrorDirection = [SKAction scaleXTo:-1 y:1 duration:0.0]; but every time I do, the sprite's physics body seems to get messed up and it drops off the screen. I have several objects (floors) beneath the sprite and it falls through all of them as if they are not there.

This only happens when I mirror the sprite. Anyone know how to fix this?

Update: Reseting the physics body as one answer suggested unfortunately did not fix the problem. It appears that only the contact part of the physics body malfunctions. Gravity still has an effect as the little guy drops like a rock.

I also tried to reset by again self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.size.width, self.size.height)];

right after the self.xScale = -1; but this also did not fix the issue.

5
Have you tried adding your mirrored node as a child of a regular SKNode and setting the physics body on the SKNode instead of the negatively scaled node?nacross

5 Answers

2
votes

Add your mirror-able node as a child of some regular SKNode and set the physics body on this parent node instead of the negatively scaled node.

0
votes

I don't know how to fix this but I would like to suggest one way you could create a mirrored sprite.

To achieve this set the x or y scale of your sprite node to -1. Then use the SKView method textureFromNode: to create a texture from this mirrored node.

You can then use this texture to create a new sprite node that is mirrored but doesn't require any negative scaling.

0
votes

Don't use an SKAction, just set it directly to the SKSpriteNode's xScale property.

self.yourSprite.xScale = -1;
0
votes

Roecrew is right about setting the xScale property directly. I would suggest you try this:

node.xScale = -1;
node.physicsBody = node.physicsBody;

You will need to 'reset' the physicsBody each time you change the xScale property.

The xScale issue with physicsBody is a bug in SpriteKit, but I was able to 'retain' the physicsBody using the second line.

0
votes

I'm meeting the problem exactly like yours. I spent about 2 hours to figure it out.

Just init your physicsBody after you scaleX, i dont know why but i had correct this issue by doing this way.

walkRight   = [SKAction sequence:@[resetDirection,[SKAction runBlock:^{
            [self changePhysicsDirectionRight];
        }],[SKAction repeatActionForever: walk]]];
walkLeft   = [SKAction sequence:@[mirrorDirection,[SKAction runBlock:^{
            [self changePhysicsDirectionLeft];
        }],[SKAction repeatActionForever: walk]]];

walkRight and walkLeft is my action when changing direction, and resetDirection and mirrorDirection is exactly the action i used to scaleXTo:1 and scaleXTo:1

So after i scaleXTo i use a method call changePhysicsDirectionRight to re-init my physicBody like

- (void)changePhysicsDirectionRight{
    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.size.width,self.size.height)];
    self.physicsBody.categoryBitMask = guyCategory;
    self.physicsBody.contactTestBitMask = 0;
    self.physicsBody.collisionBitMask   = 0;
}

Remember to re-assign all your category and everything like you init before.

I hope someone pro at spritekit can tell me the reason why ....