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.



0 comments:
Post a Comment