.

Page 1

In this tutorial you will learn how to generate walls through actionscript. There height will change and you can make them go for as long as you want. I am using Flash 8 but Flash MX 2004 will work as well. This isn’t the best way to do this, it is just the way I find easiest.

First of all, you need to make your wall. Make sure it is 50 pixels wide. Mine is just two rectangles with a gap of 220 pixels between them vertically. Each rectangle is 250 in height. If you use those dimensions this tutorial will be easier, since you wont have to change the code at all. Make them a MC (Movieclip) and delete the MC from the stage. (Both the rectangles should be in the same MC).



Now, open the library (Ctrl+L or Windows>Library) and you should see the MC you just made. Right Click on it and then go to 'Linkage...'.



A window labeled 'Linkage Properties' should now be open. Enter the same settings as I did below.



Now we can put our MC on the stage with actionscript by using the attachMovie() function. Enter this code onto the frame.
var wallY:Number = 475;
createEmptyMovieClip("holder", getNextHighestDepth());
for (i=0; i<200; i++) {
    holder.attachMovie("walls", "wall"+i, i);
    _root.holder["wall"+i]._x = i*50;
    _root.holder["wall"+i]._y = wallY;
    if (wallY>550) {
        down = true;
    }
    if (wallY<400) {
        down = false;
    }
    if (down) {
        wallY -= 50;
    } else {
        wallY += 50;
    }
}
If you tested your movie now (Ctrl+Enter) you would find that you would have the wall on your stage, and that it covers the whole stage length and changes heights. It doesn't move with the arrow keys yet, I will show you how to do that later. But for know I will explain the code you just got.

Line 1: This is just declaring a variable called wallY. This is going to tell the wall what its _y coordinates should be.

Line 2: This is creating an empty movie clip called holder. All the wall MC's that are placed on the stage will be held in here, so when it comes to moving them we can just move holder instead of having to use a for statement. This stops flash from lagging a bit.

Line 3: This is where all the magic happens. It is a for statement, that will keep running until 'i' isn't less than 200. Therefore following code will 200 times.

Lines 4-6: Here we are attaching the 'wall' MC to the stage. You should see that the code is holder.attachMovie(wall... That means we are attaching it inside the holder MC. We are also declaring the walls _x and _y coordinates. The _x coordinates is i*50, so basically it increases by 50 pixels each time. The _y is equal to the wallY variable.

Lines 7-17: All of these lines are determining the walls height. 7-12 are seeing if the walls should move up or down. 12-17 are then making the wallY variable increase or decrease depending on what 7-12 came up with.

Now, if you enter this code beneath the code I just gave you your walls should move with the arrow keys.
onEnterFrame = function () {
    if (Key.isDown(Key.RIGHT)) {
        holder._x -= 5;
    } else if (Key.isDown(Key.LEFT)) {
        holder._x += 5;
    }
};
That is pretty simple. The first line means 'Everytime this frame is entered'. So the code after that will happen every frame. Unlike the previous you just got, which will only happen once.
The rest of the frames are what makes the walls move. if(Key.isDown(Key.RIGHT)){ is an if statement that is checking is the right arrow key is pressed. If it is than the holder MC's _x coordinates is told to decrease by 5 pixels. That will make it move left, but appear as if you are traveling right. The next part says 'if the right arrow key isn't pressed, and the left one is make the holder MC move right'.

And that is it.



No comments found ! Click here to be the first adding comments for this tutorial!