Join Todd Perkins for an in-depth discussion in this video Creating dynamically moving enemies, part of ActionScript 3.0 Projects: Game Development.
- View Offline
- [Instructor] In this movie, we'll use ActionScript to create the enemies in our game. If you're following along, I'm in Creating_Enemies.fla in the chapter one folder in the exercise files folder. Before we go to ActionScript, we need to export our monster for ActionScript so we can create them with ActionScript. I'm going to right-click, or Ctrl + click, monster in the library, and choose Linkage. Check Export for ActionScript, note the class name, Monster, which is what we'll use when we create monsters, Click OK, if another box comes up, click OK also.
Let's go to the first keyframe of the actions layer, and open the actions panel. The first thing we'll need to do is create some variables that we're going to be working with. Above where we created the cursor variable, at the very top of the code, I'm going to crate a variable called monstersInGame. This is going to be a positive integer. I'll give it the value inside of the initializeGame function. This is going to represent how many monsters are in our game. On the next line, we create a variable called monsterMaker.
This is going to be a Timer. This will create our monsters at a certain timed interval. On the next line, I'm going to create a container MovieClip that will hold all of our monsters. I'll call this container_mc. Now, inside of the initializeGame function, we'll give all of these variables values. I'm going to do that above all the other code we wrote inside the initializeGame function. Going to set monstersInGame equal to 10.
Going to set monsterMaker equal to a new instance of the Timer class. For the delay, we'll have 1,000 milliseconds, or one second. And for the repeat count, I'm going to type monstersInGame. Every second, our timer will create a monster until it's created as many monsters as we say should be in the game. Now, we use our container_mc variable to create a new instance of the MovieClip class.
Before we do anything else, let's just make sure to add that container to the stage. I'm going to use addChild, and I put container_mc on the stage. Go down a few lines. I'm going to use the monsterMaker timer and I'm going to add an event listener that's going to tell Flash what's going to happen as the timer runs. Type monsterMaker.addEventListner. The event type is TimerEvent. The name of the event is TIMER, in all caps.
Going to run is createMonsters. Go down to the next line, and I'm going to start the timer, monsterMaker.start. Now, we'll define the createMonsters function. Let's scroll down to the bottom of the code, and right below where we defined the dragCursor function, I'm going to define the createMonsters function. Remember that it receives an event.
The datatype is going to be TimerEvent. Doesn't return a value. Inside of this function, let's create a variable called monster, and the data type is MovieClip. We'll set monster equal to a new instance of the Monster class, and we'll set the monter's -x and -y values to be random. Let's type monster.x equals Math.random times stage.stageWidth, and this will put the monster in a random position on the stage where the -x position will be a random position as wide as the stage.
Now, let's go to the next line and do the same thing for the monster's -y position. So, monster.y equals math.random times stage.stageHeight. Now, if we wanted to, we could put this in a loop, and every time the timer runs, we could create multiple monsters, but, for now, let's just leave the code like this, and one monster will be created every second. You can feel free to modify the code if you want to. Let's go to the next line, and we'll put the monster on the stage using addChild, and we'll put it inside of the container_mc MovieClip.
So, container_mc.addChild(monster). Check the code to make sure everything looks right, and test the movie. And, there are our monsters. Notice, they created, there should be 10 of them, and they should stop being created. That looked like that was 10, so let's close this window. And, we've successfully created some monsters using ActionScript.
In the next movie, we'll add some interactivity to our monsters by making them disappear when we click on them.
- Understanding game development
- Building a shooting game
- Making a Ping-Pong game
- Developing the enemy's artificial intelligence
- Creating a word-guessing game
- Placing tiles in a slider puzzle game
- Creating a tank battle game
- Winning and losing a game