Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


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