Call: (209) 560-6611

How to make a scrolling background in AS3

Date: June 24, 2014 Author: ang Category: Tutorials Tags: , , Comments: 0

Here is some AS3 code for a continuous scrolling background. The code requires 2 background images of the same width that are continuously placed in back of the previous image to imitate a continuous scrolling background. Works best for slow moving background.

 Javascript |  copy code |? 
01
package  {
02
 
03
import flash.display.MovieClip;
04
import flash.events.Event;
05
 
06
public class eaterMain extends MovieClip {
07
 
08
//create vars
09
var BGround1:Background=new Background();//background 1
10
var BGround2:Background=new Background();//background 2
11
 
12
public function eaterMain() {
13
// constructor code
14
 
15
stage.addChildAt(BGround1, 0);//background1 create child
16
stage.addChildAt(BGround2, 0);//background2 create child
17
BGround2.x = BGround2.width;
18
 
19
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
20
}
21
 
22
public function enterFrameHandler(e:Event):void
23
{
24
moveScroll(1);
25
}
26
 
27
public function moveScroll(scrollSpeed): void
28
{			
29
BGround1.x -= scrollSpeed; 
30
BGround2.x -= scrollSpeed; 
31
if(BGround1.x <= -BGround1.width)
32
{
33
BGround1.x =BGround2.x + BGround2.width;
34
}
35
else if(BGround2.x <= -BGround2.width)
36
{
37
BGround2.x = BGround1.x + BGround1.width;
38
}
39
}
40
 
41
}
42
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Before you post, please prove you are sentient.

What is 2 times 9?

Before you post, please prove you are sentient.

What do bees make?