精灵套件限制SKAction将纹理设置为固定时间

时间:2014-08-16 21:45:26

标签: ios iphone animation sprite-kit skaction

我有一个向左移动的精灵,角色的动画向左移动。然后它等待并开始向右移动,动画向右移动。

向左移动的动画是一系列反复显示的图像,所以它应该无休止地重复,但是当角色停止向左移动时。它应该更改为animateRight并无休止地一遍又一遍地显示这些图像。

我不太清楚如何做到这一点。

        // move left, then stop, then turn, move right, and repeat
    let moveLeft = SKAction.moveToX(leftPosition, duration: NSTimeInterval(length * 3))
    let wait = SKAction.waitForDuration(0.3)
    let moveRight = SKAction.moveToX(xPos, duration: NSTimeInterval(length * 3))

    // the animation of moving left should repeat (same series of images is shown over and over)
    // but when moveLeft is done after a certain time is should stop
    // it should maybe not be repeat action forever, but repeat for "duration: NSTimeInterval(length * 3)) 
    // like move left, but can't find that method.. 
    let animateLeft = SKAction.repeatActionForever(animationMovingLeft)
    let animateRight = SKAction.repeatActionForever(animationMovingRight)

    let groupActionsLeft = SKAction.group([animateLeft, moveLeft])
    let groupActionsRight = SKAction.group([animateRight, moveRight])

    // problem here, the groupActionsLeft runs forever, the animateLeft should stop at the same time
    // as moveLeft
    let sequence = SKAction.sequence([groupActionsLeft, wait, groupActionsRight, wait])

    let repeatSequence = SKAction.repeatActionForever(sequence)

    //sprite.runAction(animate, withKey: "snailAnimation")
    sprite.runAction(repeatSequence, withKey: "snailMovement")

1 个答案:

答案 0 :(得分:1)

以下动画仅在一个方向上(在这种情况下为左)并水平翻转精灵(通过将精灵的xScale设置为-1),因此当向右移动时它会向另一个方向动画。

// move left, then stop, then turn, move right, and repeat
let moveLeft = SKAction.moveToX(leftPosition, duration: NSTimeInterval(length * 3))
let wait = SKAction.waitForDuration(0.3)
let moveRight = SKAction.moveToX(xPos, duration: NSTimeInterval(length * 3))

let faceLeft = SKAction.scaleXTo(1.0, duration: 0)

// Setting the xScale to -1 will flip sprite horizontally
let faceRight = SKAction.scaleXTo(-1.0, duration: 0)

// like move left, but can't find that method.. 
let animateLeft = SKAction.repeatActionForever(animationMovingLeft)

// Animate in one direction only
sprite.runAction(animateLeft)

let groupActionsLeft = SKAction.group([faceLeft, moveLeft])
let groupActionsRight = SKAction.group([faceRight, moveRight])

let sequence = SKAction.sequence([groupActionsLeft, wait, groupActionsRight, wait])

let repeatSequence = SKAction.repeatActionForever(sequence)

//sprite.runAction(animate, withKey: "snailAnimation")
sprite.runAction(repeatSequence, withKey: "snailMovement")