behind the aura behind the aura - 4 months ago 11
Swift Question

Spawning different nodes randomly

I'm currently in the process of creating a new game that will feature two different balls/bubbles (a player and a enemy). I'm trying to get both of these to spawn randomly at different times kinda like this (ex. player, enemy, player, player, etc.(not in any order)) but random as the game goes on and after the player restarts the game. I'm having trouble trying to find a solution. I've tried a switch case but only either the player or enemy will spawn per game. Does anyone have a solution? If any other code is needed, I will provide it.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */


if (gamestarted == false) {

gamestarted = true


/*Player and Enemy random spawner*/
ballSwitchCase()



/*Tap to start*/
tts.removeFromParent()


} else {
/*removed unnecessary code for question*/

}


func mainBallSpawner() {

/*Main ball*/

let spawn = SKAction.runBlock({

()in

self.creatingTheBall()

})

let delay = SKAction.waitForDuration(3.0)
let spawnDelay = SKAction.sequence([spawn, delay])
let spawnDelayForever = SKAction.repeatActionForever(spawnDelay)
self.runAction(spawnDelayForever, withKey: "spawnDelayForever")

let distance = CGFloat(self.frame.height + 170 + gameBall.frame.width)
let moveBalls = SKAction.moveToY(-distance, duration: NSTimeInterval(0.008 * distance))
let removeBalls = SKAction.removeFromParent()
moveAndRemoveBalls = SKAction.sequence([moveBalls, removeBalls])


}


func enemySpawner() {

/*Enemy*/

let spawnEnemy = SKAction.runBlock({

()in

self.creatingEnemyBall()

})

let delayEnemy = SKAction.waitForDuration(3.0)
let spawnDelayEnemy = SKAction.sequence([spawnEnemy, delayEnemy])
let spawnDelayEnemyForever = SKAction.repeatActionForever(spawnDelayEnemy)
self.runAction(spawnDelayEnemyForever, withKey: "spawnDelayEnemyForever")

let enemyDistance = CGFloat(self.frame.height + 170 + enemyBall.frame.width)
let moveEnemy = SKAction.moveToY(-enemyDistance, duration: NSTimeInterval(0.008 * enemyDistance))
let removeEnemy = SKAction.removeFromParent()
moveAndRemoveEnemy = SKAction.sequence([moveEnemy, removeEnemy])


}


func ballSwitchCase() {


let spawnRandomBall = arc4random_uniform(2)

switch spawnRandomBall {
case 0:
mainBallSpawner()
break
case 1:
enemySpawner()
break
default:
break
}

}

Answer

Your code can be much simpler

import SpriteKit

class GameScene: SKScene {

    private var gameStarted = false

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        guard !gameStarted else { return }
        gameStarted = true
        beginCreatingSprites()
    }

    private func beginCreatingSprites() {

        let addSomething = SKAction.runBlock { [weak self] in
            if arc4random_uniform(2) == 0 {
                self?.addBall()
            } else {
                self?.addEnemy()
            }
        }
        let wait = SKAction.waitForDuration(3)
        let sequence = SKAction.sequence([addSomething, wait])
        let repeatForever = SKAction.repeatActionForever(sequence)
        self.runAction(repeatForever)
    }

    private func addBall() {
        let ball = SKSpriteNode(imageNamed: "ball")
        // set position etc...
        self.addChild(ball)
    }


    private func addEnemy() {
        let enemy = SKSpriteNode(imageNamed: "enemy")
        // set position etc...
        self.addChild(enemy)
    }
}