sprite kit - Moving Background Swift SKSpriteKit -
i'm working on application i'm trying set moving background. have transparent image full of clouds i'm using. problem is, how can make move more smoother? i've tried play speeds still looks laggy. blessing.
here's video of got going. http://sendvid.com/78ggkzcj
and here's picture of cloud image. cloud image
here's code. think should change or differently?
class gamescene: skscene { // background let background = skspritenode(texture:sktexture(imagenamed: "background")) // clouds var maincloud = skspritenode() var cloud1next = skspritenode() // time of last frame var lastframetime : timeinterval = 0 // time since last frame var deltatime : timeinterval = 0 override func didmove(to view: skview) { background.position = cgpoint(x: 0, y: 0) // prepare clouds sprites maincloud = skspritenode(texture: sktexture(imagenamed: "cloudbg1")) maincloud.position = cgpoint(x: 0, y: 0) cloud1next = maincloud.copy() as! skspritenode cloud1next.position = cgpoint(x: maincloud.position.x + maincloud.size.width, y: maincloud.position.y) // add sprites scene self.addchild(background) self.addchild(maincloud) self.addchild(cloud1next) } override func update(_ currenttime: timeinterval) { // called before each frame rendered // first, update delta time values: // if don't have last frame time value, first frame, // delta time zero. if lastframetime <= 0 { lastframetime = currenttime } // update delta time deltatime = currenttime - lastframetime // set last frame time current time lastframetime = currenttime // next, move each of 4 pairs of sprites. // objects should appear move slower foreground objects. self.movesprite(sprite: maincloud, nextsprite:cloud1next, speed:100) } // move pair of sprites leftward based on speed value; // when either of sprites goes off-screen, move // right appears seamless movement func movesprite(sprite : skspritenode, nextsprite : skspritenode, speed : float) -> void { var newposition = cgpoint.zero // both sprite , duplicate: spritetomove in [sprite, nextsprite] { // shift sprite leftward based on speed newposition = spritetomove.position newposition.x -= cgfloat(speed * float(deltatime)) spritetomove.position = newposition // if sprite offscreen (i.e., rightmost edge // farther left scene's leftmost edge): if spritetomove.frame.maxx < self.frame.minx { // shift on it's immediate right // of other sprite. // means 2 sprites // leap-frogging each other both move. spritetomove.position = cgpoint(x: spritetomove.position.x + spritetomove.size.width * 2, y: spritetomove.position.y) } } } }
your code looks fine. getting low fps because running game in simulator. if run on real device, should smooth.
Comments
Post a Comment