首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sprite-Kit变量不会停止添加

Sprite-Kit变量不会停止添加
EN

Stack Overflow用户
提问于 2017-04-20 11:21:03
回答 1查看 25关注 0票数 0

在我的游戏中,每当更新函数中出现某些条件时,我都会尝试将变量分数加1。问题是,更新函数每一帧都会被调用,所以我的得分变量在它应该缓慢上升1的时候却一直攀升到数百分。下面是我的代码:

代码语言:javascript
复制
override func update(_ currentTime: TimeInterval) {

    if circleStart.frame.width >= self.frame.width && userTouched == false{
        hasTouchedEdge = true
    }

    if hasTouchedEdge == true && userTouched == false {
        scaleSpeed -= 0.2  
    }

    if scaleSpeed <= 1.0 && userTouched == false { 
        scaleSpeed += 0.3
        hasTouchedEdge = false   
    }

    if userTouched == false{ 
        scaleSpeed += 0.1
        circleStart.setScale(CGFloat(scaleSpeed))
        circleLength = circleStart.frame.width
        acceptableBound1 = outerCircle.frame.width
        acceptableBound2 = outerCircle.frame.width - 100
    }

    if userTouched == true && circleLength > acceptableBound2 && circleLength < acceptableBound1{ 
        print("worked")
        isCorrect = true
    }

    if userTouched == true && circleLength > acceptableBound1 {
        gameOverLabel.isHidden = false
        playAgain.isHidden = false
        playAgain.run(fadingTapIn)   
    }

    if userTouched == true && circleLength < acceptableBound2{ 
        gameOverLabel.isHidden = false
        playAgain.isHidden = false
        playAgain.run(fadingTapIn)    
    }

    if isCorrect == true {  
        score += 1  
    }

    scoreLabel.text = String(score)   
}

我需要做什么或添加什么才能让我的分数变量以我想要的方式在上面解释?

EN

回答 1

Stack Overflow用户

发布于 2017-04-21 04:08:34

简单的解决方案:在score += 1之后添加isCorrect = false

好的解决方案:尝试删除更新中的大部分代码。在大多数情况下,更新几乎每秒调用60次,由于几个原因,它不是进行更改的最佳位置。

尝试使用

代码语言:javascript
复制
override func touchesBegan(touches: Set<UITouch>,with event: UIEvent?) {
    for touch in touches {
        let location = touch.location
        if (someNode.contains(location)) {
            //This if statement isn't needed, but an example of how you can determine if a node was pressed
            someFunctionToDoStuff(location)
        }
    }
}

代码语言:javascript
复制
override func touchesBegan(touches: Set<UITouch>,with event: UIEvent?) {
    let touch = touches.first 
    let location = touch.location
    if (someNode.contains(location)) {
        //This if statement isn't needed, but an example of how you can determine if a node was pressed
        someFunctionToDoStuff(location)
    }
}

第一个选项可能会运行多次,具体取决于确定的触摸次数。第二种选择将只关注它注意到的第一次接触,而忽略其余的。

你可以随心所欲地定义这个函数,只要确保在需要时获取触摸的位置即可:

代码语言:javascript
复制
func someFunctionToDoStuff(_ location: CGPoint) {
    score += 1
    // Then maybe move a node to where the press was located
    ball.position = location
}

您可以使用touchesBegan、touchesMoved和touchesEnded作为函数。

看看Ray Wenderlich的SpriteKit教程,你会学到很多东西!https://www.raywenderlich.com/145318/spritekit-swift-3-tutorial-beginners

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43509630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档