How do sprites start blinking?

SO I created a game in which you press balls to make them jump. You have a point for each click. Why do my sprites start blinking when I reach 10 points. I think this has something to do with the method update(timeInterval). It could be a mistake or bad coding.

import SpriteKit

class GameScene: SKScene {
let backgroundNode = SKSpriteNode(imageNamed: "redbackground")
override func didMoveToView(view: SKView) {



    backgroundNode.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    self.addChild(backgroundNode)

 //=======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
}

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")
    }

}

Look at this to understand what I mean. (Click or tap the link)

This shows that sprites blink at 10 points

in advance, Jordan

+4
source share
3 answers

Blinking because of this

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")
    }

}

Infect inside the update method, when the rating becomes >= 10, you update the texture EVERY FRAME .

This is absolutely wrong.

, , (, 9 10 19 20 29 30).

, 10, 10 11, , .

?

shouldUpdateTexture: Bool = false, , score, score 9 10 19 20 29 30, shouldUpdateTexture true.

, update , shouldUpdateTexture, false .

class GameScene: SKScene {

    private var score = 0 {
        didSet {
            shouldUpdateTexture = oldValue < 10 && score >= 10 || oldValue < 20 && score >= 20 || oldValue < 30 && score >= 30
        }
    }
    private var shouldUpdateTexture = false
    private var backgroundNode = SKSpriteNode(imageNamed: "defaultbackground")


    override func update(currentTime: CFTimeInterval) {
        if shouldUpdateTexture {
            shouldUpdateTexture = false
            switch score {
            case 10...19: backgroundNode.texture = SKTexture(imageNamed: "orangebackground")
            case 20...29: backgroundNode.texture = SKTexture(imageNamed: "yellowbackground")
            case 30...Int.max: backgroundNode.texture = SKTexture(imageNamed: "yellowbackground")
            default: break
            }
        }
    }

}

.

Update

@Knight0fDragon, , , update, didSet.

:

: score , .

+3

update score, , . :

let backgroundNames = ["orangebackground","yellowbackground","greenbackground"]
// The didSet observer is called when the score is updated
var score:Int = 0 {
    didSet {
        // Check if score is divisible by 10
        if score % 10 == 0 {
            // Determine the array index
            let index = score / 10
            // Wrap to avoid index out of range
            let name = backgroundNames[index % backgroundNames.count]
            print (name)
        }
    }
}

, , . Apple,

... [ignoresSiblingOrder = true], . .

, zPosition ,

self.backgroundNode.zPosition = -100

.

+4

Just an alternative that allows you to make multiple changes in the account, without flooding the update function with unnecessary code

class GameScene: SKScene {

    private var score = 0 {
        didSet {
             ///if a change in the 10s happen,  then update texture
             if(score/10 > oldValue/10)
             {
                 switch score{
                     case 10...19: backgroundNode.texture = SKTexture(imageNamed: "orangebackground")
                     case 20...29: backgroundNode.texture = SKTexture(imageNamed: "yellowbackground")
                     case 30...Int.max: backgroundNode.texture = SKTexture(imageNamed: "greenbackground")
                     default: break
                 }
            }
        }
    }
    private var shouldUpdateTexture = false
    private var backgroundNode = SKSpriteNode(imageNamed: "defaultbackground")



}

Then for optimization: (Not necessary, but may help with how you think about the code)

class GameScene: SKScene {
    let bgTextures = [SKTexture(imageNamed: "orangebackground"),SKTexture(imageNamed: "yellowbackground"),SKTexture(imageNamed: "greenbackground")]

    private var score = 0 {
        didSet {
             ///if a change in the 10s happen,  then update texture
             let bgIndex = score / 10
             if((bgIndex > oldValue/10) && score < bgTextures.count * 10)
             {
                backgroundNode.texture = bgTextures[bgIndex - 1]
             }
             else
             {
                backgroundNode.texture = bgTextures[bgTextures.count - 1]

             }

        }
    }
    private var shouldUpdateTexture = false
    private var backgroundNode = SKSpriteNode(imageNamed: "defaultbackground")



}
+2
source

Source: https://habr.com/ru/post/1650983/


All Articles