Extend SKAction to override timingMode

本小妞迷上赌 提交于 2019-12-12 05:19:46

问题


I have many SKActions in a SpriteKit project. The default timingMode for SKActions is "linear". Is it possible to use an extension to override this timingMode default to e.g. "easeInEaseOut" so ALL SKActions have timingMode = easeInEaseOut?

I have tried various "extension" styles but none will compile - normally returning "'timingMode' used within its own type" or "Initializer 'init()' with Objective-C selector 'init' conflicts with implicit initializer 'init()' with the same Objective-C selector"

The docs don't seem to give any examples of this, but surely this would be a useful thing to be able to do? Especially when you have hundreds of SKActions in your game?


回答1:


Pick your poison, one extends the action to allow you to quickly call .easeInEaseOut timing mode, the other extends SKNode to allow you to run using a specific timing mode.

There is no way to change default behavior, the only other way is to create your own static methods for every action that exists, which can become cumbersome.

extension SKAction
{
    //e.g. SKAction.move(to: CGPoint.zero, duration: 10).easeInEaseOut()
    func easeInEaseOut() -> SKAction
    {

        self.timingMode = .easeInEaseOut
        return self
    }


}
extension SKNode
{
    func runWithEaseInEaseOut(action:SKAction,withKey key: String = "")
    {
        action.timingMode = .easeInEaseOut
        if key != ""
        {
            self.run(action,withKey:key)
        }
        else
        {
            self.run(action)
        }
    }

}


来源:https://stackoverflow.com/questions/44713043/extend-skaction-to-override-timingmode

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