如何根据分数添加精灵?

时间:2016-08-10 20:13:51

标签: swift sprite-kit skspritenode

到目前为止,我几乎完成了比赛。我有一个由于重力而下落的球,并且人们轻拍它以使其反弹。每次点击精灵时,+1都会添加到乐谱中。当视图加载时,总共有3个球出现。我希望每个球出现在某个点标记处(当一个人达到10分时出现一个球,当玩家达到20个时出现另一个球)。

我尝试将其放在update(currentTime: CFTimeInterval)中,但我最终得到了这个:

enter image description here

(这是第二次进10分。)

似乎有无限数量的球,直到这个人达到21点才能阻止永无止境的级联。如果你点击级联,它确实会选择一个球然后跳出来,这就像我想要的那样。

这是 GameScene.swift (不包括update(CFTimeInterval)函数)

import SpriteKit

class GameScene: SKScene {
var ball: Ball!
var secondball: Ball!


override func didMoveToView(view: SKView) {
 //=======Ball 1=======//
    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


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



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

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

class Ball: SKSpriteNode {



    init() {
        let texture = SKTexture(imageNamed: "Ball")
        super.init(texture: texture, color: .clearColor(), size: texture.size())
        userInteractionEnabled = true

    }


    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let scene = self.scene as! GameScene
        scene.score += 1

        physicsBody?.velocity = CGVectorMake(0, 100)
        physicsBody?.applyImpulse(CGVectorMake(0, 900))



    }

}

 override func update(currentTime: CFTimeInterval) {


    if (score >= 10){
        self.backgroundNode.texture = SKTexture(imageNamed: "orangebackground")
    }


    if (score >= 20){
        self.backgroundNode.texture = SKTexture(imageNamed: "yellowbackground")
    }


    if (score >= 30) {
        self.backgroundNode.texture = SKTexture(imageNamed: "greenbackground")
    }
    if (score >= 40){
        self.backgroundNode.texture = SKTexture(imageNamed: "bluebackground")
    }
    if (score >= 50){
        self.backgroundNode.texture = SKTexture(imageNamed: "darkbluebackground")
    }
    if (score >= 60){
        self.backgroundNode.texture = SKTexture(imageNamed: "purplebackground")
    }
    if (score >= 70){
        self.backgroundNode.texture = SKTexture(imageNamed: "brownbackground")
    }
    if (score >= 80) {
        self.backgroundNode.texture = SKTexture(imageNamed: "maroonbackground")
    }
    if (score >= 90){
        self.backgroundNode.texture = SKTexture(imageNamed: "tanbackground")
    }
    if (score >= 100){
        self.backgroundNode.texture = SKTexture(imageNamed: "pinkbackground")
    }
    if (score >= 125) {
        self.backgroundNode.texture = SKTexture(imageNamed: "bronzebackground")
    }
    if (score >= 150) {
        self.backgroundNode.texture = SKTexture(imageNamed: "silverbackground")
    }
    if (score >= 175) {
        self.backgroundNode.texture = SKTexture(imageNamed: "goldbackground")
    }
    if (score >= 200) {
        self.backgroundNode.texture = SKTexture(imageNamed: "elitebackground")

    }




  }


}

2 个答案:

答案 0 :(得分:0)

更新方法的调用速度快于增加的分数。因此,在每次调用更新方法时,都会添加一个新球。

尝试这样的事情:

将此行代码移到update方法之外,以创建一个具有一个ball实例的全局变量:

let secondball = Ball() 

更新内部:

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

答案 1 :(得分:0)

您已选择控制Ball类的触摸部分,这是可能的但不方便。

首先在创作过程中给你的球命名:

ball.name = "ball1"
self.addChild(ball)
...
secondball.name = "ball2"
secondball.alpha = 0.0 // hide your secondBall at start
self.addChild(secondball)

然后,将touchesBeganBall课程移至您的GameScene

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first
        let positionInScene = touch!.locationInNode(self)
        let touchedNode = self.nodeAtPoint(positionInScene)

        if let name = touchedNode.name {
            switch name {
            case "ball1": // if you're touching ball1
                self.score += 1
                ball.physicsBody?.velocity = CGVectorMake(0, 100)
                ball.physicsBody?.applyImpulse(CGVectorMake(0, 900))
                if score == 10 {
                    ball.removeFromParent()
                    // or make some animation like:
                    //let fadeOut=SKAction.fadeOutWithDuration(1.0)
                    //ball.runAction(fadeOut,completion: {
                    //  self.ball.removeFromParent()
                    //})
                    secondball.alpha = 1
                    // or make some animation like:
                    //let fadeIn=SKAction.fadeInWithDuration(0.5)
                    //secondball.runAction(fadeIn,completion: {
                    //    self.secondball.physicsBody?.dynamic = true
                    //})
                    secondball.physicsBody?.dynamic = true
                }
            case "ball2": // if you're touching ball2
                self.score += 1
                secondball.physicsBody?.velocity = CGVectorMake(0, 100) // here you can decrease parameters to more difficult
                secondball.physicsBody?.applyImpulse(CGVectorMake(0, 900))
                if score == 20 {
                   // do the same thing maked for ball to show other ball
                }
            case "ball3": // if you're touching ball3
                self.score += 1
                secondball.physicsBody?.velocity = CGVectorMake(0, 100) // here you can decrease parameters to more difficult
                secondball.physicsBody?.applyImpulse(CGVectorMake(0, 900))               
                if score == 21 {
                   // do other stuff here
                }
            default: 
               break 
            }
        }
    }