ios - 基于 Swift SpriteKit 分数更改背景

标签 ios iphone swift sprite-kit

我正在尝试构建一个游戏(Swift Sprite Kit),其中场景的背景将在用户通过一定数量的分数后立即改变。 这不起作用(在 GameScene 中)

func addSwitchBackgrounds() {

         let pointsLable = MLPointsLabel(num: 0)

        if (pointsLable == 30) {

            let backgroundTexture = SKTexture (imageNamed: "Store")
            let backgroundImage =  SKSpriteNode (texture: backgroundTexture,size:view!.frame.size)
            backgroundImage.position = view!.center

        } 
}

有人可以帮忙吗?

最佳答案

这个简单的示例应该会为您指明正确的方法(只需复制并粘贴即可查看其工作原理)...

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    let backgroundNode = SKSpriteNode(imageNamed: "background")

    var score = 0

    let label = SKLabelNode(fontNamed: "ArialMT")

    override func didMoveToView(view: SKView) {
        //setup scene


        backgroundNode.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))

        label.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))

        label.text = "SCORE : 0"

        self.addChild(backgroundNode)

        self.addChild(label)

    }


    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {


        score++

        if(score == 5){

            self.backgroundNode.texture = SKTexture(imageNamed: "background_v2")



        }
         label.text = "SCORE : \(score)"

    }


}

首先,创建一个backgroundNode 属性,然后在代码中更改其纹理。您不必在更新方法中不断检查当前分数。仅当分数增加时您才能进行该检查。我不确定你在哪里调用 addSwitchBackgrounds,但只是想让你知道,不需要为此使用 update: 方法。

关于ios - 基于 Swift SpriteKit 分数更改背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31476354/

相关文章:

ios - Swift-将披露指示器添加到 UITableViewCell

ios - Swift-全屏图像与用户交互

ios - 将 viewcontroller 分配给 prepareForSegue 中的变量时的 Swift EXC_BREAKPOINT

objective-c - 如何将 subview 添加到 View

iphone - 将捏合手势从自定义 UIView 传递给父 UIScrollView

iphone - 无法加载从 Nib 引用的 "my-icon.png"图像 (iPhone)

ios - 如何在 UITableViewCell 中创建未知数量的 UILabels?

iphone - Xcode 无法检测到装有 iOS 6.1.4 的 iPhone 5 设备

iphone - 如何在iPhone SDK中添加NSDebug.h并使用NSZombie

ios - 如何在纵向模式下锁定 viewController