Skip to content

Instantly share code, notes, and snippets.

@willrax
Last active August 29, 2015 14:01
Show Gist options
  • 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 add_skyline
texture = SKTexture.textureWithImageNamed "skyline.png"
x = CGRectGetMidX(self.frame)
2.times do |i|
skyline = SKSpriteNode.spriteNodeWithTexture texture
skyline.scale = 1.12
skyline.position = CGPointMake(x + (i * x * 2), CGRectGetMidY(self.frame))
skyline.zPosition = -20
skyline.runAction scroll_action(x)
addChild skyline
end
end
def add_ground
texture = SKTexture.textureWithImageNamed "ground.png"
x = CGRectGetMidX(self.frame) + 7
2.times do |i|
ground = SKSpriteNode.spriteNodeWithTexture texture
ground.position = CGPointMake(x + (i * x * 2), 56)
ground.runAction scroll_action(x)
addChild ground
end
end
def scroll_action(x)
width = (x * 2)
move = SKAction.moveByX(-width, y: 0, duration: 0.02 * width)
reset = SKAction.moveByX(width, y: 0, duration: 0)
SKAction.repeatActionForever(SKAction.sequence([move, reset]))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment