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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。