gpt4 book ai didi

swift - 如何使用 SKAction 改变 SKSpriteNode 的位置

转载 作者:行者123 更新时间:2023-11-28 06:12:26 25 4
gpt4 key购买 nike

while word != ""{

if word[word.startIndex] == "R" {

let moveSpriteToRight = SKAction.move(to: CGPoint(x: sprite.position.x+128, y: sprite.position.y), duration: 1)

sprite.run(moveSpriteToRight)

} else if word == "D" {

let moveSpriteToDown = SKAction.move(to: CGPoint(x: sprite.position.x, y: sprite.position.y-128), duration: 1)

sprite.run(moveSpriteToDown)

} else if word == "L" {

let moveSpriteToLeft = SKAction.move(to: CGPoint(x: sprite.position.x-128, y: sprite.position.y), duration: 1)

sprite.run(moveSpriteToLeft)

} else if word == "U" {

let moveSpriteToUp = SKAction.move(to: CGPoint(x: sprite.position.x, y: sprite.position.y+128), duration: 1)

sprite.run(moveSpriteToUp)
}

word.remove(at: word.startIndex)
}

这是我代码的一部分。我不能多次使用 SKAction。例如,如果单词 = "ULD",那么 Sprite 应该沿着向上 (U)、然后向左 (L) 和向下 (D) 的路径移动。每次用户通过手势创建新的轨迹时,记录手势的终止都会通过 touchesEnded 发生。雪碧他们必须每次执行。但是我在模拟器中的 Sprite 在这个例子和所有的例子中改变了位置。

最佳答案

您可以将您的操作放入一个数组中,并在完成所有检查后按顺序运行它们

let actions = [SKAction]()

while word != ""{

if word[word.startIndex] == "R" {
actions.append(SKAction.move(to: CGPoint(x: sprite.position.x+128, y: sprite.position.y), duration: 1))
}

if word == "D" {
actions.append(SKAction.move(to: CGPoint(x: sprite.position.x, y: sprite.position.y-128), duration: 1))
}

if word == "L" {
actions.append(SKAction.move(to: CGPoint(x: sprite.position.x-128, y: sprite.position.y), duration: 1))
}

if word == "U" {
actions.append(SKAction.move(to: CGPoint(x: sprite.position.x, y: sprite.position.y+128), duration: 1))
}

word.remove(at: word.startIndex)
}

let sequenceOfActions = SKAction.sequence([actions])
sprite.run(sequenceOfActions)

关于swift - 如何使用 SKAction 改变 SKSpriteNode 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46160117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com