Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualILoveJesus

Posted 07 January 2013 - 02:33 AM

This sample code may help. Keep in mind that this was my first attempt at coding parallax scrolling. It worked but I am sure there is better code out there that does the same thing. Just repeat code like this for every background you have. Lower the numbers screenmovecount is divided by for backgrounds that are closer.

float screenmovecountx_;
float screenmoveoucnty_;
float moveamountx;
float moveamounty;
	

	

	
	

	if(levelstate_ == 0)
	{
 //Screenlocation is the camera world location
//Screenmovecount is how many pixels away the camera is from the middle of the level's
//first screen

		screenmovecountx_ = 512.0f - screenlocationx_;
		screenmovecounty_ = 384.0f - screenlocationy_;


		if(playlevel_.GetBackGround("static").IsOccupied() == true)
		{
			
		}
		if(playlevel_.GetBackGround("dynamica").IsOccupied() == true)
		{
		  
		//Making the numbers greater makes the background move slower in x or y direction
		        moveamountx = screenmovecountx_ / 32;
	                moveamounty = screenmovecounty_ / 64;

			
       // Background is 2048 x 768, game screen is 1024x 768x
       // The background's inital drawing position when the level starts is 1024x 340x
       // These do while loops prevents the background from being drawn offscreen
			if(moveamountx > 0.0f)
			{
				do{
					moveamountx = moveamountx - 1024.0f;
				}while(moveamountx > 0.0f);


			}

			if(moveamountx < -1024.0f)
			{
				do{
					moveamountx = moveamountx + 1024.0f;
				}while(moveamountx < -1024.0f);

			}

			
   //If the camera is at 512x340(middle of first screen) background moveamountx
// and y would be 0 and the background would be drawn at 1024x 340x, fitting snug
// For every 32 pixels the camera moves to the right the background will be drawn 1 
// pixel to the left
			levelbackgrounds_.at(1).SetSpritePosition(1024.0f + moveamountx, 384.0f + moveamounty);
			
		}

#2ILoveJesus

Posted 07 January 2013 - 02:28 AM

This sample code may help. Keep in mind that this was my first attempt at coding parallax scrolling. It worked but I am sure there is better code out there that does the same thing.

float screenmovecountx_;
float screenmoveoucnty_;
float moveamountx;
float moveamounty;
	

	

	
	

	if(levelstate_ == 0)
	{
 //Screenlocation is the camera world location
//Screenmovecount is how many pixels away the camera is from the middle of the level's
//first screen

		screenmovecountx_ = 512.0f - screenlocationx_;
		screenmovecounty_ = 384.0f - screenlocationy_;


		if(playlevel_.GetBackGround("static").IsOccupied() == true)
		{
			
		}
		if(playlevel_.GetBackGround("dynamica").IsOccupied() == true)
		{
		  
		//Making the numbers greater makes the background move slower in x or y direction
		        moveamountx = screenmovecountx_ / 32;
	                moveamounty = screenmovecounty_ / 64;

			
       // Background is 2048 x 768, game screen is 1024x 768x
       // The background's inital drawing position when the level starts is 1024x 340x
       // These do while loops prevents the background from being drawn offscreen
			if(moveamountx > 0.0f)
			{
				do{
					moveamountx = moveamountx - 1024.0f;
				}while(moveamountx > 0.0f);


			}

			if(moveamountx < -1024.0f)
			{
				do{
					moveamountx = moveamountx + 1024.0f;
				}while(moveamountx < -1024.0f);

			}

			
   //If the camera is at 512x340(middle of first screen) background moveamountx
// and y would be 0 and the background would be drawn at 1024x 340x, fitting snug
// For every 32 pixels the camera moves to the right the background will be drawn 1 
// pixel to the left
			levelbackgrounds_.at(1).SetSpritePosition(1024.0f + moveamountx, 384.0f + moveamounty);
			
		}

#1ILoveJesus

Posted 07 January 2013 - 02:27 AM

This sample code may help. Keep in mind that this was my first attempt at coding parallax scrolling. It worked but I am sure there is better code out there that does the same thing.

float screenmovecountx_;
float screenmoveoucnty_;
float moveamountx;
float moveamounty;
	

	

	
	

	if(levelstate_ == 0)
	{
 //Screenlocation is the camera world location
//Screenmovecount is how many pixels away the camera is from the middle of the level's
//first screen

		screenmovecountx_ = 512.0f - screenlocationx_;
		screenmovecounty_ = 384.0f - screenlocationy_;


		if(playlevel_.GetBackGround("static").IsOccupied() == true)
		{
			
		}
		if(playlevel_.GetBackGround("dynamica").IsOccupied() == true)
		{
		  
		//Making the numbers greater makes the background move slower in x or y direction
		        moveamountx = screenmovecountx_ / 32;
	                moveamounty = screenmovecounty_ / 64;

			
       // Background is 2048 x 768, game screen is 1024x 768x
       // The background's inital drawing position when the level starts is 1024x 340x
       // These do while loops prevents the background from being drawn offscreen
			if(moveamountx > 0.0f)
			{
				do{
					moveamountx = moveamountx - 1024.0f;
				}while(moveamountx > 0.0f);


			}

			if(moveamountx < -1024.0f)
			{
				do{
					moveamountx = moveamountx + 1024.0f;
				}while(moveamountx < -1024.0f);

			}

			
   //If the camera is at 512x340(middle of first screen) background moveamountx
// and y would be 0 and the background would be drawn at 1024x 340x, fitting snug
// For every 32 pixels the camera moves to the right the background will be drawn 1 
// pixel to the left
			levelbackgrounds_.at(1).SetSpritePosition(1024.0f + moveamountx, 384.0f + moveamounty);
			
		}

PARTNERS