标签:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
import SpriteKit class Cat : SKSpriteNode { //神经猫的纹理集 let dbAtlas = SKTextureAtlas (named: "db.atlas" ) //神经猫的纹理数组 var dbFrames = [ SKTexture ]() override init () { //获取纹理集的第一个纹理 let texture = dbAtlas.textureNamed( "db_01" ) //获取纹理的尺寸 let size = texture.size() //执行父类的构造方法 super . init (texture:texture,color: SKColor .whiteColor(),size:size) var i: Int var tempName: String //从纹理集中获取纹理加入数组 for i=1 ; i <= dbAtlas.textureNames.count ; i++ { //%.2d表示两位整数。下面格式化完毕后便是db_01,db_02,db_03等 tempName = String (format: "db_%.2d" ,i) let dbTexture = dbAtlas.textureNamed(tempName) dbFrames.append( dbTexture ) } //设置中心点 self .anchorPoint = CGPointMake (0.5, 0.2) //播放动画 db() } required init (coder aDecoder: NSCoder ) { fatalError( "init(coder:) has not been implemented" ) } func db(){ //循环播放纹理动画 self .runAction( SKAction .repeatActionForever( SKAction .animateWithTextures(dbFrames, timePerFrame: 0.2))) } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import SpriteKit class GameScene : SKScene { //利用lazy关键字可以让实例化在第一次使用时才执行 lazy var cat = Cat () //当切换到这个场景视图后 override func didMoveToView(view: SKView ) { cat.position = CGPoint (x: CGRectGetMidX ( self .frame)-10, y: CGRectGetMidY ( self .frame)-150); cat.zPosition = 20 self .addChild(cat) } //响应屏幕点击的方法 override func touchesBegan(touches: NSSet , withEvent event: UIEvent ) { } } |
Swift - 使用atlas图集实现动画效果(SpriteKit游戏开发)
标签:
原文地址:http://www.cnblogs.com/Free-Thinker/p/4841094.html