Last active
August 29, 2015 14:01
-
-
Save willrax/a448bd97417f919fd76e to your computer and use it in GitHub Desktop.
Parallax scrolling with Sprite Kit and RubyMotion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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