简体   繁体   中英

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

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 }

You could perhaps make an extension to SKTexture, something like this:

extension SKTexture {
    class func pixeled(imageNamed imageName: String) -> SKTexture {
        let texture = SKTexture(imageNamed: imageName)
        texture.filteringMode = .nearest
        return texture
    }
}

Then you'll simply have

let walkTexture1 = SKTexture.pixeled(imageNamed: "walk1.png")

etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM