Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So let's take a look to see where this project is at currently. I want to go ahead and test this file. We have our intro, and when I click Play, you'll see lots of alien ships start to gather at the top of the screen, which does look intimidating. But at this point, you know what, I want them to basically attack, so I want these ships to start moving downward. And I'm going to do that by adding a code snippet.
So in the Code Snippets panel, right down in here, Move Enemies. Again, I want to move many different things. I'm just kind of go ahead and double-click on move enemies, and it adds this code right here. Now that is plural, so it's going to move a lot of things, but let's take a look at this specifics, okay. Now, this is a complete sort of code chunk, so you can use this in many different projects, because quite frankly, there is this reference to this array. Well, I already have an enemies array, but basically you need to make sure you have an enemies array if you don't already have one. You need to create it in this case. I've already created that. Did that in the previous movie.
Also, you want to create a tempEnemy from the Enemy library as well. So right over here, there is my enemy, and that's been done already as well, so I'll just remove it. So it's basically two references to it just tell you not to forget to do that, because that's what's being referenced right in here. So for that enemies array, for however many are in that box, if you will, go ahead and loop through this for loop that number of times. This tempEnemy is going to be equal to the enemies and whatever time it's through, so it's going to loop through this whole box each time.
TempEnemy just refers to each and every item that comes through, but right here, this is where most of the action happens. It says "Move the tempEnemy down," so that'd be its Y position "from its current position" and then add to it plus, basically, its speed. Where does this speed come from? Well, let's a take a look. As I scroll up, right up in here, here is this tempEnemy.speed.
And that's what I want to do. I want to a reference this speed number, which happens to be 3. This will change sort of throughout the game, but I do want to move up this function now, okay. So I am going to take this moveEnemies function and the various comments, and I am going to cut it, and I'll scroll up. Right underneath makeEnemies, moveEnemies. This is going to loop through the contents of that enemies array and start moving all of those items down, based on the speed.
Okay, well how often is this being called? Well, let's take a look. For one, before I just wrap this up, I do want to make sure that this has :void, okay. So I don't want it to return anything or anything like that. It's just a common practice to add void there. And right up here makeEnemies, once they're made, I want to go ahead and moveEnemies like that. Okay, so that's when it's being called is right in here within playGame, which gets called plenty of times, okay.
So let's go ahead to see this in action. This is actually pretty exciting. So let's go to Control > Test Movie. Let's test it out. If I click Play, well, you should see all of those enemies start to march down, and here they come. Quite intimidating. In fact, there is not much I can do, especially since I can't fire. So that works pretty darn good. I really like how all of those enemies kind of come down. But really my next step with this is I want these enemies to sort of move at, sort of a random pace and kind of move from the left to the right and even tilt a little bit as if they're actually sort of coming through the atmosphere.
So that's the next step, but I'd say this is a great place to start, and now I can add individual movement to each one of these as well.
Get unlimited access to all courses for just $25/month.Become a member