Original: Swift language Combat Promotion-9th chapter game-Parkour Panda-3 show a dynamic panda
A static panda obviously does not satisfy our desire, next we let the panda run up. The principle of sequence frame animation is to constantly switch between different pictures. When we switch the panda of a one-piece, the panda runs. So first we need an array constant to store the running animation arts, and a variable to record the current state of the panda's movement.
Let runframes = [Sktexture] ()
Action state, the default value is the run in the enumeration
var status = Status.run
We then get all the arts and sciences in the Runframes by the for loop for running animations. Because we used the Sktextureatlas. So we can through the Texturenamed method, through the name of the picture, it is convenient to obtain the arts and sciences in the collection of specific arts and sciences.
for i=1 ; i<=runAtlas.textureNames.count ; i++ {
let tempName = String(format: "panda_run_%.2d", i)
let runTexture = runAtlas.textureNamed(tempName)
if runTexture {
runFrames.append(runTexture)
}
}
Attention:
String (format: "Panda_run_%.2d", I) This is the formatting of the string. 2d represents a 2-bit integer. Then this formatted string is panda_run_01, panda_run_02, panda_run_03 look.
Now we need a way to run this action, this method name we named run. The code is as follows.
Func run(){
/ / Remove all actions
self.removeAllActions()
/ / Set the current action state to run
Self.status = .run
/ / Set the logical array of running through SKAction.animateWithTextures to an animation that switches once in 0.05 seconds
// SKAction.repeatActionForever will make the animation execute forever
// self.runAction performs an action to form an animation
self.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(runFrames, timePerFrame: 0.05)))
}
Then we're going to run this method of executing the default execute run in the construct.
Run ()
Then the complete code in the Panda class should be like this
Import SpriteKit
Enum Status:Int{
Case run=1,jump,jump2,roll;
}
Class Panda : SKSpriteNode {
Let runAtlas = SKTextureAtlas(named: "run.atlas")
Let runFrames = [SKTexture]()
Var status = Status.run
Init(){
Let texture = runAtlas.textureNamed("panda_run_01")
Let size = texture.size()
Super.init(texture:texture,color:SKColor.whiteColor(),size:size)
Var i:Int
For i=1 ; i<=runAtlas.textureNames.count ; i++ {
Let tempName = String(format: "panda_run_%.2d", i)
Let runTexture = runAtlas.textureNamed(tempName)
If runTexture {
runFrames.append(runTexture)
}
}
Run()
}
Func run(){
/ / Remove all actions
self.removeAllActions()
/ / Set the current action state to run
Self.status = .run
/ / Set the logical array of running through SKAction.animateWithTextures to an animation that switches once in 0.05 seconds
// SKAction.repeatActionForever will make the animation execute forever
// self.runAction performs an action to form an animation
self.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(runFrames, timePerFrame: 0.05)))
}
Let's run the code and we can see that the pandas are running unsteadily in the scene.
Attention:
Self.status =. Run this is a shorthand for self.status = Status.run because we have previously given the enumeration value to the status variable, so we can omit the status later
My public number.
I wrote a book: "Swift language Combat Promotion" http://item.jd.com/11641501.html
Swift language Combat Promotion-9th chapter game-Parkour Panda-3 show a dynamic panda