【问题标题】:sprite kit limit SKAction animate textures to a fixed timesprite kit 将 SKAction 动画纹理限制为固定时间
【发布时间】:2014-08-16 21:45:26
【问题描述】:

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

向左移动的动画是一系列反复显示的图像,因此它应该无休止地重复,但是当角色停止向左移动时。它应该更改为 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")

【问题讨论】:

    标签: ios iphone animation sprite-kit skaction


    【解决方案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")
    

    【讨论】:

    • ScaleTo 使精灵上下颠倒,但与 ScaleXTo 相同的代码工作得很好。谢谢! :)
    • 你是对的。我使用默认的飞船图像来测试代码。我将编辑我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多