我试图设置动画中单个SKTextures的持续时间。在armsAtlas纹理图集下面。我想将upTexture设置为持续1-4秒,然后将downTexture设置为持续0.3-1秒。如何为各个纹理设置这些持续时间范围?

let armsAtlas = SKTextureAtlas(named: "arms")
let downTexture = armsAtlas.textureNamed("down")
let upTexture = armsAtlas.textureNamed("up")

最佳答案

您可以使用SKAction序列,它是waitForDuration:withRange:method,如下所示:

//By default, the sprite is initialized with downTexture
let sprite = SKSpriteNode(texture: downTexture)
sprite.position = CGPoint(x: 200, y: 200)
addChild(sprite)
let sequence = SKAction.sequence([

    SKAction.runBlock({NSLog("Up texture set")}), //Added just for debugging to print a current time
    SKAction.setTexture(upTexture),
    SKAction.waitForDuration(2.5, withRange: 3.0),  //Wait between 1.0 and 4.0 seconds
    SKAction.runBlock({NSLog("Down texture set")}),
    SKAction.setTexture(downTexture),
    SKAction.waitForDuration(0.65, withRange: 0.7),//Wait between 0.3 and 1.0 seconds
    ])

let action = SKAction.repeatActionForever(sequence)

sprite.runAction(action, withKey: "aKey")

这段代码的作用是创建一个sprite,在默认情况下用downTexture初始化它,然后:
立即交换纹理到upTexture
等待1到4秒
将纹理交换为下纹理
等待0.3到1秒
永远重复这一切
如果要停止此操作,可以这样访问:
if sprite.actionForKey("aKey") != nil {
            removeActionForKey("aKey")
}

关于swift - 如何设置动画中单个SKTexture的持续时间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34995306/

10-10 21:07