Strange SpriteKit + UIDynamics Bug

 ̄綄美尐妖づ 提交于 2019-11-29 11:43:58

Ok I found a pretty hacky solution. All you need to do is use the UIDynamics framework in some way before presenting your SKScene. For example, simply running this one line of code before presenting your scene will remove the weird physics bug caused by simultaneously using SpriteKit and UIDynamics at the same time:

UIDynamicBehavior* a = [[UIDynamicBehavior alloc] init];

I'm assuming internally this prepares the physics engine in such a way that sprite kit physics will behave normally. Unfortunately I do not trust this solution because it might not work one day and my entire game would suffer because of it.

So as an alternative to the UIDynamics framework I've decided to just use UIView Animation to replicate some of the interesting physics effects that I got with the UIDynamics Framework.

EDIT FOR IOS 8 According to doctordoder, this bug still exists in iOS 8, and this workaround still works.

Note that in iOS 9: the @Epic Byte's solution still do the half workaround.

It starts the physics engine earlier (before we initialize SpriteKit game etc.) but unfortunately it mixes it up.

And allocation and init the UIDynamicAnimator during the SpriteKit game will cause the physicsWorld and it's properties like gravity act differently.

Also it seems like the physicsWorld is removed for a short time (switching engines?) during UIDynamicAnimator initialization.

Conclusion:

  • Alloc and init UIDynamicAnimator asap and handle your physics world differently e.g. call

    [UIDynamicAnimator new]; in delegate's didFinishLaunchingWithOptions

or

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