如何在分数增加时添加arc4random?

时间:2016-06-21 03:35:26

标签: ios sprite-kit arc4random

我正在制作一个游戏,其中有不同颜色的敌人,我想这样做,如果你得到一定的分数,不同颜色的新敌人被添加到游戏中。

当分数为20时,如何为arc4random func添加另一种颜色?(例如20)

类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){

//这是敌人与玩家联系的时候

        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()//这个函数是随机颜色的arc4random           {

    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



    }

1 个答案:

答案 0 :(得分:0)

您的代码和问题确实搞砸了,但也许(只是也许)我会帮助您:)

首先添加导入语句

import GameplayKit

下一步 - 生成随机敌人颜色

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]
}