ios - Gradually increasing speed of scrolling background in SpriteKit -
i making simple game in spritekit, , have scrolling background. happens few background images placed adjacent each other when game scene loaded, , image moved horizontally when scrolls out of screen. here code that, game scene's didmovetoview
method.
// self.gamespeed 1.0 , gradually increases during game let backgroundtexture = sktexture(imagenamed: "background") var movebackground = skaction.movebyx(-self.frame.size.width, y: 0, duration: (20 / self.gamespeed)) var replacebackground = skaction.movebyx(self.frame.size.width, y: 0, duration: 0) var movebackgroundforever = skaction.repeatactionforever(skaction.sequence([movebackground, replacebackground])) var i:cgfloat = 0; < 2; i++ { var background = skspritenode(texture: backgroundtexture) background.position = cgpoint(x: self.frame.size.width / 2 + self.frame.size.width * i, y: cgrectgetmidy(self.frame)) background.size = self.frame.size background.zposition = -100 background.runaction(movebackgroundforever) self.addchild(background) }
now want increase speed of scrolling background @ points of game. can see duration of background's horizontal scroll set (20 / self.gamespeed)
. not work, because code run once, , therefore movement speed never updated account new value of self.gamespeed
variable.
so, question simply: how increase speed (reduce duration) of background images' movements according self.gamespeed
variable?
thanks!
you use gamespeed
variable set velocity of background. work, firstly, need have reference 2 background pieces (or more if wanted):
class gamescene: skscene { lazy var backgroundpieces: [skspritenode] = [skspritenode(imagenamed: "background"), skspritenode(imagenamed: "background")] // ... }
now need gamespeed
variable:
var gamespeed: cgfloat = 0.0 { // using property observer means can update speed of // background setting gamespeed. didset { background in backgroundpieces { // minus, because background moving left right. background.physicsbody!.velocity.dx = -gamespeed } } }
then position each piece correctly in didmovetoview
. also, method work each background piece needs physics body can change velocity.
override func didmovetoview(view: skview) { (index, background) in enumerate(backgroundpieces) { // setup position, zposition, size, etc... background.physicsbody = skphysicsbody(rectangleofsize: background.size) background.physicsbody!.affectedbygravity = false background.physicsbody!.lineardamping = 0 background.physicsbody!.friction = 0 self.addchild(background) } // if wanted give background , initial speed, // here's place it. gamespeed = 1.0 }
you update gamespeed
in update
example gamespeed += 0.5
.
finally, in update
need check if background piece has gone offscreen (to left). if has needs moved end of chain of background pieces:
override func update(currenttime: cftimeinterval) { background in backgroundpieces { if background.frame.maxx <= 0 { let maxx = maxelement(backgroundpieces.map { $0.frame.maxx }) // i'm assuming anchor of background (0.5, 0.5) background.position.x = maxx + background.size.width / 2 } } }
Comments
Post a Comment