PDA

View Full Version : Pirate's Space Game Tutorial, Part 1 [Flash][Actionscript]



The Pirate
05-22-2007, 03:54 PM
http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/TitleImage.png


You guys asked for it, and here it is. The Pirate's big tutorial on making a space game in Flash. In part 1 of the tutorial I'm going to walk you through the absolute basics. By the end you will have a very simple little space game ready to be expanded on.

This part of the tutorial will be done in a few main sections:


Getting started
Constructing your spaceship
Equipping your weapon
Spawning enemy spaceships
Collision Detection
Finishing touches


Before we start, I should make something clear. This tutorial assumes that you are already familiar, and reasonably skilled with the Flash program itself. You should know things like, where the actionscript tab is, where the properties tab is, how to set an instance name, how to create and work with movie clips, and so on. I'm not going to explain precisely, step by step, which buttons to press and what to click on. You need to know what you're doing.

If you meet the above criteria, you're ready to start the tutorial.

Step 1 - Getting Started


We're going to start by making a major decision that will affect how you go about creating everything else in the game. Do we want our game to be a top scrolling, or a side scrolling game?

For my example game, I am going to be making a top scroller. the things you learn in this tutorial will allow you to create either one, you just need to use some common sense and change the code around.

Just remember this:

In Flash, and basically every program you will ever come across, positions are measured in X and Y values. The X value is the horizontal position, the Y value is the vertical postition. I'm going to assume you already have SOME knowledge of how coordinates work.

If for example, I am making an object move upwards, I may type some code that deals with the Y-value. If you are making a side scrolling shooter, you will probably want that same object to be travelling sideways in your game, so you use the same code that I type BUT use the X-value instead. Just use some common sense and play with the code so it works with what you're trying to do.

For reference, an X value of 0 is the very left edge of your stage, and a Y value of 0 is the very top of the stage.

Alright, so since I'm making a top scroller, I'm probably going to want my stage to be tall and narrow. You can make your game any size you want, in general, you want a side scroller to be a short wide rectangle, and the opposite for a top scroller.

I'm going to use a width of 300, and a height of 400.


http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/001.png

While I'm here, I also set the background to black, so it will look more like space. Obviously you don't HAVE to set your game in space. Draw an elaborate scrolling background if you want, be creative.

Next, we're going to set the frame rate. I use 30fps for games to make them run smoothly, you can use anything you want.

Now that the stage is set up, we're going to set two Global Variables. Global variable is the name I use to refer to variables on the main timeline, instead of in an object. You should have a single blank keyframe on your timeline, click on it, then open the Actionscript tab. Enter this code:


stagex = *the width you set for your game*;
stagey = *the height you set for your game*;

NOTE: I use "stagex" and "stagey" for simplicity, but you can name your variables anything you want. The name of the variable doesn't actually matter, since we decide what the variable does when we actually write code.

These variables will be infinitely useful if you want to change your game later on. For example, lets say the height of my game was 300. I have some code that makes enemies dissapear when they go past the 300 mark, to make sure they don't keep going when they go off the screen. Later on, I decide the game screen is too short, and want to make the game 500 pixels high instead.

That means I have to go to each enemy and change it's code to use 500 instead. But, if I'm using global variables in my code, it's as easy as clicking on the timeline, and changing the global.

Now that your stage is set up, and it's dimensions have been noted in your global variables, you're ready to start making the game.


Step 2 - Constructing Your Spaceship

Now that we have a rectangle of pixelated space to work with, we need a spaceship so we can fly around in it. It's time to use your insane Flash drawing skills. Draw the most badass space cruiser you can imagine.


http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/002.png

Here's mine, I opted for the sleek triangular look and painted it red to make it go faster. Remember to keep your spaceship reasonably small. Once you've drawn it, you'll want to convert it into a Movie Clip. All of the objects in our space game will be movie clips, that way we can add code to them.

And that's exactly what it's time to do. We've got to write some code to allow us to fly our new spaceship.

Select your spaceship and open the actions tab. Enter this code:


onClipEvent(load){
speed = 5;
}

"onClipEvent(load)" is a very important thing to remember. When working with Movie Clips, we will divide our code into two separate parts using the "onClipEvent" handlers. "onClipEvent(load)" is the code that runs only once when the object first loads. This is where we define all the variables that the object will use. In this case, we set a variable that will be the speed of our spaceship.

The second part of the code is contained in a section called "onClipEvent(enterFrame)". This is the code that loops over and over again. This is the main part of the code, where we include everything we want the object to do while it's running.

In this case, we want to be able to move the ship with the keyboard. So just below the code you just wrote, add this:


onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
if (Key.isDown(Key.UP)) {
_y -= speed;
}
if (Key.isDown(Key.DOWN)) {
_y += speed;
}
}


The code is pretty self explanitory. "if(Key.isDown)" checks to see if a certain key on the keyboard is pressed. _x and _y refer to the X and Y position of the object. All the code means is that, if the button is pressed, the object moves the amount of pixels we specified with our speed variable.

Adjust the speed variable in the first part of the code to determine how fast the ship will go.

I know, I know, the ship can fly off the screen, but that isn't important right now. We're going to set movement boundaries at the end in "Finishing Touches". As long as the ship can move, we're fine for now.

Now that you have a completed spaceship, the last step is to give it an instance name using the properties tab. This is the name that other pieces of code can use to refer to this object. I used the instance name player for simplicity.


Step 3 - Equipping Your Weapon

Flying around is great, but it isn't going to help us much when the evil spaceships attack us. We need to equip our badass space cruiser with some serious weaponry. It's time to use your drawing skills again, we're going to be drawing the laser/missile/torpedo/bomb/whatever that your spaceship is going to fire.


http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/003.png

Here's mine, a deadly green bolt of pure nuclear-electro-antimatter-protonic laser energy. If that's not enough to stop our enemies, I don't know what is. Remember to keep your drawing small, you don't want the shot to be bigger than your ship. Or maybe you do, that would kick a lot of ass I suppose. But keep it reasonable.

Now convert it to a Movie Clip, and give it the instance name shot. Now move it off the stage. Tuck it away where you wont see it during the game, and I'll explain why.

The Pirate has developed a method of making games where he creates "stock objects", places all the code they need inside them, and then moves them off the screen. Then, when an object such as a shot is needed, I use a code that makes a duplicate of the stock object wherever it is needed. That way, I can keep track of all my objects easily and "summon" copies of them onto the screen whenever one is needed.

By no means is this the "right" way to make a game. It's probably not even the best way either, but that's the way I make games. If you want to learn a different method, you're reading the wrong tutorial.

Now it's time to add the code to the shot itself. Click on it, open the actions tab, and write this:


onClipEvent (load) {
speed = 10;
}
onClipEvent (enterFrame) {
_y -= speed;
}


The code is entirely self explanitory. You choose a speed for the projectile, and it travels upwards at that speed.

The complicated code is the code that allows you to fire your weapon. First we need to define the variables used by the code. Click on your ship again, and add this code inside the existing onClipEvent(load).


shotnum = 10;
shotmin = 10;
shotmax = 20;
reload = 8;
nextshot = 0;

I'll explain what each of these variables does. First though, put the actual firing code inside the onClipEvent(enterFrame) section. The code is as follows:


if (Key.isDown(Key.SPACE)) {
if (nextshot == 0) {
nextshot = reload;
shotnum++;
_root.shot.duplicateMovieClip("shot"+shotnum, shotnum);
_root["shot"+shotnum]._x = this._x;
_root["shot"+shotnum]._y = this._y;
}
}
if (nextshot>0) {
nextshot--;
}
if (shotnum>=shotmax) {
shotnum = shotmin;
}

So what does all that mess mean? First things first, we check to see if our fire key is pressed, in this case the spacebar. Then we come to our first variable, nextshot. This represents the cooldown timer of the gun, we can only fire the gun when the cooldown is at 0. This prevents us from firing a continuous stream.

You may have noticed the double equal sign, == in the code. This is not a typo, it is very important. In Actionscript, we use a single equal sign to define a variable, and a double equal sign to make a comparison. In this case we are testing to see if nextshot equals zero, so we use a double. The code will not work with a single equal sign, try it and see.

Once the code has checked to make sure we are allowed to fire a shot, we come to the code that actually makes the projectile. nextshot = reload; will reset the cooldown timer to our reload variable. Changing the variable will give us a different reload time.

Shotnum++; is just a simple way of increasing the variable shotnum by 1. shotnum is what determines the depth of the shot we are about to create.

In Flash, all objects that are created by code (such as the duplicate we are about to make) have a depth. An objects depth is unique, and no two objects can have the same depth at any one time. By using a constantly increasing variable shotnum we ensure that none of our shots cancel eachother out.

Now the fun part, firing the shot. _root.shot.duplicateMovieClip("shot"+shotnum, shotnum); is the code that creates the duplicate.

_root.shot tells the code which object to duplicate, the _root. is just a way of indicating to Flash that the object exists on the main stage. If the object existed inside of another object called "box" for example, you would write _root.box.shot. This just tells Flash where the object is.

duplicateMovieClip creates a duplicate, with the parameters we specify in the brackets, ("shot"+shotnum, shotnum). The first parameter is the instance name of the new object. We want each projectile to have a unique instance name so we call it "shot" and add shotnum to the end of it. Make sure to include the quotation marks around "shot", this tells Flash that we are dealing with a string of text, and not a variable or an object.

The second parameter is the depth, we set it to our variable shotnum.

Next we have to place our new duplicate at the position of our ship. _root["shot"+shotnum]. is a way of referring to the duplicate we just created. Everything in the square brackets is treated as a single instance name. so ["someobject"+30] would refer to someobject30. Then we simply set the _x and _y of the duplicate to the position of our ship.

The final pieces of the code just regulate our variables.

if (nextshot>0) {
nextshot--;
}

Will reduce the cooldown timer if it is anything more than 0.

if (shotnum>=shotmax) {
shotnum = shotmin;
}

Will keep shotnum in between a certain range that we set ahead of time. This prevents shotnum from just increasing infinitely, and possibly cancelling out other objects that might be using that depth. For the example game, I've set a range of 10-20, allowing 10 shots to be on the screen at once. If you made your gun fire like a machine gun, you might need to have more than 10 shots on the screen. Adjust your min and max values accordingly.

TestMovie to try out your gun and make sure you did everything right. Then move on to the next step.


Step 4 - Spawning Enemy Spaceships

Now that you've got a powerful weapon mounted on your ship, you're probably itching to shoot at something. It's time to create a legion of enemies for you to fight. Start by once again designing some art, draw the meanest looking enemy spaceship you can imagine. I went for that retro Space Invaders look.


http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/enemy.png

Once again, convert it to a Movie Clip, give this one the instance name enemy.

Now, there are infinite possibilities when designing the enemies of your game. You could design enemies that fire lasers at you, floating space mines, giant boss ships, torpedo weilding death warships, anything you want. Obviously, I can't cover all that right now. Pirate's Space Game Tutorial, Part 2 will deal with extra fun stuff like enemies that shoot back, or dodge bullets.

The enemy we will be designing in this tutorial is the simplest type. The kamikaze enemy. We are going to program them to fly stupidly down the screen, and hope they hit you.

You should be able to figure the code out already, if you've been learning anything from this tutorial. We'll set up some variables, and then add movement code. Like this:


onClipEvent (load) {
speed = 4+random(5);
shield = 3;
}
onClipEvent (enterFrame) {
_y += speed;
}

To mix things up a little, I've made the enemies go at a random speed. I set 4 as the base speed, plus random(5). Remember that Flash counts from 0 when it chooses random numbers, so random(5) is really picking a number from 0-4.

Don't worry about the shield variable, that will be used later.

Now that we have our basic enemy, put it away offscreen like we did with the shot. We're going to duplicate this base enemy whenever we need to spawn one. To start spawning enemies, we're going to use a technique I always use. Draw a circle/square/scribble anything really, somewhere off screen and convert it to a movie clip. Give it an instance name like spawner.

This hidden movie clip offscreen is going to spawn enemies. You'll understand how in a bit. Click on the spawner and add this code to it:


onClipEvent (load) {
nextenemy = 0;
enemynum = 30;
enemymin = 30;
enemymax = 50;
}
onClipEvent (enterFrame) {
if (nextenemy == 0) {
nextenemy = 15+random(40);
enemynum++;
_root.enemy.duplicateMovieClip("enemy"+enemynum, enemynum);
_root["enemy"+enemynum]._x = random(_root.stagex);
_root["enemy"+enemynum]._y = -40;
}
if (nextenemy>0) {
nextenemy--;
}
if (enemynum>=enemymax) {
enemynum = enemymin;
}
}


I shouldn't have to explain much of this code, it's essentially the same as the code for firing shots. A variable called nextenemy counts down. When it reaches 0, it is reset to a random number. Then a duplicate is made of the original enemy.

_root["enemy"+enemynum]._x = random(_root.stagex); makes use of our global variable. _root is used again to tell Flash where the variable is found. Then the position of the enemy is set to a random _x value within the confines of our stage.

_root["enemy"+enemynum]._y = -40; places the enemy at -40y, just above the stage, so it can fly downwards. If your enemy is particularly tall, you may want to put it even farther away.

The next pieces of code are right from our projectile firing code. They regulate enemynum, which keeps the depth of our enemies between 30-50, so they don't interfere with our shots, which are in 10-20. Once again, if you want to make enemies spawn even more frequently, you may need to change some of these parameters. The code is easy to play with so it will work with your game.

Once again, TestMovie. If you did everything right, you should see enemies spawning and flying at you.


CHARACTER LIMIT, TUTORIAL CONTINUED IN NEXT POST.

The Pirate
05-22-2007, 03:55 PM
Step 5 - Collision Detection

So now, you desperately fire your weapon at the endless legions of enemy spaceships. Something's not right though. Your shots go right through them, they are entirely invincible! Failing to destroy them, you brace yourself as the kamikaze vessels rocket towards you. BUT, instead of a doom-bringing explosion you hear nothing at all. The enemy kamikaze has simply flown right through you, the enemy pilot looks back at you in confusion as he flies onwards.

Looks like the game need some Collision Detection in order to work. First, click on your projectile object and open up its actions again. Inside the onClipEvent(enterFrame) section, add the following code.


for (a=30; a<=50; a++) {
if (this.hitTest(_root["enemy"+a])) {
_root["enemy"+a].shield -= 1;
this.removeMovieClip();
}
}

This code contains two fun new functions. The first is for (a=30; a<=50; a++) {. This is what's known in computer programming jargon as a for loop. What that means is that anything inside of the curly brackets will execute a certain amount of times, as defined by the parameters in the bracket. I'll explain that in more depth.

The first thing in the brackets is a=30;. This creates a temporary variable called a which begins at 30. The second thing a<=50; means the loop will repeat over and over again as long as a is less than or equal to 50. The final a++ means that each time the loop repeats, a increases by one.

So this loop will run 20 times, for 20 different values of a. These values match up with the range we set for enemynum.

Next is a hitTest function. if (this.hitTest(_root["enemy"+a])) { is just checking to see if the shot has hit the object with the instance name "enemy"+a. If you're smart, you've probably caught on by now. We've designed a loop that will check for a collision with every possible different enemy name. If you set different values for enemynum, adjust the values of a accordingly and the code should work.

If the shot has collided with one of the potential enemies, we reduce the shield variable we set earlier. this.removeMovieClip(); makes the projectile delete itself, now that it's done it's job.

Now we need the enemy to explode when it's shield has reached 0. For that, we'll need to draw an explosion. Create a movie clip, and animate an explosion on the movie clip timeline. Give it the instance name explosion.


http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/004.png

For the sake of simplicity, I've just made a circular explosion, and tweened it to expand and fade out. You can do anything you like. Make sure the explosion is significantly larger than the enemy, it will help the effect.

Now on the last frame of the explosion animation, add this code.


this.removeMovieClip();

The explosion will now delete itself after it's done, err, exploding.

If you clicked TestMovie to try it out, you may have noticed nothing happens, and that the explosion does not get deleted. Don't worry, you didn't do anything wrong. In Flash, the removeMovieClip command will ONLY work if the object was created by code. Such as the duplicate objects that our spawner creates. We will be making duplicates of the explosion as well, and they will dissapear properly. The base explosion can not delete itself.

Now to make the enemy explode. Select your enemy object and add this code to the onClipEvent(enterFrame) section.


if (shield<=0) {
_root.expnum++;
_root.explosion.duplicateMovieClip("explosion"+_root.expnum, _root.expnum);
_root["explosion"+_root.expnum]._x = this._x;
_root["explosion"+_root.expnum]._y = this._y;
this.removeMovieClip();
}
if (_root.expnum>_root.expmax) {
_root.expnum = _root.expmin;
}

It's the familiar duplicating code, but this time, with a small twist. This example game is supposed to be expandable, once you're done following the tutorial you should be able to add more to the game with the amazing new knowledge that The Pirate has taught you. Since an explosion could be created by more than just this one enemy in the long run, we are going to use global variables to keep track of explosion depth.

You may have noticed things like _root.expnum throughout the code. By now, you know what _root does. expnum is the global variable we are going to use for explosion depths.

In order for this to work, we are going to have to define these new variables. Click on the keyframe in your main timeline where we defined stagex and stagey and add these variables to it:


expnum = 55;
expmin = 55;
expmax = 75;

You should have figured out what they do by now, nothing new here.

Now we have to set up a second hitTest to see if the kamikaze ships have hit your ship. Click the enemy ship again and add this code:


if (this.hitTest(_root.player)) {
_root.expnum++;
_root.explosion.duplicateMovieClip("explosion"+_root.expnum, _root.expnum);
_root["explosion"+_root.expnum]._x = this._x;
_root["explosion"+_root.expnum]._y = this._y;
this.removeMovieClip();
}

Basically the same as the code we just wrote. It creates an explosion, and deletes the enemy ship.

The enemys can't actually destroy your ship yet, we'll deal with that later. For now, just TestMovie and ensure that you can destroy the enemies, and that they explode when they hit you. Our simple game is starting to come together.


http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/005.png


Step 6 - Finishing Touches

All of the most important mechanics of our game are now in place. We have a controllable ship with a working weapon, spawning enemies, and collisions between all objects. It's time to add a few final things to make this simple example game complete.

First of all, an aesthetic thing. I'm tired of looking at a plain black background, so I'll add a scrolling starfield. For the sake of simplicity, a tweened starfield will do. Put the scrolling tween inside a new MovieClip and just throw it down as a backdrop. We wont be referring to the starfield in any code, so don't worry about instance naming it.


http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/006.png

Much better.

Now we need to finally do something about the ship movement. We need to restrict movement to our stage boundaries, so we can't fly off the screen. Add this code to your ship's onClipEvent(enterFrame) section. It's very self explanatory, so I wont explain each part of it.


if (_x>_root.stagex) {
_x = _root.stagex;
}
if (_x<0) {
_x = 0;
}
if (_y>_root.stagey) {
_y = _root.stagey;
}
if (_y<0) {
_y = 0;
}

Now to allow your ship to die. This problem is a bit trickier. As you read previously, a "base object" can never be deleted. We're going to need to create a single duplicate ship for you to use instead. But we don't want the base object to still be flying around shooting.

This code should solve that problem. Add this code "surrounding" all the rest of the code in onClipEvent(enterFrame). You'll understand what I mean by that in a second.


onClipEvent(enterFrame){
if (this._name == "player") {

*ALL THE CODE YOU TYPED PREVIOUSLY SHOULD BE HERE WITHIN THIS NEW SET OF CURLY BRACKETS*

}
}

If you did that right, TestMovie and everything should still work normally.

What this code says is that the rest of the code will ONLY run if the object has the instance name "player". We don't want the base object to do anything, so give it a new instance name. Instead of player call it playerbase. Move the base ship off screen with the other base objects.

Now click on your spawner, the thing that makes enemies appear. Add this code to onClipEvent(load) since we only want it to run once at the start.


_root.playerbase.duplicateMovieClip("player", 100);
_root.player._x = _root.stagex/2;
_root.player._y = _root.stagey-50;

You know this code by now. This time, instead of having to use a variable to track depth, we are only duplicating it once. So we simply use "player" as the new instance name, and a depth of 100.

Then we set the position to the exact middle of the screen, 50 pixels from the bottom. This code is great because it will place the ship properly no matter what size you set the stage to. If you have a different place in mind that you want the player's ship to start, use your own coordinates instead. Don't be afraid to play with the code to make it work for you.

Now that our ship is a duplicate object, we can delete it if necessary. You can add something fancy like a health bar, or multiple lives later. For now, lets just have our ship dissapear when it is struck by an enemy.

Go to our enemy collision code and make this addition:


if (this.hitTest(_root.player)) {
_root.expnum++;
_root.explosion.duplicateMovieClip("explosion"+_root.expnum, _root.expnum);
_root["explosion"+_root.expnum]._x = this._x;
_root["explosion"+_root.expnum]._y = this._y;
_root.player.removeMovieClip(); <--- add this new line
this.removeMovieClip();
}

Make sure to add the new bit of code ABOVE the code that says this.removeMovieClip();. It's just common sense, once a Movie Clip has been told to delete itself, it probably wont get around to running any other functions after that. Since it no longer exists and such.

Now as a final little addition that you will probably want to have in your game, lets add a score box to keep track of our kills. First use the text tool and write KILLS: or SCORE: or SEXYNESS LEVEL: or something like that.

Then, create another text box, but this time change it to Dynamic Text using the properties tab. Dynamic text lets you keep track of variables during the game, and display them.

Where it says Var: type _root.kills.


http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/007.png

We'll use a new global variable to keep track of our kills. Then of course, we have to define this new global. You know how to do it by now. Click that keyframe in your main timeline and add Kills = 0; to the list.

Then click on your enemy and make this addition to his code:


if (shield<=0) {
_root.expnum++;
_root.explosion.duplicateMovieClip("explosion"+_root.expnum, _root.expnum);
_root["explosion"+_root.expnum]._x = this._x;
_root["explosion"+_root.expnum]._y = this._y;
_root.kills++; <--- add this
this.removeMovieClip();
}

If you don't know what that does by now you fail Pirate's class. Go sit in the corner.


Guess what?

WE'RE DONE!


http://i70.photobucket.com/albums/i92/jayshostaccount/TUTORIAL%20IMAGES/008.png

Yep, our absolute basic shell of a space shooter is now complete. Not much of a game on it's own, but the basic mechanics are all there. Now it's time for you to expand on it. If you need inspiration or hints, try some of these "challenges". All of these can be accomplished using code you learned in this tutorial. You were learnding right? Not just copy-pasting and moving on I hope.

Challenges


Adding a health variable to the player so you can take multiple shots before dying
Adding multiple lives, and some code to respawn your ship
Adding a second enemy type, remember to manage your depth ranges
Add a game over screen and an option to play again
Add powerups to the game, use collision detection code to "collect" them
Make a scripted starfield with randomly generated stars instead of a tweened one. Remember depth ranges!
Add a "smart bomb" or some kind of second weapon to your ship, fired with a separate key


And those are just a few ideas to get you started. The Pirate will also be releasing Part 2 to this tutorial eventually. It will cover some more advanced fun stuff.

And that concludes Part 1 of this tutorial. I hope it was useful to you in some way or another.


HERE (http://rapidshare.com/files/32841143/ExampleGame.fla.html) is the .FLA of my finished example game from the tutorial. If you run into trouble, use it as a reference.

The Pirate
05-22-2007, 05:37 PM
My gun doesn't fire.

This code:
if (Key.isDown(Key.SPACE)) {
if (nextshot == 0) {
nextshot = reload;
shotnum++;
_root.shot.duplicateMovieClip("shot"+shotnum, shotnum);
_root["shot"+shotnum]._x = this._x;
_root["shot"+shotnum]._y = this._y;
}
}
if (nextshot>0) {
nextshot--;
}
if (shotnum>=shotmax) {
shotnum = shotmin;
}

Goes in the spaceship, right?

Make sure it's within "onClipEvent(enterFrame)". Also, if you were using your own variables or instance names, make sure to add those to the code in place of the ones I used.

Also, I'll upload the finished product right now. I forgot about that.

stick puppet
05-22-2007, 08:26 PM
My space ship moves but I have 8 syntax errors with the "shot".

I added this code to the "shot":



onClipEvent (load) {
speed = 10;
}
shotnum = 10;
shotmin = 10;
shotmax = 20;
reload = 8;
nextshot = 0;
onClipEvent (enterFrame) {
_y -= speed;
}
if (Key.isDown(Key.SPACE)) {
if (nextshot == 0) {
nextshot = reload;
shotnum++;
_root.shot.duplicateMovieClip("shot"+shotnum, shotnum);
_root["shot"+shotnum]._x = this._x;
_root["shot"+shotnum]._y = this._y;
}
}
if (nextshot>0) {
nextshot--;
}
if (shotnum>=shotmax) {
shotnum = shotmin;
}


Is that right?????

Arch-Angel
05-22-2007, 09:47 PM
this is mine so far
http://www.swfup.com/file/10781

how d i make it so the missile doesn't automatically shoot in the beginning and that it is aligned with the center of the ship while shooting

axcho
05-22-2007, 09:58 PM
Very nice. Like the humor in it. :)

Arch-Angel
05-22-2007, 10:11 PM
Mine is F'd up and has A LOT of bugs

http://www.swfup.com/file/10782

nd Pirate, I already gave stick puppet the right codes

but if you can help me on my bugs. Here's the list i got

Level #0: Frame=1
Movie Clip: Frame=1 Target="_level0.shot"
Movie Clip: Frame=1 Target="_level0.shot.instance1"
Shape:
Movie Clip: Frame=1 Target="_level0.enemy"
Movie Clip: Frame=1 Target="_level0.enemy.instance2"
Shape:
Movie Clip: Frame=1 Target="_level0.spawner"
Shape:
Movie Clip: Frame=1 Target="_level0.explosion"
Shape:
Movie Clip: Frame=1 Target="_level0.player"
Shape:
Movie Clip: Frame=1 Target="_level0.enemy31"
Movie Clip: Frame=1 Target="_level0.enemy31.instance3"
Shape:
Movie Clip: Frame=1 Target="_level0.enemy32"
Movie Clip: Frame=1 Target="_level0.enemy32.instance4"
Shape:

stick puppet
05-23-2007, 01:35 AM
Yay almost finished mine!!!

The Pirate
05-23-2007, 04:19 AM
When it says to put something inside the "onClipEvent(whatever)" section, you need to make sure it's inside the curly brackets.

So like:


onClipEvent(load){

*CODE GOES HERE*

}
onClipEvent(enterFrame){

*CODE GOES HERE*

}

If you get stuck, I posted a rapidshare link to my example .FLA. Use that as a reference.

Slayer
05-23-2007, 06:28 AM
Nice tutorial.

Arch-Angel
05-23-2007, 01:14 PM
The part I got stuck at was the Boundary so you couldn't go off screen, and the one that makes your shif re spawn or whatever. It's right there after the boundary thing. And No offense pirate (don't know if that last post was directed at any one specifically), but I did figure that out =D I feel special. lol.


okay, I fixed my boundary. Now for the base ship.

Saline
05-23-2007, 02:12 PM
Hey, could you explain animation on key press? I was making a game, had the movement actionscript up. Then I wanted it to play an animation while it walks. So I did

onClipEvent(enterframe){
if(Key.isDown(Key.RIGHT)){
player.gotoAndStop(Z)
_x += 5
else(gotoAndPlay(1))
}
}
It wou;dn't work with the whole code there.


onClipEvent(enterFrame){
if(Key.isDown(Key.SPACE)){
Ninjar.gotoAndPlay(6)
}
if(Key.isDown(Key.RIGHT)){
Ninjar.gotoAndStop(12)
_x += 5
}
else(Ninjar.gotoAndPlay(1))
if(Key.isDown(Key.LEFT)){
Ninjar.gotoAndStop(13)
_x -= 5
}
else(Ninjar.gotoAndPlay(1))
}
Is my whole code.

There is no errors with the code. I don't know what's going on though.

Arch-Angel
05-23-2007, 03:13 PM
http://i115.photobucket.com/albums/n289/TheBlindSamurai/prob.jpg

My boundaries are f'd up.

And DA. I think "On Key Press" is basically a way the actionscript tells the animation or whatever, that if you hit a certain key, it does your selected transation. I don't know if that's what u wanted, but you'd prolly want to hear the answer from Pirate

Apples
05-23-2007, 05:52 PM
Mine wont work on the shooting part.

Greenfire
05-23-2007, 08:09 PM
I have a problem, My ship shoots it's lazor, but if you hold space, it just stays in one spot. Also, it will only ever have 1 on screen, even though I have it set to 10.

Arch-Angel
05-23-2007, 08:38 PM
I don't think u guys are experienced enough in AS for this, Cept DA. Mainly for the moving and missiles. Because the tutorial is very clear on what to do and stuff. I can understand about the respawning ships thing, cuz that is tricky and to me, not well explained. Non the less its a great tutorial. I would like if you could explain the ship spawn part a little more, because when you said that the "remove clip" thing would remove it and it wouldn't do anything else with the movie clip, I looked at my ships AS and it has more than 1 part of AS like that. It confuses me.

All others having problems. Read what it says. And when it tells you to put AS in a '}', you simply go to the line with the '}', hit enter, and on the new line under the '}', paste the AS. for the shotmin and stuff. you'd go to the line with the shotmin or speed (the lowest line of it), hit enter and paste the AS under it.

Another good help thing is to not just read where to put your AS, copy it and paste it. Read what each part of it does so you learn what you'll need when making a game by yourself, and you'll know how to fix an AS problem yourself.

The Pirate
05-24-2007, 04:19 AM
I can understand about the respawning ships thing, cuz that is tricky and to me, not well explained. Non the less its a great tutorial. I would like if you could explain the ship spawn part a little more, because when you said that the "remove clip" thing would remove it and it wouldn't do anything else with the movie clip, I looked at my ships AS and it has more than 1 part of AS like that. It confuses me.

Wasn't quite sure what you were talking about here. Can you show me what part of the code you were having trouble with?


DA (Archangel, whatver) try this.


onClipEvent (enterFrame) {
gotoAndStop(1);
if (Key.isDown(Key.SPACE)) {
gotoAndPlay(6);
}
if (Key.isDown(Key.RIGHT)) {
gotoAndStop(12);
_x += 5;
}
if (Key.isDown(Key.LEFT)) {
gotoAndStop(13);
_x -= 5;
}
}


@Greenfire - Read my explanation of depths and how to set the depth range. It sounds to me like you've set the depths to only allow one value of shotnum.

Arch-Angel
05-24-2007, 04:22 AM
onClipEvent(enterFrame){
if (this._name == "player") {

*ALL THE CODE YOU TYPED PREVIOUSLY SHOULD BE HERE WITHIN THIS NEW SET OF CURLY BRACKETS*

}
}


that. The thing that lets u die or something

Scorpioxxx
05-24-2007, 11:23 AM
I found the single weapon thind boring but when i added another it went odd i have a MC with the instance name mini yet when i press control it duplicats the 'shot' MC instread, also, there arn't any errors, heres the code;

onClipEvent (load) {
speed = 5;
shotnum = 10;
shotmin = 10;
shotmax = 15;
reload = 25;
nextshot = 0;
shotmini = 10;
shotminimin = 10;
shotminimax = 30;
minireload = 3;
mininextshot = 0;
}
onClipEvent (enterFrame) {
if (this._name == "player") {
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
if (Key.isDown(Key.UP)) {
_y -= speed;
}
if (Key.isDown(Key.DOWN)) {
_y += speed;
}
if (_x>_root.stagex) {
_x = _root.stagex;
}
if (_x<0) {
_x = 0;
}
if (_y>_root.stagey) {
_y = _root.stagey;
}
if (_y<0) {
_y = 0;
}
if (Key.isDown(Key.SPACE)) {
if (nextshot == 0) {
nextshot = reload;
shotnum++;
_root.shot.duplicateMovieClip("shot"+shotnum, shotnum);
_root["shot"+shotnum]._x = this._x;
_root["shot"+shotnum]._y = this._y;
}
}
if (nextshot>0) {
nextshot--;
}
if (shotnum>=shotmax) {
shotnum = shotmin;
}
if (Key.isDown(Key.CONTROL)) {
if (mininextshot == 0) {
mininextshot = minireload;
shotmini++;
_root.shot.duplicateMovieClip("mini"+shotmini, shotmini);
_root["mini"+shotmini]._x = this._x;
_root["mini"+shotmini]._y = this._y;
}
}
if (mininextshot>0) {
mininextshot--;
}
if (shotmini>=shotminimax) {
shotmini = shotminimin;
}
}
}

The Pirate
05-24-2007, 12:35 PM
_root.shot.duplicateMovieClip("mini"+shotmini, shotmini);

This line of code here is your problem. _root.shot is telling Flash which object to duplicate, and you still have it set to the normal shot. I'm assuming you made a second object called "minishot" or something, so write:

_root.minishot.duplicateMovieClip("mini"+shotmini, shotmini);

Arch-Angel
05-24-2007, 12:53 PM
that. The thing that lets u die or something
lol. I got a name change


onClipEvent(enterFrame){
if (this._name == "player") {

*ALL THE CODE YOU TYPED PREVIOUSLY SHOULD BE HERE WITHIN THIS NEW SET OF CURLY BRACKETS*

}
}

thats the AS im talking about (2nd page)

Scorpioxxx
05-24-2007, 01:01 PM
_root.shot.duplicateMovieClip("mini"+shotmini, shotmini);

This line of code here is your problem. _root.shot is telling Flash which object to duplicate, and you still have it set to the normal shot. I'm assuming you made a second object called "minishot" or something, so write:

_root.minishot.duplicateMovieClip("mini"+shotmini, shotmini);
Missed that, i should get glasses.....

The Pirate
05-24-2007, 01:05 PM
thats the AS im talking about (2nd page)

Alright, that part of the tutorial just meant to put that new set of brackets, if(this._name == "player"){, around all the other code in onClipEvent(enterFrame). The reason for doing that, was so when we create a duplicate of the players ship, the base object wont still be flying around and running its code.

The code that actually lets you die is here:


if (this.hitTest(_root.player)) {
_root.expnum++;
_root.explosion.duplicateMovieClip("explosion"+_root.expnum, _root.expnum);
_root["explosion"+_root.expnum]._x = this._x;
_root["explosion"+_root.expnum]._y = this._y;
_root.player.removeMovieClip(); <--- add this new line
this.removeMovieClip();
}

This code is found in the enemy movie clip. It simply uses removeMovieClip(); to delete the player's ship.

Does that help at all?

Arch-Angel
05-24-2007, 01:16 PM
so all I have to add is brackets? or "if(this._name == "player"){

The Pirate
05-24-2007, 04:18 PM
so all I have to add is brackets? or "if(this._name == "player"){


onClipEvent(enterFrame){
if (this._name == "player") {

*ALL THE CODE YOU TYPED PREVIOUSLY SHOULD BE HERE WITHIN THIS NEW SET OF CURLY BRACKETS*

}
}

Like this, you just put if(this._name == "player"){ right below onClipEvent(enterFrame){, and add an extra } curly bracket at the end. The rest of the code you entered is within the new set of brackets, that way it will only execute if the object has the proper instance name.

Arch-Angel
05-24-2007, 04:32 PM
do i duplicate my ship? Because i did that and named one base ship

The Pirate
05-24-2007, 05:42 PM
do i duplicate my ship? Because i did that and named one base ship

Oh I see where you're getting confused now.

All of the "duplicates" are created by the code, the same code used to make the lasers shoot, and the enemies spawn. You don't physically copy and paste the ship to make a duplicate. You move the ship off the screen entirely, it IS the base ship. The new duplicate ship is created by some code in the next step.

Arch-Angel
05-24-2007, 06:04 PM
Oh, so I just move the ship off the screen. do I still need to change the name to "playerbase"?

also, my boundaries are messed up. You can leave the stage on the right side


1 last problem. Then it's onto the "kills" box and making finishing touches.

this
if (this.hitTest(_root.player)) {
_root.expnum++;
_root.explosion.duplicateMovieClip("explosion"+_root.expnum, _root.expnum);
_root["explosion"+_root.expnum]._x = this._x;
_root["explosion"+_root.expnum]._y = this._y;
_root.player.removeMovieClip(); <--- add this new line
this.removeMovieClip();
}

where do i add that onto my collision code, which should be in my enemy right?
but about that code. Do i only add _root.player.removeMovieClip();




-to everyone else. A nifty trick i discovered. It's pretty obvious. Seeing as how your enemies are movie clips, you can add like fire coming out of the rockets or something, to give it a little more feel or something like that.

Sorry, another problem. I only have my "playerbase" movieclip. I moved it off the stage. when i start my game everythings fine, but what's this, after 5 seconds another ship flies in and is like attatched to you on an invisible cord

http://www.swfup.com/file/10942
thats my game so far.

stick puppet
05-25-2007, 03:40 AM
Here is mine!
http://i158.photobucket.com/albums/t100/Dino_Dylan/game01.swf


How do you make a respawn ship button or key?

And one more how do you add lives?????

Steyene
05-25-2007, 05:09 AM
http://www.swfup.com/uploads/swf-10978.swf
Mine... still trying to fix the weapon... it fires from way out in front... no errors....

Scorpioxxx
05-25-2007, 09:11 AM
http://www.swfup.com/file/10994

Controls;

ARROWS - Move player
SPACE - Cannon (reload 25) damage 5
CONTROL - machinegun/minigun (reload 3) damage 1
ENTER - Laza (reload 100) damage 25

Still trying to add lives/respawn.....

The Pirate
05-25-2007, 12:27 PM
Mine... still trying to fix the weapon... it fires from way out in front... no errors....

Looks pretty cool. I like how you added your own code (the barriers) instead of just following the tutorial exactly and stopping there. That's what I was hoping people would do, since the game from the tutorial itself is pretty bare on its own. Same to Scorpio, the multiple weapons are a cool idea.

Anyways, I think the problem might have to do with the center point of your movie clips. When you create a Movie Clip you might have noticed the little cross shape that shows up when you click on them. Flash treats that cross as the exact center of the movie clip, and when it is moving the laser to your ship's position, what you are really telling flash to do is line up the two crosses.

I'm guessing either your ship is too far down from the center point, or your laser is too far up. Double click the Movie Clips to check.

Arch-Angel
05-25-2007, 02:15 PM
Oh, so I just move the ship off the screen. do I still need to change the name to "playerbase"?

also, my boundaries are messed up. You can leave the stage on the right side


1 last problem. Then it's onto the "kills" box and making finishing touches.

this

where do i add that onto my collision code, which should be in my enemy right?
but about that code. Do i only add _root.player.removeMovieClip();




-to everyone else. A nifty trick i discovered. It's pretty obvious. Seeing as how your enemies are movie clips, you can add like fire coming out of the rockets or something, to give it a little more feel or something like that.

Sorry, another problem. I only have my "playerbase" movieclip. I moved it off the stage. when i start my game everythings fine, but what's this, after 5 seconds another ship flies in and is like attatched to you on an invisible cord

http://www.swfup.com/file/10942
thats my game so far.

And I gree pirate. Im gonna expand my game after Im done reading through the tutorial.

Also, whay type stuff are you gonna add in the 3rd one?

Steyene
05-25-2007, 05:25 PM
cool thanks pirate
*goes and checks*
EDIT: yeap it works, thank Pirate... now I have just got to try and get the hitTest for the bullet against the shelter to work....
EDIT: Got it working now.... one wrong symbol...>_<
http://www.swfup.com/file/11049

Scorpioxxx
05-26-2007, 03:08 AM
cool thanks pirate
*goes and checks*
EDIT: yeap it works, thank Pirate... now I have just got to try and get the hitTest for the bullet against the shelter to work....
EDIT: Hey Pirate, the bullet/shelter hit test for the isn't working, nor is its shield any help/ideas?
So Far
http://www.swfup.com/file/11049

Post the code, its easier for anyone looking at the thread to find the problem

general_zim
05-26-2007, 06:40 AM
This is by far the best tutorial I've ever read. It's understandable, and it's well written, and everything is explained very well, no vague descriptions what so ever.


But I do have a question:

Could this engine work for a guitar hero like game?

Scorpioxxx
05-26-2007, 06:52 AM
This is by far the best tutorial I've ever read. It's understandable, and it's well written, and everything is explained very well, no vague descriptions what so ever.


But I do have a question:

Could this engine work for a guitar hero like game?

Some of the code could be pulled apart and become usefull for something like that.....

Freddeh
05-26-2007, 02:54 PM
Really nice tut, Pirate.

Gaarrrrr...

The Pirate
05-26-2007, 03:26 PM
This is by far the best tutorial I've ever read. It's understandable, and it's well written, and everything is explained very well, no vague descriptions what so ever.


But I do have a question:

Could this engine work for a guitar hero like game?

Well, this tutorial covered a lot of fundamentals like variables, MovieClips, depth, and hitTests that can be used in pretty much any sort of game.

But, using this tutorial as your sole resource for a different genre of game probably isn't the best idea.

Scorpioxxx
05-26-2007, 03:45 PM
http://www.swfup.com/file/11135
Right, i got health and lives but i havn't got a game over screen left, when i use a code like;

if (root.lives <=0) {
goto (2)
}

It should go to frame 2 when the lives variable text is at 0, it has the instance name 'lives' and the variable 'root.lives' help?

Steyene
05-26-2007, 07:00 PM
@Pirate. I have been trying to spawn my shelter once at the start of the game, like you do with the ships, but it only displays either the shelter or the ship never both. Any help/ideas would be great.

The Pirate
05-26-2007, 07:06 PM
@Pirate. I have been trying to spawn my shelter once at the start of the game, like you do with the ships, but it only displays either the shelter or the ship never both. Any help/ideas would be great.

Make sure they are using separate depths.

Once you start getting a lot of objects in the game, it's good to keep track of their depths so you don't screw things up. I usually just make a notepad file and write them all down.

Steyene
05-26-2007, 07:11 PM
Oh ok thanks heaps... yeap it works... now just have got to get the random enemy types nailed...

silent assassin
05-27-2007, 05:56 PM
Um..MY GUN SHOOTS BACKWARDS!!!! WTF?!?!?!

im making a sidescroller..and my common sense isn't that good i suppose...heres the code


onClipEvent(load){
speed = 10;
shotnum = 10;
shotmin = 10;
shotmax = 20;
reload = 8;
nextshot = 0;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
if (Key.isDown(Key.UP)) {
_y -= speed;
}
if (Key.isDown(Key.DOWN)) {
_y += speed;
}

if (Key.isDown(Key.SPACE)) {
if (nextshot == 0) {
nextshot = reload;
shotnum++;
_root.shot.duplicateMovieClip("shot"+shotnum, shotnum);
_root["shot"+shotnum]._x = this._x;
_root["shot"+shotnum]._y = this._y;
}
}
if (nextshot>0) {
nextshot--;
}
if (shotnum>=shotmax) {
shotnum = shotmin;
}
}

The Pirate
05-27-2007, 07:07 PM
Um..MY GUN SHOOTS BACKWARDS!!!! WTF?!?!?!

im making a sidescroller..and my common sense isn't that good i suppose...heres the code

Clearly not.

If your shot moves backwards, just reverse it's direction. Set the speed to a negative number.

silent assassin
05-28-2007, 08:26 AM
or i could have used ma mega coding skillz and noticed that u used "_y -= speed " in the shot's code so i thought all i had to do is chang it to "x". That was wrong i also had to chang the + to a -. HA coding skillz beats common sense lol... I just wanted to show that to any siderscroller makers.

Scorpioxxx
05-28-2007, 09:44 AM
Um..MY GUN SHOOTS BACKWARDS!!!! WTF?!?!?!

im making a sidescroller..and my common sense isn't that good i suppose...heres the code


onClipEvent(load){
speed = 10;
shotnum = 10;
shotmin = 10;
shotmax = 20;
reload = 8;
nextshot = 0;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
if (Key.isDown(Key.UP)) {
_y -= speed;
}
if (Key.isDown(Key.DOWN)) {
_y += speed;
}

if (Key.isDown(Key.SPACE)) {
if (nextshot == 0) {
nextshot = reload;
shotnum++;
_root.shot.duplicateMovieClip("shot"+shotnum, shotnum);
_root["shot"+shotnum]._x = this._x;
_root["shot"+shotnum]._y = this._y;
}
}
if (nextshot>0) {
nextshot--;
}
if (shotnum>=shotmax) {
shotnum = shotmin;
}
}


or i could have used ma mega coding skillz and noticed that u used "_y -= speed " in the shot's code so i thought all i had to do is chang it to "x". That was wrong i also had to chang the + to a -. HA coding skillz beats common sense lol... I just wanted to show that to any siderscroller makers.
You are aware that that part or code is for the player movement and your 'side scroller' has pointless code, i bolded it for you.

silent assassin
05-28-2007, 09:54 AM
i didn't mean a sidescroller exactly...i meant his game...but sideways.... so that code does have a point...

i also found out that the code was wrong for "shot". It was _y -= speed

its supposed to be _x += speed

The Pirate
05-28-2007, 01:10 PM
i didn't mean a sidescroller exactly...i meant his game...but sideways.... so that code does have a point...

i also found out that the code was wrong for "shot". It was _y -= speed

its supposed to be _x += speed

A top scroller sideways IS a side scroller you silly wanker.

silent assassin
05-28-2007, 04:41 PM
Some people consider a side scroller as a Mario type game. Since its not...there im only agreeing with u...

Gavel
05-29-2007, 12:14 PM
Alright, I was cruising along nicely with this until I got to the part where you add the code to make your ship die.

This code:

onClipEvent(enterFrame){
if (this._name == "player") {

*ALL THE CODE YOU TYPED PREVIOUSLY SHOULD BE HERE WITHIN THIS NEW SET OF CURLY BRACKETS*

}
}

I've been putting that code like this on my "player" movie clip:

onClipEvent(enterFrame){
if (this._name == "player") {
onClipEvent (enterFrame) {if (_x>_root.stagex) {
_x = _root.stagex;
}
if (_x<0) {
_x = 0;
}
if (_y>_root.stagey) {
_y = _root.stagey;
}
if (_y<0) {
_y = 0;
}if (Key.isDown(Key.SPACE)) {
if (nextshot == 0) {
nextshot = reload;
shotnum++;
_root.shot.duplicateMovieClip("shot"+shotnum, shotnum);
_root["shot"+shotnum]._x = this._x;
_root["shot"+shotnum]._y = this._y;
}
}
if (nextshot>0) {
nextshot--;
}
if (shotnum>=shotmax) {
shotnum = shotmin;
}
if (Key.isDown(Key.LEFT)) {
_x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
_x += speed;
}
if (Key.isDown(Key.UP)) {
_y -= speed;
}
if (Key.isDown(Key.DOWN)) {
_y += speed;
}
}
}
}

The bolded parts are the new code I put in.

I'm not sure how I'm misunderstanding it.

Oh, and here's my game. You might've noticed that the gunshot isn't firing at the center of the car. Well that's the way I want it, considering the dude's hand is leaning out the window. But the explosions for the enemies are way off. The enemy dissapears and the explosions pops up to far back and to the right.

width=400 height=500
http://www.swfup.com/uploads/swf-11478.swf

Yes, I didn't make it space themed. It's called "breaking away from the norm", or something like that. I wanted to do something different than everyone else.

Short1
05-29-2007, 03:46 PM
This is totally cool. : D
I think I'll make a sort of driving game in a desert type background with a dune buggy.

Steyene
05-30-2007, 02:08 AM
@Spazzy-Z. I think its because you forgot to code the hit tests between the enemies and the player.

Rather Cheesy
08-05-2007, 08:21 PM
awesome tut pirate, helped me tons, never knew how to duplicate MC's before now

9/10 cause theres no part 2 lol

Gavel
08-23-2007, 05:59 PM
mine dosent shoot. could sombody give me the exact code?

He did give you the exact code (not that this is the only way to do this). You obviously didn't follow the tut properly. Maybe you should actually learn from it and try to understand actionscript so you can see what you're doing wrong.

pagdog9
09-01-2007, 07:39 AM
I have the code and i clicked check sintaxis. It says i has no errors but when i shoot my enemy it doesn´t die

Scorpioxxx
09-01-2007, 09:09 AM
Have you put in that when the shield/hp/whatever the variable is = 0 or <1 to remopve the movieclip (enemy) an also make sure you even put in to remove a number from the variable when they collide.

pagdog9
09-01-2007, 01:56 PM
i cant make me die

Gavel
09-01-2007, 02:24 PM
i cant make me die

You obviously didn't read the tutorial properly. Maybe if you actually try to learn from it and understand the actionscript so you can figure out what you did wrong.

Steyene
09-01-2007, 05:11 PM
OMG copy pasta. Yours working now spazzy?

pokit5
09-04-2007, 07:56 PM
my spawner only spawns in one spot. all the enemies just come like a waterfall at once. code-

onClipEvent (load) {
nextenemy = 0;
enemynum = 30;
enemymin = 30;
enemymax = 50;
}
onClipEvent (enterFrame) {
if (nextenemy == 0) {
nextenemy = 15+random(40);
enemynum++;
_root.enemy.duplicateMovieClip("enemy"+enemynum, enemynum);
_root["enemy"+enemynum]._x = random(_root.stagex);
_root["enemy"+enemynum]._y = -40;
}
if (nextenemy>0) {
nextenemy--;
}
if (enemynum>=enemymax) {
enemynum = enemymin;
}
}



no errors on output .any problem?

Scorpioxxx
09-05-2007, 03:16 PM
In the keyframe have you got a/the 'stagex =' variable.

killervirus
01-12-2008, 11:18 AM
This tutorial is awesome. However I've got two questions:
1.: When part 2 will be done ? :)
2.: How should I add more enemy types? Like a mothership or advanced enemies.

killervirus
01-12-2008, 01:59 PM
Sorry for double posting but I figured out how to add another enemy type and it can be interesting for the others:

First step:

View the actions of the spawner in the onClipEvent(load) segment add this:



nextenemy2 = 0;
enemy2num = 60;
enemy2min = 60;
enemy2max = 80;


Second step: Still in the spawner add this bit of code but now in the enterFrame segment, after the original code:



if (nextenemy2 == 0) {
nextenemy2 = 15+random(40);
enemy2num++;
_root.enemy2.duplicateMovieClip("enemy2"+enemy2num,enemy2num);
_root["enemy2"+enemy2num]._x = random(_root.stagex);
_root["enemy2"+enemy2num]._y = -40;
}
if (nextenemy2>0) {
nextenemy2--;
}
if (enemy2num>=enemy2max) {
enemy2num = enemy2min;
}


Third step:

on the shot mc add this code, after the original, but still in the enterFrame segment:



for (a=60; a<=80; a++) {
if (this.hitTest(_root["enemy2"+a])) {
_root["enemy2"+a].shield -= 1;
this.removeMovieClip();
}
}


Fourth step:

Draw another enemy type and name the instance of it enemy2 on the actions of this mc add these (I made it slower but equipped with stronger shield)



onClipEvent (load) {
speed = 1+random(5);
shield = 5;
}
onClipEvent (enterFrame) {
_y += speed;
if (shield<=0) {
_root.expnum++;
_root.explosion.duplicateMovieClip("explosion"+_root.expnum, _root.expnum);
_root["explosion"+_root.expnum]._x = this._x;
_root["explosion"+_root.expnum]._y = this._y;
_root.kills++;
this.removeMovieClip();
}
if (_root.expnum>_root.expmax) {
_root.expnum = _root.expmin;
}
if (this.hitTest(_root.player)) {
_root.expnum++;
_root.explosion.duplicateMovieClip("explosion"+_root.expnum,_root.expnum);
_root["explosion"+_root.expnum]._x = this._x;
_root["explosion"+_root.expnum]._y = this._y;
_root.gotoAndStop(2);
_root.player.removeMovieClip();
this.removeMovieClip();
}
}


Done. I hope it works for everyone.

Edit: There is a bug in it (at least for me) sometimes the advanced enemies just disappear, without even shooting on them. Ideas?