Learn about functions, methods and how to handle timing using Adobe Flash ActionScript 2.0
Whenever you want to develop a piece of code that you can use over and over, you'll need to use functions. You are already very familiar with the way they work because methods work pretty much like functions.
When you call a method such as gotoAndPlay(label); you are essentially executing a function that performs a task. In this case the task is to go to the frame labeled "label" and start playing from that moment on. It's very easy for us to create our own functions by using the following syntax:
Alright, let's create a new movie and try out this new function powers. Our first function will be very easy. In keyframe 1 type in the following function:
If you run flash, you'll notice that nothing will happen. Flash will not execute any code in a function until you call upon that function. Therefore we must add a statement that calls the function. It would look like this:
Notice a couple of things about functions. The parameters area is optional. The function declaration does not contain a semicolon, but the function call should. Now whenever we want the program to type "Hello World", we can repeat the function call again.
The code above is great and it does save a lot of time if we want to work with a lot of code, but our functions become more powerful when we can pass them information. What if we wanted to say Hi, then a person's name, but we wanted to make the person's name different everytime we called the function. We would type the function this way:
Now we can call the function and make it do different things.
This will put Hello Fred and Hello Roger in your output window.
Creating multiple levels
So, how does this apply to your game. We need a way to create multiple levels in the game and functions will come in handy. Your game should have moving enemies and a moving ship. Your ship should be shooting by now and the bad guys should be disappearing when you shoot them.
The next step in the development of your game is to further develop your ship so that it explodes or something happens when it is hit.You'll need to go into the movie clip for your ship and develop a version of the ship when it explodes. Then on the script for the asteroids, you'll need to add a script to gotoAndPlay() the exploding movie when one of the bad guys hits the ship.
What I have for my collision detection when the asteroid hits the ship looks something like this:
So when the asteroid hits the ship, my ship plays the explode frame and then returns to it's original state.
Keeping track of the asteroids
Now that we have that working, we need to find out when all of the bad guys are destroyed so we can start a new level. We'll need to create a variable that keeps track of how many bad guys (asteroids in this case) are on the screen. We can store this number in a variable called howmanybadguys. We'll go to our first keyframe where we spawned the bad guys and change the code. It should look something like this:
for (i=1; i<=numberofasteroids; i++)
Now, when an asteroid is hit by a ship or by the bullet, we want to decrement (which means decrease by one) the number of asteroids in the numberofasteroids variable. On the asteroids code, look for your hittest and add something like this:
Now, be very careful that you don't put this after the removeMovieClip(this); command, otherwise, you'll be trying to do a calculation after the movie clip has been deleted.
Flash has another neat way of taking care of this. Remember the good ole onClipEvent(load) event. That's the event that is called one time before the particular clip loads into the movie. In the case of our asteroids this event happens when the asteroids are dynamically created. Well, there's a similar event called unload that happens once when you delete a dynamically generated movie clip.
We can rewrite the above code to take advantage of the fact that an asteroid will disappear when it is being hit by either the shot or the ship and use this code:
You'll definitely want to do something more than just use a trace function here though. What you want to happen is that whenever all the bad guys are gone, you want new bad guys to appear. Preferrably more. That's where functions become handy. We want to put the creation of bad guys into a function so that when we're outta bad guys, we just call that function to spawn some new ones.
The function should look something like this:
for (i=1; i<=numberofasteroids; i++)
You can use something like this in your asteroids movie clip:
This will create 6 asteroids when the old ones are destroyed. You'll need to figure out a way to increase the difficulty. I suggest creating another variable that keep tracks of the current level (you won't be able to call it level because the word level has meaning to Flash. Call the variable mylevel or something like that. Start the variable with a value of 1 and then increase it everytime the asteroids are destroyed. Then you can set the _root.numberofasteroids to mylevel*somenumber or mylevel+somenumber or some other formula. I'll leave it up to you to figure this part out, but your name must have multiple levels.
You might want to keep track of how many lives your character has and if they get killed too many times, you'll want to stop the game and play some kind of gameover screen. This should be pretty easy and you should be able to figure this out as well. I suggest some kind of variable that keeps track of when the game is playable and then an if statement at the beginning of the asteroid scripts and the ship scripts that only makes something moveable if these variables have some value.
Spawning at set intervals
Flash has a very convenient function for doing things at certain times. In your game, you might want to have some good guys or bad guys appear at certain times to make your game harder. This is fairly easy to do with Flash's setInterval method.
This function allows you to have Flash keep track of time and call a second function that executes every few seconds. To create the timer, you would type something like this:
This runs a function called dosomething() 10 seconds. You still need to have a dosomething() function somewhere in your program that does some other thing. In our case, we might want to have a function that spawns some dubious character placed here.
You can also kill the timer so that it stops running. To do that first you create a variable and make the function call equal the variable like so:
Whenever you want to kill the timer, simply run a different function called clearInterval()
and pass it the name of the variable like so:
This is a convenient way to have a program to execute some function only once after 10 seconds. You'd code that like this:
// Do whatever you want before the next line.
If you want to learn more, check out the full description in Flash's handy reference panel (window menu, then reference).blog comments powered by Disqus