swiftswift3sprite-kitsktexture

Change SKTextureFilteringMode for all SKTextures


Is there a way to assign the filteringMode attribute to SKTextureFilteringMode.nearest for ALL SKTextures? Other than assigning to each texture individually. The following works fine, but I'd rather that I didn't have to iterate over the textures, but just set a default for the filtering mode. Is this possible?

func walk () -> SKAction {

    let walkTexture1 = SKTexture(imageNamed: "walk1.png")
    let walkTexture2 = SKTexture(imageNamed: "walk2.png")
    let walkTexture3 = SKTexture(imageNamed: "walk3.png")
    let walkTexture4 = SKTexture(imageNamed: "walk4.png")
    let walkTexture5 = SKTexture(imageNamed: "walk5.png")


    let animationTextures: [SKTexture] = [walkTexture1, walkTexture2, walkTexture3, walkTexture4, walkTexture5]

    for texture in animationTextures {
        texture.filteringMode = SKTextureFilteringMode.nearest
    }

    let walkAnimation = SKAction.animate(with: animationTextures, timePerFrame: 0.3/5)

    return walkAnimation

Solution

  • Create textures and set properties in the same loop

    let textures = (1...5).map { let texture = SKTexture(imageNamed: "walk\($0).png") texture.filteringMode = SKTextureFilteringMode.nearest return texture }

    or just set properties using forEach

    textures.forEach { $0.filteringMode = .nearest }