更改所有SKTextures的SKTextureFilteringMode

时间:2017-03-14 07:20:38

标签: swift swift3 sprite-kit sktexture

有没有办法将filteringMode属性分配给SKTextureFilteringMode.nearest所有SKTextures?除了单独分配每个纹理。以下工作正常,但我宁愿我不必迭代纹理,但只是为过滤模式设置默认值。这可能吗?

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

2 个答案:

答案 0 :(得分:1)

在同一循环中创建纹理和设置属性

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

或只使用forEach

设置属性

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

答案 1 :(得分:1)

您可以对SKTexture进行扩展,如下所示:

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

然后你就会

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