加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

斯威夫特 – 精灵怎么开始闪烁?

发布时间:2020-12-14 04:48:16 所属栏目:百科 来源:网络整理
导读:所以我创造了一个游戏,你可以点击球让它们跳起来.你得到每个点击点.当我达到10分时,为什么我的精灵会开始闪烁.我认为它与update(timeInterval)方法有关.它可能是一个错误或错误的编码. import SpriteKitclass GameScene: SKScene {let backgroundNode = SKSp
所以我创造了一个游戏,你可以点击球让它们跳起来.你得到每个点击点.当我达到10分时,为什么我的精灵会开始闪烁.我认为它与update(timeInterval)方法有关.它可能是一个错误或错误的编码.

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

}

看到这个,看看我的意思.(点击或点击链接)

This shows the sprites flashing at 10 points

先谢谢,乔丹

解决方法

因此而闪烁

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

}

在更新方法中,当分数变为> = 10时,您正在更新纹理EVERY FRAME.

这是绝对错误的.

如果您想更新纹理,只需在需要更改纹理时(例如,当分数从9到10,或从19到20或从29到30)时.

但是当纹理从10变为11时保持10时没有任何意义,因为你将用另一个相同的纹理更新纹理.

你怎么能这样做?

只需创建一个shouldUpdateTexture:Bool = false属性,然后每次更新分数,如果分数从9到10或19到20或29到30,则将shouldUpdateTexture转为true.

最后在update方法中检查shouldUpdateTexture是否为true,在这种情况下将其设置为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
            }
        }
    }

}

而已.

更新

正如@ Knight0fDragon所指出的那样,将我放入更新方法的代码移动到didSet中可能会更快.

Pros: this way you don’t need to perform a boolean equality check every frame

Cons: if you change the score value multiple times within the same frame you will perform multiple loads of the texture.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读