如果达到某个分数,请添加精灵

时间:2016-08-22 22:40:17

标签: swift sprite-kit case

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

这是我要实施的声明。

   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我只想要另一个球出现,如果前一个不在那里。

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

------ ---- UPDATE 除非得分达到50以检查球是否存在,否则代码运行良好。

谢谢,乔丹。

1 个答案:

答案 0 :(得分:1)

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

}

希望这有帮助!

相关问题