iOS Tutorial Simple Parallax Scrolling iSpriteKit

here is an example of how to make a simple Parallax Scrolling using iSpriteKit

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

we call it in the Sub we SetUp the SkScene

B4X:
 Sub SetUpGameScene(aSize As Size) As SKScene
    GameScene.Initialize("GameScene")
    GameScene.SceneWithSize(aSize)
    GameScene.ScaleMode = GameScene.SKSceneScaleModeAspectFit
    GameScene.BackgroundColor = Colors.RGB(255,255,255)
    GameScene.PhysicsWorld.Gravity = Functions.CreateVector(0,-10)

    createBackground(GameScene) 'Create Parallax Scrolling
            
    Return GameScene
End Sub

 
Last edited:
Top