Flipped x-scale breaks collision handling (SpriteKit 7.1)

前端 未结 3 1879
感情败类
感情败类 2020-12-16 03:49

I use SKNode\'s xScale property to flip my sprites horizontally. Now, after updating iOS to version 7.1 horizontal flip causes my objects to sink inside the ground. (See ani

3条回答
  •  轮回少年
    2020-12-16 04:35

    I'm convinced this is a bug in SpriteKit.

    Anyway, here is one solution for the problem (Actually, this is more a workaround than a real solution but...): Wrap the sprite in a container node. Also, container node holds the physicsBody while the child node is merely a graphics node. This way you can safely flip the sprite using xScale without affecting the physics of the node.

    // Init
    {
        SKSpriteNode* turtleSprite = [SKSpriteNode spriteNodeWithImageNamed:@"turtle.png"];
        self.turtleSprite = turtleSprite;
    
        SKNode* turtleWrapper = [SKNode node]; 
        turtleWrapper.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:turtleSprite.size.width/2];
        turtleWrapper.physicsBody.categoryBitMask = 2;
        turtleWrapper.physicsBody.collisionBitMask = 1;
        turtleWrapper.physicsBody.contactTestBitMask = 1;
    
        [turtleWrapper addChild:turtleSprite];
        [self.world addChild:turtleWrapper];
    }
    
    // Elsewhere
    {
        self.turtleSprite.xScale *= -1;
    }
    

提交回复
热议问题