我找不到一个简单的解决方案,每个例子都只显示非常复杂的解决方案,但我只想要2-3张图片循环,以便看起来像是动画。与动画Gif相同的效果。目前我有这个来创建一张图片
MonsterNode = SKSpriteNode(imageNamed: "MonsterNode_GameScene")
但我怎样才能将MonsterNode变量设置为这种类型的动画呢?我真的希望只需最少的代码就能实现这一点。
我找不到一个简单的解决方案,每个例子都只显示非常复杂的解决方案,但我只想要2-3张图片循环,以便看起来像是动画。与动画Gif相同的效果。目前我有这个来创建一张图片
MonsterNode = SKSpriteNode(imageNamed: "MonsterNode_GameScene")
但我怎样才能将MonsterNode变量设置为这种类型的动画呢?我真的希望只需最少的代码就能实现这一点。
animateWithTextures
来完成这个任务。您需要设置精灵需要动画的所有帧及每帧显示的时间。然后使用 repeatActionForever
运行动画循环。// Add 3 frames
let f0 = SKTexture.init(imageNamed: "MonsterNode_GameScene_0")
let f1 = SKTexture.init(imageNamed: "MonsterNode_GameScene_1")
let f2 = SKTexture.init(imageNamed: "MonsterNode_GameScene_2")
let frames: [SKTexture] = [f0, f1, f2]
// Load the first frame as initialization
monsterNode = SKSpriteNode(imageNamed: "MonsterNode_GameScene_0")
monsterNode.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
// Change the frame per 0.2 sec
let animation = SKAction.animateWithTextures(frames, timePerFrame: 0.2)
monsterNode.runAction(SKAction.repeatActionForever(animation))
self.addChild(monsterNode)
我遇到了相同的大红叉问题。 所以,我没有在代码中定义monsterNode。而是通过从atlas文件夹中拖动动画的第一张图像,在sks屏幕上创建了monstor。然后在属性部分为它指定了一个名称:monsterNode。以下是代码:
var runAnimation = [SKTexture]()
override func didMove(to view: SKView) {
let runAtlas = SKTextureAtlas(named: "run")
for index in 1...runAtlas.textureNames.count{
let textureName = String.init(format: "run%1d", index)
runAnimation.append(SKTexture(imageNamed: textureName))
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let monsterNode = self.childNode(withName: "monsterNode")
if(monsterNode != nil){
let animation = SKAction.animate(with: runAnimation, timePerFrame: 0.1)
monsterNode?.run(SKAction.repeatForever(animation))
}
}