1
votes
SKSpriteNode *_node;

- (void)didBeginContact:(SKPhysicsContact *)contact
{
     _node.alpha = 0.5; //this works
     _node.zRotation = 0.5; // this doesn't work
}

Any ideas why zRotation doesn't work? I tried doing NSLog and it gives the right zRotation but the screen is not showing.

3
What do you mean "the screen is not showing"? What do you expect to get?Andrey Gordeev
I expect to see _node do a rotation but it doesn't rotate.Josh Lee
May be zRotation is already set to 0.5?Andrey Gordeev

3 Answers

1
votes

Have a look at what the runloop looks like for SpriteKit. Nothing is drawn until after physics are simulated. So even though you are setting the zRotation manually it is probably overridden by the physicsBody and set to its own value. Try putting an NSLog into the -didSimulatePhysics method to get the zRotation before render. I would try moving your zRotation setter to -didSimulatePhysics.

The order of operations is

-update:
action evaluation
-didEvaluateActions
run physics
-didSimulatePhysics
render the scene

Reference: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Actions/Actions.html

0
votes

Another idea that may work: setup the lever as a contact object so you'll get a callback when the lever is hit. Get the position of the object colliding with the lever. If the object is colliding on the wrong side/direction then try setting level.physicsBody.allowsRotation = NO; otherwise set it to yes. You could also try forcing the zRotation here if that doesn't work.

0
votes

I had a similar issue, when setting zRotation and position for SKSpriteNode with physicsBody didn't have any effect, while with physicsBody set to nil everything was ok. As joshd supposed physicsBody's simulation somehow overrides these values after they are set manually. So a workaround for me was to remove body temporarily, then orient and position the sprite, then recreate physics body from scratch or from a stored property

storedPhysicsBody = physicsBody
physicsBody = nil
position = newPosition
zRotation = newZRotation
physicsBody = storedPhysicsBody