Override public variable for class in Swift and SpriteKit for use in XCode ActionEditor

I'm trying to make a little platform game, for educational purposes only. I tried to animate a character with an SKTextures array as follows:

let animation: SKAction = SKAction.animate(
    with: frames.map {
        let texture : SKTexture = SKTexture(imageNamed: $0)
        texture.filteringMode = SKTextureFilteringMode.nearest

        return texture
    },
    timePerFrame: 0.15
)

framesvariable is an array of SKTextures. I used mapto set the filtering mode for each texture. This works fine and dandy, but I found that I can create animations (actions) in ActionEditor in Xcode with the AnimateWithTextures action. And here is my problem. This is much more efficient, but I cannot change the filtering mode of textures in animations in ActionEditor . I looked at the SKTexture class and saw this:

open class SKTexture : NSObject, NSCopying, NSCoding {
    ...
    /**
    The filtering mode the texture should use when not drawn at native size. Defaults to SKTextureFilteringLinear.
    */
    open var filteringMode: SKTextureFilteringMode
    ...
}

, open , , , . filteringMode SKTextureFilteringMode.nearest SKTextures, ActionEditor , .

, , - , .

+4
3

SKTexture init, node.

extension SKTexture { // Declare this extension outside your class decleration
convenience init(customImageNamed: String) {
    self.init(imageNamed: customImageNamed)
    self.filteringMode = .nearest
}
}

func setupAnimationWithPrefix(_ prefix: String, start: Int, end: Int, timePerFrame: TimeInterval) -> SKAction{
    var textures = [SKTexture]()
    for i in start...end{
        textures.append(SKTexture(customImageNamed: "\(prefix)\(i)"))
    }
    return SKAction.animate(with: textures, timePerFrame: timePerFrame)
}

10 image1, image2, image3 ect.. then

func runAction(){
    let action = setupAnimationWithPrefix("image", start: 1, end: 10, timePerFrame: 0.1) 
    yourNode.run(action)
} // you can change timePerFrame to your liking i find 0.1 sec per frame the best
+2

, , -

class MyTexture: SKTexture {

:

  override var filteringMode: SKTextureFilteringMode {
    get {
      return .nearest
    }
    set {
    }
  }

, , - ( hooplah).

, , SKTexture , .

, - convenience init Arie , :

func nearestTexture(imageNamed name: String) -> SKTexture {
   let newTex = SKTexture(imageNamed: name)
   newTex.filteringMode = .nearest
   return newTex
}

let texture: SKTexture = nearestTexture(imageNamed: "lol")
+1

ActionEditor is still very limited in functions, you will have to write some code to enable texture filtering. If you do not plan to scale your sprites (note that I said the sprite, not the scene, these are two different behaviors), then I would not even worry about filtering textures, the time difference would be insignificant.

0
source

Source: https://habr.com/ru/post/1678516/


All Articles