Search code examples
iossprite-kitarc4random

How to add an arc4random when the score is increment?


I am making a game in which there are enemies of diferente colors, I want to do that if you get to a certain score a new enemy of different color is added to the game.

how can I add another color to my arc4random func when the score is 20?(20 for example)

class GameScene: SKScene, SKPhysicsContactDelegate {

var circuloPrincipal = SKSpriteNode(imageNamed: "circulo")

var enemigoTimer = NSTimer()

var hits = 0

var colorAmarillo: UIColor = UIColor(red: 0.9, green: 0.7, blue: 0.2, alpha: 0.9)

var colorAzul = UIColor(red: 0.1, green: 0.4, blue: 0.5, alpha: 1.0)

var colorVerde: UIColor = UIColor(red: 0.3, green: 0.7, blue: 0.5, alpha: 0.9)

var scoreLabel = SKLabelNode(fontNamed: "STHeitiJ-Medium")

 var score = 0

  //colorAmarillo = Yellow color
    //colorAzul = Blue color
    //colorVerde = Green color

func colisionPrincipal(enemigo: SKSpriteNode) {

//This is when the enemy makes contact with the player

        if hits < 3 && circuloPrincipal.color != enemigo.color{


            circuloPrincipal.runAction(SKAction.scaleBy(1.5, duration:0.5))




           enemigo.removeFromParent()

        hits++


            }

            if scoreLabel == "20" {


                //Here I want to add an enemy of another color


            }




        scoreLabel.removeAllActions()


        }else if circuloPrincipal.color == enemigo.color {

    //circuloPrincipal = player
    //enemigo = enemy
    //color = color


            enemigo.removeFromParent()

       score++

            scoreLabel.text = "\(score)"

        }

func enemigos() //This función is the arc4random to random the colors {

    let enemigo = SKSpriteNode(imageNamed: "circulo")
    enemigo.size = CGSize(width: 25, height: 25)
    enemigo.color = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
    enemigo.colorBlendFactor = 1.0
    enemigo.zPosition = 1.0

            enemigo.physicsBody = SKPhysicsBody(circleOfRadius: enemigo.size.height / 2)
    enemigo.physicsBody?.categoryBitMask = physicsCategory.enemigo
    enemigo.physicsBody?.contactTestBitMask = physicsCategory.circuloPrincipal
    enemigo.physicsBody?.collisionBitMask = physicsCategory.circuloPrincipal
    enemigo.physicsBody?.dynamic = true
   enemigo.physicsBody?.affectedByGravity = true


    enemigo.name = "enemigo"



    }

    let colorRandom = arc4random() % 3

    switch colorRandom {
    case 0:

        enemigo.color = colorAmarillo
        enemigo.colorBlendFactor = 1.0




        break

    case 1:

        enemigo.color = colorAzul
        enemigo.colorBlendFactor = 1.0


    break

    case 2:

        enemigo.color = colorVerde
        enemigo.colorBlendFactor = 1.0


        break
    default:


        break



    }

Solution

  • Your code and question is really messed up but maybe (just maybe) I will help you:)

    First add import statement

    import GameplayKit
    

    Next - generate random enemy color

    let colors = [SKColor.redColor(), SKColor.blueColor(), SKColor.greenColor()]
    
    let randomColorIndex = GKRandomDistribution(lowestValue: 0, highestValue: colors.count - 1).nextInt()
    
    // First solution using ternary conditional operator
    enemy.color = score == 20 ? SKColor.blackColor() : colors[randomColorIndex]
    
    // Second - using if-else clause
    if score == 20 {
        enemy.color = SKColor.blackColor()
    } else if score == 100 {
        enemy.color = SKColor.whiteColor()
    } else {
        enemy.color = colors[randomColorIndex]
    }