Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Mehmet Turan

Member Since 16 May 2012
Offline Last Active May 18 2012 11:22 AM

Topics I've Started

How can I add a parallax effect into my side-scrolling game?

16 May 2012 - 03:32 PM

How can I add a parallax effect into my side-scrolling game? I read a lot about parallax scrolling so I know what the logic is and what parallax is but I can't create a dynamic parallax effect.

I have draw and update functions like this:
Camera2D guiCam = new Camera2D();

updateWorld(){

guiCam.position.x+=7f;
}



public class Background extends DynamicGameObject {


int dirX=-1;


public Background(float x, float y, float width, float height,float velX) {
super(x, y, width, height);
velocity.x = velX;

}
public void update(float deltaTime,float cameraX) {


position.x = cameraX * 0.3f; // magic number to make the background move slowly
if(position.x + bounds.width-1200 <= 0)
position.x+= 2400;

}

}

 void render()
	{
     batcher.beginBatch(Assets.background);
        
        batcher.drawSprite(world.onKatman.position.x, 240, 2400, 480, Assets.backgroundRegion);
       
        if(world.onKatman.position.x + world.onKatman.bounds.width-1200 < 800)
         batcher.drawSprite(world.onKatman.position.x + world.onKatman.bounds.width, 240, 2400, 480, Assets.backgroundRegion);
        
     
        
        batcher.endBatch();
	}
So this draws a parallax effect with two background objects but it draws it statically. I'm creating a 2D side-scrolling game and I'm translating my character and my camera position along x axis. So I have to translate my parallax effect with my camera but when I add to background `position.x` it doesn't work. How can I solve this?

PARTNERS