如何连续重复一系列动作中的一个动作
How do I repeat an action from an array of actions continuously
我是swift新手,最近才开始学习。
目前,我在生成随机对象时遇到问题。一个在上,一个在下,我想在两者之间随机选择,然后继续重复那个动作。
到目前为止,我可以让对象从右向左连续滚动,然后离开屏幕。但是使用我的代码,它会生成顶部或底部对象,然后永远不会生成另一个对象。它会重复生成它首先选择的那个。
不确定我是否需要将它从 didMove(to view: Skview)
函数移至另一个函数,或者是否有更好的编码方法。
下面是我用来尝试让它连续地一一重复的代码。
这是override func didMove(to view: SKView)
中的代码:
let randomArray = [spawnAndDelayForeverUpObject, spawnAndDelayForeverDownProject]
self.run(randomArray.randomElement()!)
let randomObject = randomArray.randomElement()!
self.run(SKAction.repeatForever(randomObject))
我对我的第一个项目感到困惑,所以我尝试了一个新项目,下面是我的代码。`
我仍然不明白如何每隔几秒随机选择红色或蓝色框出现,而不是同时出现。我在苹果开发者网站上发现了游戏套件编程指南中的随机化,我将在 randomization guide apple
上阅读该指南
class 游戏场景:SKScene,SKPhysicsContactDelegate {
let blueBox = SKSpriteNode(color: UIColor.blue, size: CGSize(width: 50, height: 50))
let redBox = SKSpriteNode(color: UIColor.red, size: CGSize(width: 25, height: 25))
override func didMove(to view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: -9.0)
physicsBody = SKPhysicsBody.init(edgeLoopFrom: frame)
run(SKAction.repeatForever(SKAction.sequence([SKAction.run(spawnRedBox), SKAction.wait(forDuration: (5)), SKAction.run(removeRedBox)])))
run(SKAction.repeatForever(SKAction.sequence([SKAction.run(spawnBlueBox), SKAction.wait(forDuration: (5)), SKAction.run(removeBlueBox)])))
}
func removeBlueBox() {
blueBox.removeFromParent()
}
func removeRedBox() {
redBox.removeFromParent()
}
func spawnBlueBox() {
print("spawnBlueBoxFuncCalled")
blueBox.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: blueBox.size.width, height: blueBox.size.height))
blueBox.physicsBody?.affectedByGravity = false
blueBox.physicsBody?.isDynamic = true
let posTwo = SKAction.move(to: CGPoint(x: frame.minX + 50, y: frame.midY + 100), duration: 0.0)
let Position = posTwo
let move = SKAction.move(by: CGVector(dx: frame.size.width, dy: 0), duration: 5.0)
let moveRemoveMoveBack = SKAction.sequence([Position, move])
let repeatMoving = SKAction.repeatForever(moveRemoveMoveBack)
blueBox.run(repeatMoving)
self.addChild(blueBox)
}
func spawnRedBox() {
print("spawnRedBoxFuncCalled")
redBox.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: redBox.size.width, height: redBox.size.height))
redBox.physicsBody?.affectedByGravity = false
redBox.physicsBody?.isDynamic = true
let posOne = SKAction.move(to: CGPoint(x: frame.minX + 50, y: frame.midY), duration: 0.0)
let Position = posOne
let move = SKAction.move(by: CGVector(dx: frame.size.width, dy: 0), duration: 5.0)
let moveRemoveMoveBack = SKAction.sequence([Position, move])
let repeatMoving = SKAction.repeatForever(moveRemoveMoveBack)
redBox.run(repeatMoving)
self.addChild(redBox)
//Array example
}
`
问题是您 select 一个对象并将其分配给 randomObject
但没有将其重新分配给 run
函数的新值。 SKAction.repeatForever
调用将重复使用 randomObject
及其分配的值。您可以通过重复 运行 相同的代码来解决此问题。为此,您可以将代码移动到一个函数中,然后使用 Timer 重复调用该函数。或者您可以使用给定的答案之一 here.
我是swift新手,最近才开始学习。
目前,我在生成随机对象时遇到问题。一个在上,一个在下,我想在两者之间随机选择,然后继续重复那个动作。
到目前为止,我可以让对象从右向左连续滚动,然后离开屏幕。但是使用我的代码,它会生成顶部或底部对象,然后永远不会生成另一个对象。它会重复生成它首先选择的那个。
不确定我是否需要将它从 didMove(to view: Skview)
函数移至另一个函数,或者是否有更好的编码方法。
下面是我用来尝试让它连续地一一重复的代码。
这是override func didMove(to view: SKView)
中的代码:
let randomArray = [spawnAndDelayForeverUpObject, spawnAndDelayForeverDownProject]
self.run(randomArray.randomElement()!)
let randomObject = randomArray.randomElement()!
self.run(SKAction.repeatForever(randomObject))
我对我的第一个项目感到困惑,所以我尝试了一个新项目,下面是我的代码。`
我仍然不明白如何每隔几秒随机选择红色或蓝色框出现,而不是同时出现。我在苹果开发者网站上发现了游戏套件编程指南中的随机化,我将在 randomization guide apple
上阅读该指南class 游戏场景:SKScene,SKPhysicsContactDelegate {
let blueBox = SKSpriteNode(color: UIColor.blue, size: CGSize(width: 50, height: 50))
let redBox = SKSpriteNode(color: UIColor.red, size: CGSize(width: 25, height: 25))
override func didMove(to view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: -9.0)
physicsBody = SKPhysicsBody.init(edgeLoopFrom: frame)
run(SKAction.repeatForever(SKAction.sequence([SKAction.run(spawnRedBox), SKAction.wait(forDuration: (5)), SKAction.run(removeRedBox)])))
run(SKAction.repeatForever(SKAction.sequence([SKAction.run(spawnBlueBox), SKAction.wait(forDuration: (5)), SKAction.run(removeBlueBox)])))
}
func removeBlueBox() {
blueBox.removeFromParent()
}
func removeRedBox() {
redBox.removeFromParent()
}
func spawnBlueBox() {
print("spawnBlueBoxFuncCalled")
blueBox.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: blueBox.size.width, height: blueBox.size.height))
blueBox.physicsBody?.affectedByGravity = false
blueBox.physicsBody?.isDynamic = true
let posTwo = SKAction.move(to: CGPoint(x: frame.minX + 50, y: frame.midY + 100), duration: 0.0)
let Position = posTwo
let move = SKAction.move(by: CGVector(dx: frame.size.width, dy: 0), duration: 5.0)
let moveRemoveMoveBack = SKAction.sequence([Position, move])
let repeatMoving = SKAction.repeatForever(moveRemoveMoveBack)
blueBox.run(repeatMoving)
self.addChild(blueBox)
}
func spawnRedBox() {
print("spawnRedBoxFuncCalled")
redBox.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: redBox.size.width, height: redBox.size.height))
redBox.physicsBody?.affectedByGravity = false
redBox.physicsBody?.isDynamic = true
let posOne = SKAction.move(to: CGPoint(x: frame.minX + 50, y: frame.midY), duration: 0.0)
let Position = posOne
let move = SKAction.move(by: CGVector(dx: frame.size.width, dy: 0), duration: 5.0)
let moveRemoveMoveBack = SKAction.sequence([Position, move])
let repeatMoving = SKAction.repeatForever(moveRemoveMoveBack)
redBox.run(repeatMoving)
self.addChild(redBox)
//Array example
}
`
问题是您 select 一个对象并将其分配给 randomObject
但没有将其重新分配给 run
函数的新值。 SKAction.repeatForever
调用将重复使用 randomObject
及其分配的值。您可以通过重复 运行 相同的代码来解决此问题。为此,您可以将代码移动到一个函数中,然后使用 Timer 重复调用该函数。或者您可以使用给定的答案之一 here.