Best viewed in Firefox

Awesty Productions

Generating Walls through ActionScript

April 21st, 2007 by awesty

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.

Click here to see an example of what we will be making. Arrow Keys to navigate.

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).

Making the walls

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…’.

Opening the Linkage Window

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

Identifier: walls, Export for actionscript and first frame

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 isnt pressed, and the left one is make the holder MC move right’.

And that is it. You should now have something similar to this. (LINK)

You can find more tutorials here.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed | Trackback URI

13 Comments »

Comment by omg Subscribed to comments via email
2007-04-22 08:01:48

all i can say is first comment!

 
Comment by Dark Subscribed to comments via email
2007-04-24 07:20:36

umm….what is the point of this??
no offence
but what good would moving walls do?
arent walls made for you to stop at, and if they move u never stop at them.
oh yah, and i sent you about 3 emails now and you never respond. please do.

Comment by awesty
2007-04-24 19:53:09

Well if you had a game where you have to get as far as you can through a tunnel you could use this. You make make it so the walls are random and it can go for as long as you want. It will stop the lag from a massive MC, since you could make some code that deletes the ones that have already been used and make new ones in front when needed.
Moving the walls would be if you were to navigate through them, since it would make what ever is in the middle look like it is. If it moved itself it would go off the screen.

Comment by dark Subscribed to comments via email
2007-04-26 07:15:01

ok i get it sort of but you still havent replied to my other emails. i really need your help with this part of my game because making an RPG on flash isnt easy. so please email me back or atleast let me know if you got them or not. thanks

 
 
Comment by Matt Subscribed to comments via email
2007-05-25 08:49:00

Because it’s a test for the player to see if he can time it correctly to make it past the wall especially if it is going fast. Duh.

 
 
Comment by me Subscribed to comments via email
2007-05-06 16:16:19

nice tut, thanks!

 
Comment by Timmy Subscribed to comments via email
2007-05-15 09:00:38

okay here is what i made with this tut:
http://www.swfup.com/file/10231
how could make it that if the plane hit the wall it would go to next frame or something and make random walls appear in the middle of the tunnel?
that would be soooooooooo great!!!

Comment by awesty
 
 
Comment by cruesrjr Subscribed to comments via email
2007-05-18 07:53:43

Hey Awesty, It’s me again… I want to make it go on it’s own, then make a guy that you control with arrow keys, and up key is jump and side keys are move, so when the bg walls are auto scrolling, It’s like a Mario game!

Can you help me with that?

Thanks!

 
Comment by Ricky Shroder Subscribed to comments via email
2007-05-31 14:06:07

“There height will change “.

You mean THEIR height?

There is over there.
Their is their daughter came on my property and kicked my dog.

Nice spelling. Great tutorial, really helped me out, once I got over all the spelling mistakes and typos. Come on man grow a beard and get moving already.

Comment by awesty
2007-07-02 18:10:45

Yea, I know what there, their and they’re mean. It was just a typo.

 
 
Comment by Josh Subscribed to comments via email
2007-07-08 06:59:06

How come I found 3 errors?

I have Flash 8 Pro and lookity look.

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 1: Statement must appear within on/onClipEvent handler
var wallY:Number = 475;

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 2: Statement must appear within on/onClipEvent handler
createEmptyMovieClip(”holder”, getNextHighestDepth());

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 3: Statement must appear within on/onClipEvent handler
for (i=0; i

 
Comment by simon Subscribed to comments via email
2007-08-12 17:11:36

is there a way to make the walls more random??

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.