Swift - 如何在获得一定数量的点时更改背景颜色

时间:2015-06-18 17:54:09

标签: ios swift sprite-kit

我一直在开发一款游戏,当玩家获得10分后,背景会变成另一种颜色,我将其放入字符串中,同样会出现20分,30分等等。我的问题是当玩家获得超过10点/ 20点/ 30点时,如何使背景淡入不同的颜色。我不希望颜色是随机的,因为我想要放置我自己的颜色代码/十六进制值,我也不希望按下按钮时颜色会改变。我只是希望它在玩家获得超过一定数量的积分时改变。

一个很好的例子就是游戏" Don't Touch The Spikes"你获得背景的每5个点会变成另一个。

注意:我在GameScene中制作了游戏而没有使用GameViewController,因为我将项目制作成游戏文件所以:

DateTime

1 个答案:

答案 0 :(得分:0)

我认为您无法动画SKView.First声明变量

var isStarted = false
var isGameOver = false
var lastChangedBackground = 0
var background = SKSpriteNode()

要做你想做的事,你可以添加一个SKNode作为你的SKScene的孩子,让它成为背景,并在didMoveToView()函数中创建一个变量lastChangedBackground。 lastChangedBackground会让背景在达到某个点值时只改变一次颜色。

然后你可以添加这样的更新功能:

override func didMoveToView(view: SKView) {
    //backgroundColor = UIColor.greenColor()
    //backgroundColor = SKSpriteNode() 
    var background = SKSpriteNode(color: UIColor(red: 223/255.0, green: 86/255.0, blue: 94/255.0, alpha: 1.0),size: view.bounds.size)
    background.position = view.center
    self.addChild(background)

    addMovingGround()
    addHero()
    addWallGen()
    addTapToStartLabel()
    addStageLabel()
    addPointsLabels()
    addPhysicsWorld()
    loadHighscore()
}

override func update(currentTime: CFTimeInterval) {

    if wallGen.wallTrackers.count > 0 {

        let wall = wallGen.wallTrackers[0] as PPWall

        let wallLocation = wallGen.convertPoint(wall.position, toNode: self)
        if wallLocation.x < hero.position.x {
            wallGen.wallTrackers.removeAtIndex(0)

            let pointsLabel = childNodeWithName("pointsLabel") as! PPPointsLabel
            pointsLabel.increment()

            //Here points should receive pointsLabel value
            //if pointsLabel.num % 10 == 0 then points is 10, 20, 30...
            if pointsLabel.num % 10 == 0 && pointsLabel.num != lastChangedBackground{
                lastChangedBackground = points
                if (lastChangedBackground == 10) {
                    background.runAction(SKAction.colorizeWithColor(SKColor.blueColor(), colorBlendFactor: 0.5, duration: 1))
                }
                else if(lastChangedBackground == 20){
                    background.runAction(SKAction.colorizeWithColor(SKColor.greenColor(), colorBlendFactor: 0.5, duration: 1))
                }
                else if(lastChangedBackground == 30){
                    background.runAction(SKAction.colorizeWithColor(SKColor.redColor(), colorBlendFactor: 0.5, duration: 1))
                }
                for wall in wallGen.wallTrackers {
                    wall.runAction(SKAction.colorizeWithColor(background.color, colorBlendFactor: 0.5, duration: 1))
                }

            }
        }
    }else if

     wallGen.wallTrackers.count > 0 {

        let wall = wallGen.wallTrackers[0] as PPWall

        let wallLocation = wallGen.convertPoint(wall.position, toNode: self)
        if wallLocation.x < hero.position.x {
            wallGen.wallTrackers.removeAtIndex(0)

            let stageLabel = childNodeWithName("stageLabel") as! PPStageLabel
            stageLabel.increment()
        }
    }
}