Join Paul Trani for an in-depth discussion in this video Adding chance and randomness, part of Flash Professional CS5: Creating a Simple Game for Android Devices.
Let's take a look at where the game is at now. I'm going to do that by just going to do a test movie. I'll click Play. Notice that really my spaceship doesn't stand a chance against all of these UFOs. There's absolutely no way. So what I want to do is I want to create an element of chance, so maybe only a couple come down. Maybe a little more. Maybe a little less. It all just kind of depends. Another thing I want to do is for the little spaceships that come down, I want them to come down kind of wobbly and a little more random than they do now, as opposed to just straight down.
Okay, so that's the goal, and what I'm going to do in order to do this is I'm going to go ahead and use this Mobile Game folder, these code snippets, and I'm going to select Random Chance is the one I'm going to use. Let's just double-click on that. It adds it to my Actions frame and right in here, notice that it establishes a variable called level, which is a number. All my variables need to go to at the top, so I'll just cut that out and scroll up to the top and paste that variable in there, because depending on the level will determine how many of those little spaceships appear.
Okay, so level 1, where would I put this? Well, I would want to put this where the game initializes. So, let's just go ahead and take a look at that. So, I'll cut that and scroll up right where I initialize the game. Here we are, initialize the game between lines 60 and 70. It's where I create my players. And sure enough, right in here-- in fact, I'll just put it right above those other items, level 1.
Okay, so we're initially going to set it to level 1, and it's going to create the player, and it's going to create the enemies. And when I create the enemies, I want to drop this in, okay. I'm going to drop in this code where my enemies are created, so I'll cut that, scroll on up, and right in here where I create my enemies, that's actually where I want to add a little bit of an element of chance, okay. So I'm just going to paste that in. Okay, so I'm going to create this new variable, and it's called Chance.
It is a number, and it's going to be a random number between zero and 60. That's my chance number. And for that chance number, if it's less than or equal to one plus the level, then go ahead and do something. In this case, what I want to happen, that do something happens to be create an Enemy. There we are. So again, open curly brace encapsulates creating an enemy and then that closing curly brace, closes it.
So, I'm going to click this Auto Format button. We can see how it nests everything together. All right, so again, right in here, pick a number between zero and 60, and then right in here, if the number is less than or equal to one plus the level, so that could be two on up, okay.
So it's the equivalent, at least in this first case, of typing in 2 right there. It may seem like a pretty small number, but keep in mind that this make enemies function gets fired off 24 times every second, okay. So sure, it might be a small chance that this happens, but that's going to mean less enemies that are generated, and also keep in mind that it's going to still fire off 24 times every second. Okay, so that's all I need to do.
Just if you meet these requirements, go ahead and create an enemy, so let's run this. Click Play. That's more like it. This is manageable; I can probably shoot them down a lot easier. But now that they are less of them, I noticed they're pretty straightforward. They're coming straight down, and really I want to make them wobble a little bit. So let's go ahead and add some additional functionality to them. For the move enemies, I want to add a little bit more randomness than what I have right in here, okay.
So let's go ahead and take a look at the Code Snippets panel, because I want to have some random rotation X and even Y movement. So I'm going to double-click on that. It adds that code down at the bottom, and I'm going to go ahead and move this code right into place. So I'll cut it and then move it right into move Enemies right here, so just paste it on in there, and then I'll click the Auto Format button.
All right, let's go ahead and take a look at this now, because first off, what happens is it's automatically going to rotate the enemy between 10 and 5 degrees. Technically those are just going to be the numbers. Okay, but it's just going to go ahead and rotate it, and what is it rotating? It's rotating the tempEnemy, so I'll copy that and paste it in. So go ahead and rotate it any random number between 10 and 5. Rotate it that direction. Make sure you round it, so we land on a whole number.
Next up, find the rotation and move the X position in that direction, okay. So right in here, notice this looks pretty complex, and let's face it, it does look complex. Let me just paste in tempEnemy, right in here, for these instance names, and it looks a little bit daunting. But know that all that's really going on here is this is a degree, so it's getting this degree, whatever angle it's at, and then it's converting that to radians. Okay, so Flash uses radians, so that's why I need to have math sign.
But this chunk of code right here, all it does is return the angle in which I want this enemy to travel, okay. So let's not get too hung up on the fact that there is some math in there. That's what it's going to do. It's going to start moving that direction based on the rotation, and not only that, it's going to move it that tempEnemy speed, which is right up here as well as within this area where we actually make the enemy, okay. So that's where that comes from.
We're going to go ahead and move its X position based on the rotation. Not only that, we're going to move its Y position based on that rotation as well, and I'll just paste in where it says instance name here for all of those. Okay, keep in mind that I still have this tempEnemy Y, and I want to go ahead and remove that line as well. So remove that. This is my new movement, and that's exactly what's going to take place. In fact, the last thing I need to do is just to make sure that it stays within bounds.
Okay, so if it's off the stage, move it back onto the stage, whether it's on the left or the right side, and this would be the right side. Just remove some of these empty spaces like that. I would say we're looking pretty good here. In fact, I'll just click the Auto Format button. It doesn't throw me any errors. So let's go ahead and test this out. Click Play, and there he is coming down.
There are all kind of wobbliness, exactly what I want. It kind of seems like they're attacking me. I don't want to have any higher of a number because they could start traveling upwards if that's the case. You can see some more are to kind of traveling like upwards, which is fine, but again, they're trying to attack. So that's why I have that number between 5 and 10; that is the random number. But this is manageable. It seems pretty fun. I can always adjust the levels if I want maybe less of these enemies to come down, but it looks pretty good.
You can see how adding this element of chance and the randomness can really create some excitement for your game.
- Designing for mobile platforms
- Creating and optimizing game graphics
- Adding random movement and interactivity
- Moving players based on the accelerometer
- Using gestures
- Detecting collisions
- Implementing audio
- Adding scoring and levels
- Implementing a high score screen
- Publishing an app on both Mac and Windows
- Uploading to the Android Market