
2D
图形和
Sprite Kit
|
207
的小数,然后用乘以偏移位置。最终,这个数字会被乘以
0
,换句话说,位置又回
到了中心点位置。
6.18
动画精灵
问题
我们想用一系列位图构成一个
Sprite Kit
动画。例如,我们有一个能够“自动运行”
的动画,然后让精灵播放这个动画。
解决方案
在这个答案中,我们假设已经制作了所有分离的动画帧,并将它们都放到
Animation.atlas
文件夹中,然后将这个文件夹添加到项目中。
通过
SKAction
的
animate (with:, timePerFrame:)
方
法,我们可以用动画方式播
放一个精灵集:
//
加载包含有动画帧的纹理集
let atlas = SKTextureAtlas(named: "Animation")
//
读取所有的动画帧贴图的名称并进行排序
let textureNames = atlas.textureNames.sorted {
(first, second) -> Bool in
return first < second
}
//
加载所有动画帧
var allTextures : [SKTexture] = textureNames.map { (textureName) -> SKTexture in
return atlas.textureNamed(textureName)
}
//
创建动画精灵,设置它的动画帧,
//
放到屏幕的中央
let animatedSprite = SKSpriteNode(texture:allTextures[0]) ...