Skip to content

Instantly share code, notes, and snippets.

@willrax
Last active August 29, 2015 14:01
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willrax/a448bd97417f919fd76e to your computer and use it in GitHub Desktop.
Save willrax/a448bd97417f919fd76e to your computer and use it in GitHub Desktop.
Parallax scrolling with Sprite Kit and RubyMotion
# Video of it in action: http://cl.ly/VSIF
class MyScene < SKScene
def scroll_action(x, duration)
width = (x * 2)
move = SKAction.moveByX(-width, y: 0, duration: duration * width)
reset = SKAction.moveByX(width, y: 0, duration: 0)
SKAction.repeatActionForever(SKAction.sequence([move, reset]))
end
def add_ground
x = CGRectGetMidX(self.frame) + 7
2.times do |i|
ground = SKSpriteNode.spriteNodeWithTexture ground_texture
ground.position = CGPointMake(x + (i * x * 2), 56)
ground.runAction scroll_action(x, 0.02)
addChild ground
end
end
def mid_x
CGRectGetMidX(self.frame)
end
def mid_y
CGRectGetMidY(self.frame)
end
def sky_texture
SKTexture.textureWithImageNamed "skyline.png"
end
def ground_texture
SKTexture.textureWithImageNamed "ground.png"
end
def add_skyline
2.times do |i|
x_position = mid_x + (i * mid_x * 2)
skyline = SKSpriteNode.spriteNodeWithTexture sky_texture
skyline.position = CGPointMake(x_position, mid_y)
skyline.zPosition = -20
skyline.scale = 1.12
skyline.runAction scroll_action(mid_x, 0.1)
addChild skyline
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment