Wednesday, 1 December 2010

Endless scrolling background in as3 part 2

This is part 2 of the Endless scrolling in as3 tutorial where I will show you how to control the scrolling images using the left and right keyboard keys. Take a look at the keyboard controls and the detect key press tutorials for more information on using keyboard keys.


Endless scrolling background in as3 part 2

Step 1

Open up part 1 of the tutorial then go in the Actions panel (F9) and make the following changes.

//Adds an event listener to the stage.
stage.addEventListener(KeyboardEvent.KEY_DOWN, keypress);

//This function moves the image to left when the left keyboard
//key is pressed and moves to the right when the right key is pressed.
function keypress(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.RIGHT :
s1.x += scrollSpeed;  
s2.x += scrollSpeed;  

if(s1.x > s1.width){
s1.x = -s1.width;
}else if(s2.x > s2.width){
s2.x = -s2.width;
}

break;

case Keyboard.LEFT :
s1.x -= scrollSpeed;  
s2.x -= scrollSpeed; 

if(s1.x < -s1.width){
s1.x = s1.width;
}else if(s2.x < -s2.width){
s2.x = s2.width-10;
}
break;
}
}
Note that I have removed the enter frame event and the moveScroll function and replaced it with the above code.


Step 2

Test your movie Ctrl + Enter.

3 comments:

Leonhard Euler 9 July 2013 at 18:48  

Hi,

How to scroll multiple objects,
objects from an array for example,
and more objects :)

iliketo 14 July 2013 at 13:41  

@Leonhard,

Are you using a For loop to scroll the multiple objects.

Leonhard Euler 14 July 2013 at 16:58  

No, I use for loop and array to create my objects, enemies...
I have read that its possible to put all game objects including player to one container movieclip and, after that add container to stage and then scroll it.
Now my only problem is that I cant set player in the center.
Im confused.

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP