Swift变量没有保持值或以某种方式重置自己

时间:2018-02-17 01:59:25

标签: ios swift

我试图在一个游戏中保持得分,该游戏具有与不同事物相关联的不同点值。用户必须触摸游戏中的内容才能获得积分。但是,得分没有准确计算。

我已确定将游戏中每个元素正确存储在名为ballValue的变量中的分数。变量currentScore用于保存分数,然后我使用SKLabelNode显示currentScore的值。

currentScore初始化为0,并在用户触球时使用当前值加ballValue进行更新。

更新以显示代码和上下文

class ChallengeScene: SKScene, GKGameCenterControllerDelegate {

    // MARK: Ball Data
    var randomBallType : Int = 0
    var ballValue : Int = 0

    // MARK: HUD
    var scoreLabel = SKLabelNode(fontNamed: "Impact")
    var timeLabel = SKLabelNode(fontNamed: "Impact")

    var currentScore : Int = 0 {
        didSet {
            self.scoreLabel.text = "\(self.currentScore)"
            GameHandler.sharedInstance.score = currentScore
        }
    }

    // MARK: Initial Setup
    func createHUD() {
    ... UNRELATED HUD STUFF...
        currentScore = 0
    }

    // MARK: Gameplay Setup
    func createBall(forTrack track: Int) {
        setupTracks()

        randomBallType = GKRandomSource.sharedRandom().nextInt(upperBound: 9)

        switch randomBallType {
        case 0:
            player = SKSpriteNode(imageNamed: "blueOne")
            ballSpeed = 0.008
            ballValue = 1
        case 1:
            player = SKSpriteNode(imageNamed: "minusThree")
            ballSpeed = 0.008
            ballValue = -3
        case 2:
            player = SKSpriteNode(imageNamed: "minusFive")
            ballSpeed = 0.008
            ballValue = -5
        case 3:
            player = SKSpriteNode(imageNamed: "purpleThree")
            ballSpeed = 0.008
            ballValue = 3
        case 4:
            player = SKSpriteNode(imageNamed: "greenFive")
            ballSpeed = 0.008
            ballValue = 5
        case 5:
            player = SKSpriteNode(imageNamed: "blackTen")
            ballSpeed = 0.008
            ballValue = 10
        default:
            player = SKSpriteNode(imageNamed: "blueOne")
            ballSpeed = 0.008
            ballValue = 1
        }

        player?.name = "BALL"
        player?.size = CGSize(width: 100, height: 100)
        player?.physicsBody?.linearDamping = 0

        ...UNRELATED BALL STUFF...

        self.addChild(player!)
    }

    // MARK: Overrides
    override func didMove(to view: SKView) {

        createHUD()
        launchGameTimer()

        self.run(SKAction.repeatForever(SKAction.sequence([SKAction.run {
            self.createBall(forTrack: self.track)
            }, SKAction.wait(forDuration: 1)])))

    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let location = touch.location(in: self)
            let node = self.nodes(at: location).first

            if node?.name == "BALL" {
                currentScore = currentScore + ballValue
                node?.removeFromParent()
            }
        }

    }


    override func update(_ currentTime: TimeInterval) {

        // GAME TIMER RELATED STUFF

    }
}

我想我知道问题发生在哪里(触摸覆盖),我只是不明白它为什么会发生。无论如何你都可以提供帮助,这将是非常棒的

1 个答案:

答案 0 :(得分:2)

您有一个ballValue属性。每次你产生一个新球时,你都要ballValue设置那个新创建的球。所有其他现有球怎么样?触摸它们中的任何一个也值得ballValue

当触球时,您需要知道触球的正确值,这可能与最近创建的球的值不同。您可能想要做的是创建一个代表球的SKSpriteNode子类,并保留自己的值:

class BallNode: SKSpriteNode {
    let value: Int

    init(imageName: String, speed: CGFloat, value: Int) {
        let texture = SKTexture(imageNamed: imageName)
        self.value = value
        super.init(texture: texture, color: .init(white: 1, alpha: 0), size: texture.size())
        self.speed = speed
    }

    required init(coder decoder: NSCoder) { fatalError() }
}

像这样创建球:

func createBall(forTrack track: Int) {

// setupTracks()

    randomBallType = GKRandomSource.sharedRandom().nextInt(upperBound: 9)

    let ball: BallNode
    switch randomBallType {
    case 0: ball = BallNode(imageNamed: "blueOne", speed: 0.008, value: 1)
    case 1: ball = BallNode(imageNamed: "minusThree", speed: 0.008, value: -3)
    case 2: ball = BallNode(imageNamed: "minusFive", speed: 0.008, value: -5)
    // etc.
    }

    ball.name = "BALL"
    ball.size = CGSize(width: 100, height: 100)
    ball.physicsBody?.linearDamping = 0

    // ...UNRELATED BALL STUFF...

    self.addChild(ball)
}

然后,要处理触摸,请使用触摸的球的值来更新分数:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        if let ball = self.nodes(at: location).flatMap({ $0 as? BallNode }).first {
            currentScore += ball.value
            ball.removeFromParent()
        }
    }
}