2017-07-08 101 views
1

我有我的快捷动画麻烦。动画不播放。SpriteKit动画问题斯威夫特

这些都是我的快捷文件:

这是我的精灵文件:

import Foundation 
import SpriteKit 

class Dog: SKSpriteNode { 

var dog = SKSpriteNode() 

var textureAtlas = SKTextureAtlas() 
var textureArray = [SKTexture]() 

init() { 
    super.init(texture: SKTexture(imageNamed:"dog_1"), color: UIColor.clear, size: CGSize(width: 44, height: 25)) 
    textureAtlas = SKTextureAtlas(named: "dog") 

    for i in 1...textureAtlas.textureNames.count { 

     let name = "dog_\(i).png" 
     textureArray.append(SKTexture(imageNamed: name)) 

    } 

} 

func animate() { 
    dog.run(SKAction.repeatForever(SKAction.animate(with: textureArray, timePerFrame: 0.1))) 
} 

func stopAnimation() { 
    dog.removeAllActions() 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 
} 

这是我GameScene文件:

import SpriteKit 
import GameplayKit 

class GameScene: SKScene { 

var dog: Dog! 

override func didMove(to view: SKView) { 
    scene?.backgroundColor = UIColor(red: 132.0/255.0, green: 179.0/255.0, blue: 255.0/255.0, alpha: 1.0) 

    addDog() 

} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    dog.animate() 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    dog.stopAnimation() 
} 

func addDog() { 

    dog = Dog() 
    dog.position = CGPoint(x: frame.size.width/2, y: frame.size.height/2) 
    addChild(dog) 
} 

} 

我认为这个问题是在动画功能精灵文件。

我已经试过所有我能想到的。任何帮助将不胜感激。

+0

查看是否甚至执行循环。将一个断点在它...此外,从图像中删除名字巴纽扩展 - '让NAME =“_狗\(我)”' – Whirlwind

回答

2

您的课程你有一个变种被初始化为SKSpriteNode。你的启动和停止动画功能,然后在该,而不是类本身,而不是其他在你的游戏场景进行操作。由于永远不会被添加到场景中,您的动画永远不可见。

从狗取下狗var和对它的引用 - 动画应该在你的子类犬直接运行。

+0

哦,是的。从类Dog的实例var狗永远不会被添加到节点树...很好地发现:) – Whirlwind

+0

它的工作!感谢所有的帮助 – Grandmango227