Android Example libGDX and Parallax (scrolling) background with multiple layers

Hi.

This is example with two simple classes "ParallaxLayer" and "ParallaxBackground".
It help us to make scrolling backgrounds very easily in just few lines of code and
"unlimited" number of layers.
You can set scrolling speed of any individual layer.

Example on using it:

B4X:
    'Horizontal Scrolling
    Dim Layer1 As ParallaxLayer: Layer1.Initialize(backgroundRegion,.1,0): ParalaxLayers.Add(Layer1): Layer1.Name = "Hills" ' the "Name" parameter is just helper (like a Tag property of a button)
    Dim Layer2 As ParallaxLayer: Layer2.Initialize(cloudsRegion,.2,0)    : ParalaxLayers.Add(Layer2): Layer2.Name = "clouds"
    Dim Layer3 As ParallaxLayer: Layer3.Initialize(MidleRegion,.4,0)    : ParalaxLayers.Add(Layer3): Layer3.Name = "fence"
    Dim Layer4 As ParallaxLayer: Layer4.Initialize(landRegion,4,0)      : ParalaxLayers.Add(Layer4): Layer4.Name = "land"
    myParallaxBackground.Initialize(ParalaxLayers,Camera,Batch)

and in "LG_Render" method we call

B4X:
If myParallaxBackground.ScrolingHorizontal = True Then
        myParallaxBackground.moveX(Velocity * DeltaTime)
        myParallaxBackground.render
    Else
        myParallaxBackground.moveY(Velocity * DeltaTime)
        myParallaxBackground.render 
End If

The code of those classes is not mine, I fond it somewhere on the internet (java) and
adapted it to work with B4A and Informatix libGDX library.
So all the credits goes to them :)

See the attached example.
You'll need version 1.0 of the libGDX library.

screenshoot of attached example:

1.jpg


the file is too big for the forum, so there is a mine dropbox link
https://dl.dropboxusercontent.com/u/40815149/parallaxExample.zip

second example: (see the post #5)
https://dl.dropboxusercontent.com/u/40815149/parallaxExample2.zip

CheerS!
 
Last edited:

Jaames

Active Member
Licensed User
Longtime User
Don't know really, it's a common case with my chrome as well, I can't download it...
inside a zip :

Files - folder (files - background.png , clouds.png , land.png , midlehill.png)
Objects - folder (res - folder ; AndroidManifest.xml - file)
ParallaxBackground.bas - file
ParallaxLayer.bas - file
SimpleParalax.b4a - file
SimpleParalax.b4a.meta - file

And that's all...
Try to download with mozilla or ie and scan it with antivirus for any case...
 

Jaames

Active Member
Licensed User
Longtime User
As per request , there is a second example (see the post #1) With another simple class lgSpriteAnimator for easy sprite animation as well as a box2d simple - basic example on how to move - rotate animated character .
There is also example on using 2 camera objects , one for ui , and one for the game itself (for easily scaling and creating ui objects).

TIP : Touch the screen as soon as the "game" begins to avoid obstacles moves the character out of screen. :)

2.jpg
 

Douglas Farias

Expert
Licensed User
Longtime User
@Jaames how to know when character go out of screen
have a position to this?

like for example on Sub LG_Update

If CharacterBody.Position.x < 0%x Then
Log("lalala")
End If
 
Last edited:

Jaames

Active Member
Licensed User
Longtime User
@Jaames how to know when character go out of screen
have a position to this?

like for example on Sub LG_Update

If CharacterBody.Position.x < 0%x Then
Log("lalala")
End If
The 0.0 position is actually in the center of the screen.
So for example character could be -200 on the x axis and stil visible. If you prefer 0.0 to be on the bottom left, you can easily accomplish that with moving camera object.
 

Douglas Farias

Expert
Licensed User
Longtime User
@Jaames how to put a fixed obstacles , only the player runing and the random of obstacles on the screen come?
 

Reids

Member
Licensed User
Longtime User
Great Sample!
Please more sample, like move player left or right then parallax moving like basic mario games, parallax move left or right based player movement
 

SpinBower

Member
Licensed User
Longtime User
Could this be made into a scrollview? Like could I combine it with MSCardView and make a user-scrollable list with a background and other layers for the parallax effect?
 
Top