Graphics Programs Reference
In-Depth Information
clip instance will move a defined speed in the appropriate direction. This code looks
similar to the first exercise except for layer and speed . We will come back to these
later in the exercise.
Step 4: Enter the ActionScript
Add to the ActionScript as shown. This script prevents each layer from moving off the
screen, taking into account which direction the artwork is moving. It checks to see how
far the instance has moved to the right or left. If it has moved too far, Flash shifts it
back over to create a seamless loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function parallax(layer, speed)
{
if(_xmouse > Stage.width/2){
layer._x -= speed;
}else{
layer._x += speed;
}
if(layer._x <= 0){
layer._x = layer._x + layer._width/2;
}else if(layer._x >= layer._width/2){
layer._x = layer._x - layer._width/2;
}
This completes the parallax function. If you test the movie, you won't see anything
move. In order for the function to work, it needs to be called. Since we want the move-
ment to loop, the function needs to be called constantly. As you learned from the previ-
ous exercise, an onEnterFrame handler can continuously call to a function.
Step 5: Enter the ActionScript
Add to the end of ActionScript as shown. Each instance will call the parallax function.
15
16
17
18
foreground_mc.onEnterFrame = function(){parallax(this,10);}
middleground_mc.onEnterFrame= function(){parallax(this,5);}
background_mc.onEnterFrame = function(){parallax(this,2);}
 
Search WWH ::




Custom Search