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

斯威夫特 – 精灵怎么开始闪烁?,第1张

概述所以我创造了一个游戏,你可以点击球让它们跳起来.你得到每个点击点.当我达到10分时,为什么我的精灵会开始闪烁.我认为它与update(timeInterval)方法有关.它可能是一个错误或错误的编码. import SpriteKitclass GameScene: SKScene {let backgroundNode = SKSpriteNode(imageNamed: "redback 所以我创造了一个游戏,你可以点击球让它们跳起来.你得到每个点击点.当我达到10分时,为什么我的精灵会开始闪烁.我认为它与update(timeInterval)方法有关.它可能是一个错误或错误的编码.

@R_301_5565@ SpriteKitclass 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.

总结

以上是内存溢出为你收集整理的斯威夫特 – 精灵怎么开始闪烁?全部内容,希望文章能够帮你解决斯威夫特 – 精灵怎么开始闪烁?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1009730.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存