SKAction.animation - 框架顺序

时间:2018-03-26 07:46:34

标签: ios swift sprite-kit

А导致意外的问题:

let jaba = SKSpriteNode (imageNamed: "jaba0")
let jabaAnimationThrow: SKAction

for i in 0...1 {
        texturesJaba.append(SKTexture(imageNamed: "jaba\(i)"))
    }
    jabaAnimationThrow = SKAction.animate(with: texturesJaba,
                                         timePerFrame: 0.30)
func enemyThrow(enemy: SKSpriteNode) { 
    let enemyRun = (SKAction.repeatForever((SKAction.sequence([SKAction.wait(forDuration: 3, withRange: 2),jabaAnimationThrow]))))
enemy.run(enemyRun)} 
enemyThrow(enemy: jaba)

问题:SCAstion.animation播放第一帧1“jaba1”,然后帧0“jaba0”!为什么呢?

谢谢!

1 个答案:

答案 0 :(得分:1)

我编译了你的代码,它运行得很好。以下是步骤:

  1. 在你的GameScene.swift文件中设置jaba播放器如下

    override func didMove(to view: SKView) {
        let jaba = SKSpriteNode (imageNamed: "jaba0")
        jaba.position = CGPoint(x: 100, y: 200) // set your desired position
        jaba.setScale(0.2) // whatever scale you might need
        self.addChild(jaba)
        }
    
  2. 之后在GameScene.swift文件中生成enemyThrow(敌人:SKSpriteNode)函数,如下所示

    func enemyThrow(enemy: SKSpriteNode) {
            var texturesJaba = [SKTexture]()
            let jabaAnimationThrow: SKAction
            for i in 0...1 {
            texturesJaba.append(SKTexture(imageNamed: "jaba\(i)"))
            }
            jabaAnimationThrow = SKAction.animate(with: texturesJaba,
                                          timePerFrame: 0.30)
            let enemyRun =  (SKAction.repeatForever((SKAction.sequence([SKAction.wait(forDuration: 3, withRange: 2),jabaAnimationThrow]))))
    enemy.run(enemyRun)
    
            }
    
  3. 最后,在didMove(toView :)函数中,在self.addchild(jaba)之后调用enemyThrow(enemy :)函数。代码应如下所示:

     override func didMove(to view: SKView) {
        let jaba = SKSpriteNode (imageNamed: "jaba0")
        jaba.position = CGPoint(x: 100, y: 200) // set your desired position
        jaba.setScale(0.2) // whatever scale you might need
        self.addChild(jaba)
    
        // call the function enemyThrow here
        enemyThrow(enemy: jaba)
        }