【问题标题】:How to make a spritekit node change from one image to another如何使 spritekit 节点从一个图像更改为另一个图像
【发布时间】:2015-06-11 06:37:07
【问题描述】:

我有一个带有代码的 SKSpriteNode 图像:

let Drake1 = SKSpriteNode(imageNamed: "Drake1")
    Drake1.position = CGPoint(x:self.frame.size.width/3, y:self.frame.size.height - 230)
    Drake1.zPosition = 2
    addChild(Drake1)

    //drake movement
    let moveRight = SKAction.moveByX(frame.size.width/2.8, y: 0, duration: 2)
    let moveLeft = SKAction.moveByX(-frame.size.width/2.8, y: 0, duration: 2)
    let moveBackAndForth = SKAction.repeatActionForever(SKAction.sequence([moveRight, moveLeft]))
    Drake1.runAction(moveBackAndForth)

我想做的是,当图像向右移动时,我想用不同的 SKSpriteNode 图像替换图像,当它向左移动时,我想使用原始图像,并一直重复.我正在为代码应该是什么而苦苦挣扎。

【问题讨论】:

    标签: swift skspritenode skaction


    【解决方案1】:

    SpriteKit 带有一个 SKAction,setTexture,可以相对轻松地即时更改精灵的纹理。您可以为每个图像创建一个内联 SKTexture 对象,在 SKActions 中使用它们,并将它们添加到您的序列循环中,如下所示:

    let moveRight = SKAction.moveByX(frame.size.width/2.8, y: 0, duration: 2)
    let moveLeft = SKAction.moveByX(-frame.size.width/2.8, y: 0, duration: 2)
    let texRight = SKAction.setTexture(SKTexture(imageNamed: "Drake1r"))
    let texLeft = SKAction.setTexture(SKTexture(imageNamed: "Drake1l"))
    let moveBackAndForth = SKAction.repeatActionForever(SKAction.sequence([texRight, moveRight, texLeft, moveLeft]))
    Drake1.runAction(moveBackAndForth)
    

    希望这对你有用!请注意,如果纹理大小不同,则必须将resize: Bool 添加到setTexture 的参数中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 2019-05-22
      相关资源
      最近更新 更多