我有一个带有代码的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图像替换图像,当它向左移动时,我想使用原始图像,并永远重复此图像。我正在努力解决代码应该是什么。
答案 0 :(得分:2)
SpriteKit带有一个SKAction 1
,可以相对轻松地瞬间改变精灵的纹理。您可以为每个图像创建一个内联SKTexture对象,在SKActions中使用它们,然后将它们添加到序列循环中,如下所示:
setTexture
希望这适合你!请注意,如果纹理大小不同,则必须将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
的参数中。