Games Old-school Parallax Scrolling

salvadoro

Member
Licensed User
Longtime User
and now with iSpriteKit :)


Code:

B4X:
Sub createBackground(myScence As SKScene)
    'first create Sky
    Dim sky As SKSpriteNode
    sky.Initialize("")
    sky.SpriteNodeWithImageNamed("sky")
    sky.Size = myScence.Size
    sky.Position = Functions.CreatePoint(vpW/2,vpH/2)
    myScence.AddChild(sky)

    'create clouds
    paralexBackLayer(myScence,"clouds",0.12)

    'create mountains
    paralexBackLayer(myScence,"mountains",0.16)

    'create trees
    paralexBackLayer(myScence,"trees",0.06)
End Sub

Sub paralexBackLayer(myScene As SKScene, spritename As String, speed As Float)
    Dim MoveGroundSprite As SKAction
    MoveGroundSprite.Initialize
    MoveGroundSprite.MoveByX(-vpW,0,speed*vpW)

    Dim ResetGroundSprite As SKAction
    ResetGroundSprite.Initialize
    ResetGroundSprite.MoveByX(vpW,0,0)

    Dim GroundActions As SKAction
    GroundActions.Initialize
    GroundActions.Sequence(Array(MoveGroundSprite,ResetGroundSprite))

    Dim MoveGroundSpritesForever As SKAction
    MoveGroundSpritesForever.Initialize
    MoveGroundSpritesForever.RepeatActionForever(GroundActions)

    For i = 0 To 2 + GameScene.Frame.Width/vpW
        Dim Background As SKSpriteNode
        Background.Initialize("")
        Background.SpriteNodeWithImageNamed(spritename)
        Background.Size = Functions.CreateSize(vpW,vpH)
        Background.Position = Functions.CreatePoint(i * Background.Size.Width,Background.Size.Height/2)
        Background.RunAction(MoveGroundSpritesForever)
        myScene.AddChild(Background)
    Next
End Sub

Create:

B4X:
createBackground(GameScene) 'Create Parallax Scrolling

btw. there is no Render.Call in iSpriteKit. you create all your Nodes once and add actions to them. then you just add them to your Scene and they will run until you remove them from your scene. simple :)


Hi Ilan,

Could you please paste the complete project to understand this.
 
Top