Sprite Kit: changing zRotation in DidBeginContact

元气小坏坏 提交于 2019-12-11 11:38:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!