1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

iOS Tutorial Simple Parallax Scrolling iSpriteKit

Discussion in 'iOS Tutorials' started by ilan, Aug 21, 2017.

  1. ilan

    ilan Expert Licensed User

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

    Code:
    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

    Code:
    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: Aug 21, 2017
    wonder, moore_it, GabrielM and 2 others like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice