Reputation: 189
Im creating a game and i am trying to implement a run animation on a character. The animation consists of 30 frames being ran at 30fps. the animation works fine however I'm having trouble looping the animation when walking, i have created a button linked with the hero movement however with the current coding when the button is held down the animation only loops the first frame and when the button is release the full animation is played. I have tried to implement a SKwaitForduration action but that did not work either and now I'm pretty stumped. Any help is good help lol I'm very new to animation
here is my soldierLegs class
import Foundation
import SpriteKit
class AssassinLegs: SKSpriteNode {
let soldierLegs = SKSpriteNode(imageNamed: "RZ-AssassinLegs.png")
var runAction:SKAction?
var isJumping:Bool = false
var isFalling:Bool = false
var isRunning:Bool = true
var isAttacking:Bool = false
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init (imageNamed:String) {
let imageTexture = SKTexture(imageNamed: imageNamed)
super.init(texture: imageTexture, color:SKColor.clearColor(), size:
imageTexture.size() )
self.setUpRun()
}
func setUpRun() {
let atlas = SKTextureAtlas (named: "RzAssassinLegs")
var array = [String]()
//or setup an array with exactly the sequential frames start from 1
//was
// for var i=1; i <= 12; i++ {
for i in 1 ... 30 {
let nameString = String(format: "RzAssassinRun%i", i)
array.append(nameString)
}
//create another array this time with SKTexture as the type
(textures being the .png images)
var atlasTextures:[SKTexture] = []
//was...
//for (var i = 0; i < array.count; i++ ) {
for i in 0 ..< array.count {
let texture:SKTexture = atlas.textureNamed( array[i] )
atlasTextures.insert(texture, atIndex:i)
}
let atlasAnimation = SKAction.animateWithTextures(atlasTextures,
timePerFrame: 1.0/30, resize: true , restore:false )
runAction = SKAction.repeatActionForever(atlasAnimation)
// runAction = SKAction
}
func Run (){
isFalling = false
isRunning = true
isJumping = false
let runAnimationFinish = SKAction.waitForDuration(1)
let runAnimationSequence = SKAction.sequence([runAction!,
runAnimationFinish])
self.runAction(runAnimationSequence)
}
class GameScene: SKScene, SKPhysicsContactDelegate {
func RightArrow () {
rightArrow.position = CGPointMake(101, 33)
rightArrow.size = CGSize (width: 50, height: 50)
rightArrow.xScale = 1
rightArrow.yScale = 1
rightArrow.zPosition = 1
self.addChild(rightArrow)
}
func heroMovementRight () {
soldierLegs.Run()
soldierLegs.position = CGPointMake(soldierLegs.position.x + 4.0,
soldierLegs.position.y)
soldierTorso.position = CGPointMake(soldierTorso.position.x + 4.0,
soldierTorso.position.y)
}
override func touchesBegan(touches: Set<UITouch>, withEvent
event:UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
if stickActive == false{ fireWeapon = false}
if node == jumpArrow { if (canJump) {heroJumpMovement() } }
if node == leftArrow {goLeft = true}
if node == rightArrow {goRight = true}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event:
UIEvent?) {
goLeft = false
goRight = false
fireWeapon = false
stickActive = false
}
override func update(currentTime: CFTimeInterval) {
if goLeft {heroMovementLeft()}
if goRight {heroMovementRight()}
if heroJump {heroJumpMovement()}
}
}
Upvotes: 1
Views: 477
Reputation: 35392
Seems your animation stuck due to multiple calls. You could check your animations everytime there are multiple reps:
For example:
if !self.actionForKeyIsRunning("rotate") {
let rotate = SKAction.rotateToAngle(newAngle, duration: 0.2,shortestUnitArc: true)
self.runAction(rotate,withKey: "rotate")
}
func actionForKeyIsRunning(key: String) -> Bool {
return self.actionForKey(key) != nil ? true : false
}
Upvotes: 2