PDA

View Full Version : Endless Scrolling Background


captsparrow
06-23-2007, 11:57 PM
How do you this seemingly simple thing in Flash MX 2004? I've tried the tutorial, unfortutanely is always jumps back the to the beginning.

Here's the code I used:

animator = createEmptyMovieClip('animator', 1);
bg_1 = animator.attachMovie('bg_mc', 'bg_1', 1);
bg_2 = animator.attachMovie('bg_mc', 'bg_2', 2);
bg_1._y = bg_1._width/2;
bg_1._y = bg_2._width/2;
speed = 10;
cloudWidth = 700;
animator.onEnterFrame = function() {
bg_1._y -= speed;
bg_2._y -= speed;
if (bg_1._y<=-bg_1._height) {
bg_1._y = cloudWidth;
}
if (bg_1._y<=-bg_2._height) {
bg_2._y = cloudWidth;
}
};

whizkid1990
06-26-2007, 01:32 PM
I think the actionscript is done in such a way so that it is supposed to go back to the beginning....... everytime bg_1._y reaches -700, it goes back to y coordinate 700. The trick here, i think, is to make the end part of your picture similar to the starting point.
This way, it will successfully give the illusion of a scrolling background.

zilla
07-15-2008, 06:56 AM
Hi There,

It's been a while since this post but since I used your code as the basis for my code for a scrolling rain texture I thought I'd reply.

Please keep in mind when creating the art within the movie it should be alinged to the left and top of the stage. The art should also be the same height as the stage. In this example the stage is 720px heigh.

Once you've created your rain movie in the library right click on it and make sure you set the linkage name to "mov_rain" and that "export for actionScript" is ticked.

Also and very importantly make sure the texture is vertically tiled! (i.e. the top and bottom connect seamlessly)

The following is AS2.0


var loop:MovieClip = this.createEmptyMovieClip("loop", this.getNextHighestDepth());
rain_1 = loop.attachMovie ("mov_rain", "rain_1", 1);
rain_2 = loop.attachMovie ("mov_rain", "rain_2", 2);

/* speed settings have to be divisive in full numbers of the rainStart
(stage height in negative) or the two movies will play out of sync.
720 stage means speeds of = 5 6 8 9 10 15 18 20 30 36 40 45 60 80 72 90 120 144 180 240 360 etc.*/

speed = 60;
rain_1._y = rain_1._y - speed;
rain_1._x = 0;
rain_2._y = rain_2._y + rain_2._height - speed;
rain_2._x = 0;
rainStart = -720;

loop.onEnterFrame = function(){
rain_1._y += speed;
rain_2._y += speed;

if (rain_1._y >=+rain_1._height) {
rain_1._y = rainStart;
}
if (rain_2._y >=+rain_2._height) {
rain_2._y = rainStart;
}
}