如何在swift

时间:2016-08-02 00:20:15

标签: swift animation sprite-kit textures

我创建一个游戏,我正在尝试在角色上实现运行动画。动画由30帧以30fps运行组成。动画工作正常但是我在行走时无法循环播放动画,我创建了一个与英雄运动相关联的按钮但是当按下按钮时当前的编码动画只会循环第一帧和按钮是发布完整动画播放。我试图实现SKwaitForduration操作,但这也没有用,现在我很难过。任何帮助都是很好的帮助大声笑我是动画片的新手

这是我的士兵腿类

 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()}
}
}

1 个答案:

答案 0 :(得分:2)

由于多次通话,似乎您的动画卡住了。 每次有多个代表时,您都可以检查动画:

例如:

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
}