Search Tutorials
Step 2: Let's get the ground scrolling. The ground will scroll to the left of the screen, which is achieved by gradually reducing the x-coordinate of the ground movie clip. We actually need two copies of the ground movie clip placed next to each other to achieve a smooth scrolling effect. The second copy is sometimes referred to as a buffer. This ensures that the ground always covers the stage. There is a little flash file below that demonstrates this:
When the first copy of the ground has moved completely off the left of the stage both copies of the ground are reset back to their start positions. Duplicate the ground As shown above, we will need a second copy of the ground
movieclip. Instead of moving both of these movie clips to create our scrolling, what we will do is put them inside another movie clip (which we will call mainGround) and then we can move this parent movie clip instead of the two ground movie clips. In other words the mainGround movie clip will contain the two copies of the ground and we will just move mainGround instead of moving the two grounds individually. The first thing we need to is to put the ground movie clip inside another new movie clip.
So what we now have is the ground movieclip inside of the mainGround movie clip. Why did we do that? Because we will now duplicate the ground movie clip within the mainGround movie clip. Then we will be able to move the two ground movie clips at once by just moving the parent mainGround movie clip. This will be less work for the flash player - as it only has to move one movie clip not two and hence will improve the speed of the scrolling. It seems like extra work for little pay off - but in building flash games the speed of the flash player is very important, so where-ever possible you should reduce the amount of code or movie clip manipulation that flash has to do. We are now going to add in the clipEvent code for the mainGround
movie clip. Select the mainGround movie clip and choose Window> Actions
to open up the actions window then type the following. onClipEvent (load) {
ground.duplicateMovieClip("ground2", 100);
ground2._x = ground._x+ground._width;
groundStartx = this._x;
groundSpeed=10;
}
This code is contained within a load ClipEvent, so it will be run when the mainGround is first loaded. The second line of the code makes a duplicate copy of the
ground movie clip and gives it the name ground2
and a depth of 100. The fourth line creates a new variable called groundStartx which is equal to the x-position of the the mainGround movie clip. The purpose of this variable is to store the start location of the mainGround movie clip - we will use this variable later. The fifth line groundSpeed=10; just sets up a new variable called groundSpeed with a fixed value of 10. This will be the number of pixels the ground moves per frame.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|