swift - 如果达到一定分数则添加 Sprite

标签 swift sprite-kit case

我有一个简单的游戏。您从一个球开始,然后弹跳直到获得 10 分。当你达到 10 分时,另一个球会出现(这样可以更快地到达下一个地标)。当达到 20 分时,出现第三个球。我已经设置好了,但我需要知道如何使用 case 执行 if 语句。

这是我要实现的语句。

   if (score == 10) && secondball.parent == nil {

这是我目前所拥有的:

import SpriteKit

class GameScene: SKScene {

override func didMoveToView(view: SKView) {

let ball = Ball()

    ball.position = CGPoint(x:self.frame.midX, y:440)

    addChild(ball)



    ball.physicsBody = SKPhysicsBody(circleOfRadius: 90)
    ball.physicsBody?.dynamic = true
    ball.physicsBody?.allowsRotation = false

    ball.physicsBody?.friction = 0
    ball.physicsBody?.angularDamping = 0
    ball.physicsBody?.linearDamping = 0
    ball.physicsBody?.usesPreciseCollisionDetection = true
    ball.physicsBody!.categoryBitMask = 0




override func update(currentTime: CFTimeInterval) {


    if shouldUpdateTexture {
        shouldUpdateTexture = false
        switch score {
    case 10...19: backgroundNode.texture = SKTexture(imageNamed: "orangebackground")

            let secondball = Ball()
            secondball.position = CGPoint(x:self.frame.midX * 1.65, y: 440)
            addChild(secondball)



            secondball.physicsBody = SKPhysicsBody(circleOfRadius: 90)
            secondball.physicsBody?.dynamic = true
            secondball.physicsBody?.allowsRotation = false

            secondball.physicsBody?.friction = 0
            secondball.physicsBody?.angularDamping = 0
            secondball.physicsBody?.linearDamping = 0
            secondball.physicsBody?.usesPreciseCollisionDetection = true
            secondball.physicsBody!.categoryBitMask = 0



    case 20...29: backgroundNode.texture = SKTexture(imageNamed: "yellowbackground")

    let thirdball = Ball()
    thirdball.position = CGPoint(x:self.frame.midX * 0.35, y:440)
    addChild(thirdball)



    thirdball.physicsBody = SKPhysicsBody(circleOfRadius: 90)
    thirdball.physicsBody?.dynamic = true
    thirdball.physicsBody?.allowsRotation = false

    thirdball.physicsBody?.friction = 0
    thirdball.physicsBody?.angularDamping = 0
    thirdball.physicsBody?.linearDamping = 0
    thirdball.physicsBody?.usesPreciseCollisionDetection = true
    thirdball.physicsBody!.categoryBitMask = 0

    case 30...39: backgroundNode.texture = SKTexture(imageNamed: "greenbackground")
    case 40...49: backgroundNode.texture = SKTexture(imageNamed: "bluebackground")
    case 50...59: backgroundNode.texture = SKTexture(imageNamed: "darkbluebackground")

    if ball.parent != nil {
        let ball = Ball()

        ball.position = CGPoint(x:self.frame.midX, y:440)

        addChild(ball)



        ball.physicsBody = SKPhysicsBody(circleOfRadius: 90)
        ball.physicsBody?.dynamic = true
        ball.physicsBody?.allowsRotation = false

        ball.physicsBody?.friction = 0
        ball.physicsBody?.angularDamping = 0
        ball.physicsBody?.linearDamping = 0
        ball.physicsBody?.usesPreciseCollisionDetection = true
        ball.physicsBody!.categoryBitMask = 0


            }

case:10...19 有效,case:20...29 也有效,但对于 case 50...59 如果前一个球不存在,我只希望另一个球出现。

如果您需要更多或更少的信息,请在下方评论。我会立即更新。

------更新---- 代码运行良好,除非分数达到 50 以检查球是否存在。

谢谢,乔丹。

最佳答案

case 50...59: 中,只需添加一个 if 语句来检查球是否已经存在。添加这样的内容:

if thirdBall.parent == nil {

addChild(thirdBall)
// Whatever other code you want to run only if the ball needs to be added to the scene

}

希望这对您有所帮助!

关于swift - 如果达到一定分数则添加 Sprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39089775/

相关文章:

swift - 考虑夏令时 Swift 设置日期选择器的日期

ios - 调整 SKShapeNode 的大小

ios - SpriteKit Swift 节点数问题

ios - SpriteKit世界定位错误

sql - 在 ORDER BY 中使用 CASE 将一些记录推到最后

swift - 从 CoreData 中删除对象

ios - 如何展开到导航堆栈上的第一个 View Controller

ios - 更新 PromiseKit : Cannot convert value of type 'PMKFinalizer' to expected argument type 'Promise<Void>' 后出错

mysql - 存储过程中的多案例选择

java - 使用逗号分隔大小写是不好的做法还是不赞成? Switch case 语句 Java