I got this code from a web tutorial. Right now the code increase the spawn rate of the character once you reach a certain score in a game. But I want to add more characters once you reach a certain score in the game not just the spawn count. I have listed my code bellow.
import SpriteKit
var gameScore = 0
class GameScene: SKScene, SKPhysicsContactDelegate {
let scoreLabel = SKLabelNode(fontNamed: "Georgia")
var livesNumber = 5
let livesLabel = SKLabelNode(fontNamed: "Georgia")
var levelNumber = 0
let player = SKSpriteNode(imageNamed: "playerShip")
let bulletSound = SKAction.playSoundFileNamed("1.mp3", waitForCompletion: false)
let explosionSound = SKAction.playSoundFileNamed("P.wav", waitForCompletion: false)
let taptoStartLabel = SKLabelNode(fontNamed: "Georgia")
enum gameState{
case preGame
case ingame
case afteringame
case aftergame
}
var currentGameState = gameState.preGame
struct PhysicsCategories {
static let None: UInt32 = 0
static let Player: UInt32 = 0b1
static let Bullet: UInt32 = 0b10
static let Enemy : UInt32 = 0b100
static let Bullet1: UInt32 = 0b1000}
var CurrentGameState = gameState.ingame
func random() -> CGFloat{
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)}
func random(min min: CGFloat, max: CGFloat) -> CGFloat {
return random() * (max - min) + min}
var gameArea: CGRect
override init(size: CGSize) {
let maxAspectRatio: CGFloat = 16.0/9.0
let playablewidth = size.height / maxAspectRatio
let margin = (size.width - playablewidth) / 2
gameArea = CGRect(x: margin, y: 0, width: playablewidth, height: size.height)
super.init(size: size)}
required init?(coder aDecoder: NSCoder){
fatalError("init(Coder:) has not been implemented")}
override func didMoveToView(view: SKView) {
gameScore = 0
self.physicsWorld.contactDelegate = self
for i in 0...1{
let background = SKSpriteNode(imageNamed: "background")
background.size = self.size
background.anchorPoint = CGPoint(x: 0.5, y: 0)
background.position = CGPoint(x: self.size.width/2, y: self.size.height*CGFloat(i))
background.zPosition = 0
background.name = "Background"
self.addChild(background)}
player.setScale(1)
player.position = CGPoint(x: self.size.width/2, y: self.size.height/33222)
player.zPosition = 2
player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size)
player.physicsBody!.affectedByGravity = false
player.physicsBody!.categoryBitMask = PhysicsCategories.Player
player.physicsBody!.collisionBitMask = PhysicsCategories.None
player.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy
self.addChild(player)
scoreLabel.text = " Score: 0"
scoreLabel.fontSize = 70
scoreLabel.fontColor = SKColor.whiteColor()
scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left
scoreLabel.position = CGPoint(x: self.size.width*0.15, y: self.size.height + scoreLabel.frame.size.height)
scoreLabel.zPosition = 100
self.addChild(scoreLabel)
livesLabel.text = "LIVES: 5"
livesLabel.fontSize = 70
livesLabel.fontColor = SKColor.whiteColor()
livesLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Right
livesLabel.position = CGPoint(x: self.size.width*0.85, y: self.size.height + livesLabel.frame.size.height)
livesLabel.zPosition = 100
self.addChild(livesLabel)
let moveontoscreenaction = SKAction.moveToY(self.size.height*0.9, duration: 0.3)
scoreLabel.runAction(moveontoscreenaction)
livesLabel.runAction(moveontoscreenaction)
taptoStartLabel.text = "Tap To Begin"
taptoStartLabel.fontSize = 100
taptoStartLabel.fontColor = SKColor.whiteColor()
taptoStartLabel.zPosition = 1
taptoStartLabel.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
taptoStartLabel.alpha = 0
self.addChild(taptoStartLabel)
let fadeinaction = SKAction.fadeInWithDuration(0.3)
taptoStartLabel.runAction(fadeinaction)}
var lastupdatetime: NSTimeInterval = 0
var deltaFrameTime: NSTimeInterval = 0
var amountomove: CGFloat = 600.00
override func update(currentTime: NSTimeInterval) {
if lastupdatetime == 0{
lastupdatetime = currentTime}
else{
deltaFrameTime = currentTime - lastupdatetime
lastupdatetime = currentTime}
let amounttomovetobackground = amountomove * CGFloat(deltaFrameTime)
self.enumerateChildNodesWithName("Background"){
background, stop in
if self.currentGameState == gameState.ingame{
background.position.y -= amounttomovetobackground}
if background.position.y < -self.size.height{
background.position.y += self.size.height*2}}}
func startGame(){
currentGameState = gameState.ingame
let doa = SKAction.fadeOutWithDuration(0.5)
let da = SKAction.removeFromParent()
let ds = SKAction.sequence([doa, da])
taptoStartLabel.runAction(ds)
let msos = SKAction.moveToY(self.size.height*0.2, duration: 0.5)
let sla = SKAction.runBlock(startNewLevel)
let startgameseq = SKAction.sequence([msos, sla])
player.runAction(startgameseq)}
func loseAlife(){
livesNumber -= 1
livesLabel.text = "LIves: \(livesNumber)"
let scaleUp = SKAction.scaleTo(1.5, duration: 0.2)
let scaleDown = SKAction.scaleTo(1, duration: 0.2)
let scaleSequence = SKAction.sequence([scaleUp, scaleDown])
livesLabel.runAction(scaleSequence)
if livesNumber == 0 {
rungameover()}}
func addScore(){
gameScore += 1
scoreLabel.text = "Score: \(gameScore)"
if gameScore == 10 || gameScore == 25 || gameScore == 50 {
startNewLevel()}}
func rungameover(){
CurrentGameState = gameState.aftergame
self.removeAllActions()
self.enumerateChildNodesWithName("Bullet"){
bullet, stop in
bullet.removeAllActions()}
self.enumerateChildNodesWithName("Enemy"){
enemy, stop in
enemy.removeAllActions()}
let changeSceneAction = SKAction.runBlock(changeScene)
let waitTochangeScence = SKAction.waitForDuration(1)
let changescencesequence = SKAction.sequence([waitTochangeScence,changeSceneAction])
self.runAction(changescencesequence)}
func changeScene(){
let sceneToMoveTo = gameoverScene(size: self.size)
sceneToMoveTo.scaleMode = self.scaleMode
let mytransition = SKTransition.fadeWithDuration(0.5)
self.view!.presentScene(sceneToMoveTo,transition: mytransition)}
func didBeginContact(contact: SKPhysicsContact) {
var body1 = SKPhysicsBody()
var body2 = SKPhysicsBody()
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask{
body1 = contact.bodyA
body2 = contact.bodyB}
else{
body1 = contact.bodyB
body2 = contact.bodyA}
if body1.categoryBitMask == PhysicsCategories.Player && body2.categoryBitMask == PhysicsCategories.Enemy{
if body1.node != nil{
spawnExplosioin(body1.node!.position)}
if body2.node != nil{
spawnExplosioin(body2.node!.position)}
body1.node?.removeFromParent()
body2.node?.removeFromParent()
rungameover()}
if body1.categoryBitMask == PhysicsCategories.Bullet && body2.categoryBitMask == PhysicsCategories.Enemy && body2.node?.position.y < self.size.height{
addScore()
if body2.node != nil {
spawnExplosioin(body2.node!.position)}
body1.node?.removeFromParent()
body2.node?.removeFromParent()}}
func spawnExplosioin (spawnPosition : CGPoint){
let explosion = SKSpriteNode(imageNamed: "explosion")
explosion.position = spawnPosition
explosion.zPosition = 3
self.addChild(explosion)
let scaleIn = SKAction.scaleTo(1, duration: 0.1)
let fadeOut = SKAction.fadeOutWithDuration(0.1)
let delete = SKAction.removeFromParent()
let explosionSequence = SKAction.sequence([explosionSound,scaleIn,fadeOut])
explosion.runAction(explosionSequence)}
func startNewLevel(){
levelNumber += 1
if self.actionForKey("spawningEnemies") != nil{
self.removeActionForKey("spawningEnemies")}
var levelDuration = NSTimeInterval()
switch levelNumber{
case 1: levelDuration = 1.2
case 2: levelDuration = 1
case 3: levelDuration = 0.8
case 4: levelDuration = 0.5
default:
levelDuration = 0.5
print("cannot find level info")}
let spawn = SKAction.runBlock(spawnEnemy)
let waitToSpawn = SKAction.waitForDuration(levelDuration)
let spawnSequence = SKAction.sequence([waitToSpawn, spawn])
let spawnForever = SKAction.repeatActionForever(spawnSequence)
self.runAction(spawnForever)}
func fireBullet(){
let bullet = SKSpriteNode(imageNamed: "bullet")
bullet.setScale(1)
bullet.position = player.position
bullet.zPosition = 1
bullet.physicsBody = SKPhysicsBody(rectangleOfSize: bullet.size)
bullet.physicsBody!.affectedByGravity = false
bullet.physicsBody!.categoryBitMask = PhysicsCategories.Bullet
bullet.physicsBody!.collisionBitMask = PhysicsCategories.None
bullet.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy
self.addChild(bullet)
let moveBUllet = SKAction.moveToY(self.size.height + bullet.size.height, duration: 0.3)
let deletebullet = SKAction.removeFromParent()
let bulletesequence = SKAction.sequence([bulletSound,moveBUllet,deletebullet])
bullet.runAction(bulletesequence)}
func fireBullet1(){
let bullet = SKSpriteNode(imageNamed: "bullet")
bullet.setScale(1)
bullet.position = player.position
bullet.zPosition = 1
bullet.physicsBody = SKPhysicsBody(rectangleOfSize: bullet.size)
bullet.physicsBody!.affectedByGravity = false
bullet.physicsBody!.categoryBitMask = PhysicsCategories.Bullet
bullet.physicsBody!.collisionBitMask = PhysicsCategories.None
bullet.physicsBody!.contactTestBitMask = PhysicsCategories.Enemy
self.addChild(bullet)
let moveBUllet = SKAction.moveToY(self.size.height + bullet.size.height, duration: 0.3)
let deletebullet = SKAction.removeFromParent()
let bulletesequence = SKAction.sequence([bulletSound,moveBUllet,deletebullet])
bullet.runAction(bulletesequence)}
func spawnEnemy(){
let randomXstart = random(min: CGRectGetMinX(gameArea), max: CGRectGetMaxX(gameArea))
let randomxend = random(min: CGRectGetMinX(gameArea), max: CGRectGetMaxX(gameArea))
let startPoint = CGPoint(x: randomXstart, y: self.size.height * 1.2)
let endpoint = CGPoint(x: randomxend, y: -self.size.height * 0.2)
let enemy = SKSpriteNode(imageNamed: "enemyShip")
enemy.name = "Enemy"
enemy.setScale(1)
enemy.position = startPoint
enemy.zPosition = 2
enemy.physicsBody = SKPhysicsBody(rectangleOfSize: enemy.size)
enemy.physicsBody!.affectedByGravity = false
enemy.physicsBody!.categoryBitMask = PhysicsCategories.Enemy
enemy.physicsBody!.collisionBitMask = PhysicsCategories.None
enemy.physicsBody!.contactTestBitMask = PhysicsCategories.Player | PhysicsCategories.Bullet
self.addChild(enemy)
let moveEnemy = SKAction.moveTo(endpoint, duration: 5)
let deleteEnemy = SKAction.removeFromParent()
let LoseAlifeAction = SKAction.runBlock(loseAlife)
let enemySequence = SKAction.sequence([moveEnemy, deleteEnemy, LoseAlifeAction])
if CurrentGameState == gameState.ingame{
enemy.runAction(enemySequence)}
let dx = endpoint.x - startPoint.x
let dy = endpoint.y - startPoint.y
let amountToRotate = atan2(dy,dx)
enemy.zRotation = amountToRotate}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if currentGameState == gameState.preGame{
startGame()}
if CurrentGameState == gameState.ingame{
fireBullet()}}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches{
let pointOfTouch = touch.locationInNode(self)
let previouspointoftouch = touch.previousLocationInNode(self)
let amountDragged = pointOfTouch.x - previouspointoftouch.x
if CurrentGameState == gameState.ingame{
player.position.x += amountDragged}
if player.position.x > CGRectGetMaxX(gameArea) - player.size.width/2{
player.position.x = CGRectGetMaxX(gameArea) - player.size.width/2}
if player.position.x < CGRectGetMinX(gameArea) + player.size.width/2{
player.position.x = CGRectGetMaxX(gameArea) + player.size.width/2}}}}