00:00
00:00
Newgrounds Background Image Theme

Xyphondevi just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

looping background

448 Views | 3 Replies
New Topic Respond to this Topic

looping background 2015-08-27 06:55:48


what is the best way to loop a background through coding or frames?
if it is through coding then what is an example code for it? if it is through frames how you do it?

because I want to use it in my game so that it loops when I move left or right but I have no idea how it works! any ideas ._.?

note:
* I use as3 to develop my game.
*I hope you guys understand what I mean by looping background I mean something like the moving building in the newgrounds preloader.

Response to looping background 2015-08-27 15:27:43


At 8/27/15 06:55 AM, cuteboy99 wrote: what is the best way to loop a background through coding or frames?

A simple way would be to put a copy of your background besides your background.
You can then move the background like this:
background.x = (background.x - movementX + width) % width;
where width is the width of your orginal image (half the width of your current background)
and movementX is how far you want to move your background.

If you want i can explain what the thought behind this code is.

Response to looping background 2015-08-28 03:53:14


At 8/27/15 03:27 PM, Etherblood wrote:
At 8/27/15 06:55 AM, cuteboy99 wrote: what is the best way to loop a background through coding or frames?
A simple way would be to put a copy of your background besides your background.
You can then move the background like this:
background.x = (background.x - movementX + width) % width;
where width is the width of your orginal image (half the width of your current background)
and movementX is how far you want to move your background.

If you want i can explain what the thought behind this code is.

I knew people will miss understand me! I don't want it to loop forever! I just want it to loop when ever it reach the end of the screen which is the white area, so the white area will not be shown when the background loops or am I not understanding how your code work?

this is how I used the code !

var bgWidth:Number= 1588.15;
var movementX:Number = 0;
this.addEventListener(Event.ENTER_FRAME,moveLeft);
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed);

function KeyPressed(e:KeyboardEvent):void
{
	if(e.keyCode == 68){
		movementX+=1;
	}
}

function moveLeft(e:Event):void
{
	mc_bgMid.x= (mc_bgMid.x - movementX + bgWidth) % bgWidth;	
}

Response to looping background 2015-08-28 05:00:37


At 8/27/15 03:27 PM, Etherblood wrote:
At 8/27/15 06:55 AM, cuteboy99 wrote: what is the best way to loop a background through coding or frames?
A simple way would be to put a copy of your background besides your background.
You can then move the background like this:
background.x = (background.x - movementX + width) % width;
where width is the width of your orginal image (half the width of your current background)
and movementX is how far you want to move your background.

If you want i can explain what the thought behind this code is.

ok I think I figure it out:

stop();

//background width
var bgWidth:Number= 1588.15;

//the width of the game
var screenWidth:Number = 550;

//the point that helps left and right to loop
var bgPoint:Number = (bgWidth - screenWidth);

//background speed
var bgSpeed:Number=5;

//is left button pressed?
var isLeft:Boolean = false;

//is right button pressed?
var isRight:Boolean = false;

//do again to infinity
this.addEventListener(Event.ENTER_FRAME,loop);

//do check buttons pressed
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed);

//check buttons 
stage.addEventListener(KeyboardEvent.KEY_UP,KeyReleased);

function KeyPressed(e:KeyboardEvent):void
{
	//D keyboard button pressed
	if(e.keyCode == 68){
		isLeft = true;
	}
	
	//A keyboard button pressed
	if(e.keyCode == 65){
		isRight = true;
	}
}

function KeyReleased(e:KeyboardEvent):void
{
	//D keyboard button released
	if(e.keyCode == 68){
		isLeft = false;
	}
	
	//A keyboard button released
	if(e.keyCode == 65){
		isRight = false;
	}
	
}

//do loop plan as it speak
function loop(e:Event):void
{
	//is left button pressed? yes? then move our background to the left
	if(isLeft){
		
		//is moving left reached end? no? then continue this code otherwise to the other code
		if(mcBg.x <=-bgSpeed){
		mcBg.x+=bgSpeed;
		}else{
		mcBg.x =-bgPoint;
		}
	}
	
	//is right button pressed? yes? then move our background to the right
	if(isRight){
		
		//is moving left reached end? no? then continue this code otherwise to the other code
		if(mcBg.x >=-bgPoint+bgSpeed){
		mcBg.x-=bgSpeed;
		}else{
		mcBg.x =0;
		}
	}
	
}

but Is that a good code or does it need improvement?