如何更改SKAction序列中节点的纹理图集?

时间:2015-06-21 00:00:02

标签: swift sprite-kit skaction sktexture sktextureatlas

我正在使用SpriteKit进行游戏,我有一个节点,使用此SKAction序列重复从屏幕的左侧到右侧来回移动:

func move(){
    let recursive = SKAction.sequence([
        SKAction.moveByX(frame.size.width/2.8, y: 0, duration: NSTimeInterval(4),
        SKAction.moveByX(-frame.size.width/2.8, y: 0, duration: NSTimeInterval(4),
        SKAction.runBlock({self.move()})])
        doc.runAction(recursive, withKey: "move")
} 

节点是SKTexture,我正在使用名为WalkRight的图集。我想要做的是,当SKAction.moveByX(-frame.size.width/2.8, y: 0, duration: NSTimeInterval(4)运行时,我想将节点的纹理更改为我称为WalkLeft的不同地图集。我无法弄清楚如何在我的序列中运行一个动作,让我更改节点的纹理。

我添加纹理的代码:

 //In class
var docWalkingFrames: [SKTexture]!

 // general runAction method to make doc walk.
func walkingDoc() {
    doc.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(docWalkingFrames,timePerFrame: 0.3,resize: false,restore: true)),withKey:"walkingInPlaceDoc")
}

//In didMoveToView
  //doc animation walk right
    let docAnimatedAtlas = SKTextureAtlas(named: "WalkRight")
    var walkFrames = [SKTexture]()
    let numImages = docAnimatedAtlas.textureNames.count
    for var i=1; i<=numImages; i++ {
        let docTextureName = "docRight\(i)"
        walkFrames.append(docAnimatedAtlas.textureNamed(docTextureName))
    }

    docWalkingFrames = walkFrames

    let firstFrame = docWalkingFrames[0]
    doc = SKSpriteNode(texture: firstFrame)
    doc.position = CGPoint(x:self.frame.size.width/3.1, y:self.frame.size.height/2)
    doc.size = CGSize(width: 220, height: 470)
    doc.zPosition = 2
    addChild(doc)

walkingDoc()

0 个答案:

没有答案