我正在尝试制作小型平台游戏,只是为了学习目的。我尝试用这样的一系列Sktextures来动画角色:
let animation: SKAction = SKAction.animate(
with: frames.map {
let texture : SKTexture = SKTexture(imageNamed: $0)
texture.filteringMode = SKTextureFilteringMode.nearest
return texture
},
timePerFrame: 0.15
)
frames
变量是sktextures的数组。我使用map
为每个纹理设置过滤模式。这效果很好且花花公子,但是我发现我可以在XCode中使用AnimateWithTextures Action中的Actioneditor创建动画(Action(。这是我的问题。这要高得多,但是我无法更改Animation 中纹理的过滤模式。我研究了Sktexture课程,并看到了:
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
的默认值设置为所有Sktextures的SKTextureFilteringMode.nearest
另外,如果您知道如何在特定动作中设置纹理的过滤模式 - 我也想知道如何做。谢谢
您可以创建一个函数来为您设置动画,并使用自定义init创建一个扩展名来进行Sktexture,当您准备就绪时,只需将其调用到您的节点。
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的图像。
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
不幸的是,您不能在不划分的情况下覆盖变量,即做
之类的事情class MyTexture: SKTexture {
从那里,您可以覆盖变量:
override var filteringMode: SKTextureFilteringMode {
get {
return .nearest
}
set {
}
}
应该有效的,但是现在您无法更改过滤模式(不经过一堆Hooplah(。
因此,更恰当的方法是通过initializer设置默认值,但是SKTexture
没有公共指定的初始化器,因此该选项不在窗口中。
因此,您最好的选择将是Arie的convenience init
扩展,或者只是制作一个返回新纹理的普通功能:
func nearestTexture(imageNamed name: String) -> SKTexture {
let newTex = SKTexture(imageNamed: name)
newTex.filteringMode = .nearest
return newTex
}
let texture: SKTexture = nearestTexture(imageNamed: "lol")
Actioneditor在功能上仍然非常有限,您将不得不在后面编写某种代码以进行纹理过滤。如果您不打算缩放精灵(请注意,我说Sprite,不是场景,这是两种不同的行为(,那么我什至不用担心纹理过滤,时间差会忽略不计。