问题
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.
回答1:
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
回答2:
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.
回答3:
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
来源:https://stackoverflow.com/questions/21324728/sprite-kit-changing-zrotation-in-didbegincontact