I am new to swift and just started learning recently.
Currently, I am having trouble with spawning random objects. One is top and the other is bottom and I want to choose between the two randomly and continue to repeat that action.
What I have so far is I can get the objects to scroll continuously from right to left and then go off the screen. But with my code, it will either spawn the top or the bottom object and then never the other one. It will repeat spawning the one it chooses first.
Not sure if I need to move it from the didMove(to view: Skview)
function to another one or if there is a better way to code this.
Below is the code I used to try and get it to repeat both of them one by one continuously.
This is the code in the override func didMove(to view: SKView)
:
let randomArray = [spawnAndDelayForeverUpObject, spawnAndDelayForeverDownProject]
self.run(randomArray.randomElement()!)
let randomObject = randomArray.randomElement()!
self.run(SKAction.repeatForever(randomObject))
I got to confused with my first project so I tried a new project and below is my code for it.`
I still can't understand how to randomly chose like every few seconds for either the red or the blue box to show up instead of both of them. I was on the apple developer website and found the randomization from the gameplay kit programming guide which I will read up onrandomization guide apple
class GameScene: 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
}
`
The issue is that you select an object and assign it to randomObject
but don't reassign it to a new value for the run
function. SKAction.repeatForever
call will repeatedly use the randomObject
with it's assigned value. You can resolve this issue by repeatedly running the same code. To do this you can move your code to a function and call that function repeatedly with a Timer. Or you can use one of the given answers here.