Posted tagged ‘hitTest’

The simple hitTest code, explained, and a lil’ bit improved.

August 5, 2008

Hi, I’ve seen many questions by many beginners asking about hitTest. Their questions are usualy about some simple hitTest codes, and I’m gonna explain for you how everything works in this tutorial.

WHAT IS HITTEST?

hitTest is a command used in actionscript to check if one object touches another. This is a very well used code, as it’s used in about every game. It can be used to create walls, powerups, traps or anything else that will do any command when the specified object touches it.

SO, WHAT’S THE CODE?

Um, no copy/paste, ok? This tutorial is meant for teaching you, if you copy/paste, you might get the code, but you will NOT understand it!

But the code is still here, and it’s explained too. It should be put on the enemy MC.

onClipEvent (enterFrame) { //When you enter the frame, code will run itself over and over again.
if (this.hitTest(_root.player)) { //Checks if it touches the player MC, in case it does, the following command will be executed...
_root.life -= 15;//You'll lose 15 health
}
}

Now, this isn’t the code I use normally, as it only checks the object rectangle, which means, if your MC is any other shape than a rectangle, the hitTest will be oversensitive. If you want to see the object rectangle, click on a MC with the selection tool. You will now understand, the player doesn’t have to touch the actual object, only that rectangle. This is why that code is a bad code…

THE CODE I DO RECOMMEND:

Well, let’s look at a better code. Put it on the player MC this time. And, no copy/paste!

onClipEvent (enterFrame) {//When you enter the frame, code is running itself over and over again
if (_root.enemy.hitTest(_x, _y, true)) {//The better hitTest, it do actually check if it touches the actual shape! enemy is the instance of the enemy MC
_root.life -= 15;//You'll lose 15 health
}
}

Well, this code does the same thing, but on a better way! It will check if it touches the actual shape, not the object rectangle. The first hitTest is really bad, and it’s not really easier either. Use this one, I would say it’s better!

Advertisements

Picking up health, making enemies and moving a character.

August 3, 2008

Welcome to my first tutorial. This tutorial is a basic tutorial which shows you how to make a health pack, create some enemies and create a simple walking script for a simple flash game.

STEP 1:

Make two different MC’s. Give one of them the instance name hero. The hero MC will be your character that you control in your game. The other one (the health pack) doesn’t need any instance name.

STEP 2:

These MC’s do need some graphical work inside them, so draw a health pack inside the health pack MC and draw a player inside the hero MC.

STEP 3:

Now, let’s begin with making the player walking with the arrow keys. This is very simple and I’m now gonna explain for you how to do it.

Please, take some time to actually understand the code and don’t just copy/paste. If you do that, you will never learn!

onClipEvent (load) { //When the MC have loaded
power = 3; //The speed of the player
}
onClipEvent (enterFrame) { //When you enter the frame, this code will run itself on and on again
if (Key.isDown(Key.LEFT)) { //When you hold down the left key
_x -= power; //x position will go lower according to the "power" variable
this._rotation = 270; //This will rotate the MC 270 degrees.
play(); //Play the walking animation, if you have any
}
if (Key.isDown(Key.RIGHT)) { //When you hold down the right key
_x += power; //x position will go higher
according to the "power" variable
this._rotation = 90; //This will rotate the MC 90 degrees.
play(); //Play walking animation
}
if (Key.isDown(Key.UP)) { //When you hold down the up key
_y -= power;
//y position will go lower according to the "power" variable
this._rotation = 0; //No rotation needed, will reset rotation if the MC was already rotated
play(); //Play the walking animation
}
if (Key.isDown(Key.DOWN)) { //If you hold down the down key
_y += power;
//y position will go higher according to the "power" variable
this._rotation = 180; //Rotate the MC 180 degrees.
play(); //Play the walking animation
}
if (Key.isDown(Key.RIGHT) && Key.isDown(Key.UP)) { //If you hold down both the right and up key
_rotation = 45; //Rotate the MC 45 degrees.
}
if (Key.isDown(Key.LEFT) && Key.isDown(Key.UP)) { //If you hold down both the left and up key
_rotation = 315; //Rotate the MC 315 degrees.
}
if (Key.isDown(Key.RIGHT) && Key.isDown(Key.DOWN)) { //If you hold down both the right and down key
_rotation = 135; //Rotate the MC 135 degrees.
}
if (Key.isDown(Key.LEFT) && Key.isDown(Key.DOWN)) { //If you hold down both the left and the down key
_rotation = 225; //Rotate the MC 225 degrees
} //END!

STEP 4:

Now, let’s click on the first frame on the main timeline. We will now add a health variable. It’s only one line code.

health=100; //This means you have 100 HP's

STEP 5:

Now, let’s add a dynamic text box. Insert a text field, and change it to dynamic in the drop down menu. Then after that, you will see a text field called “Var:” under the properties. Change var to health. If you now test your movie (CTRL+ENTER), you can see that there is the number 100 in the dynamic text field.

STEP 6:

We was talking about making an enemy. This is easy, as long as we don’t want any AI for it. We mustn’t use anything more complicated than hitTest for this. So, create a third MC. This will be the enemy. You must also have some code on it, of course.

The code isn’t many lines, and it’s easy to learn and understand, study, and don’t just copy/paste!

onClipEvent(enterFrame){ //When you enter the frame, code will run over and over again
if(_root.hero.hitTest(_x, _y, true)){ //Checks if the hero MC hits this.
_root.health-=10; //You'll lose 10 health in case you touch this enemy.
}
}

Now, that wasn’t hard, right?

STEP 7:

We’re almost finished, but we must add the health pack first. To begin, double click the health pack MC. Then press the first frame and open the actions panel. Then just write stop(); which will stop the MC from playing.

Now, add a blank keyframe inside the health pack. Then, just return to the main timeline and add the following code (but no copy/paste, I think you’re starting to get it!) to the health pack MC.

onClipEvent (enterFrame) { //When you enter the frame, code will run itself over and over again.
if (_root.hero.hitTest(_x, _y, true)) { //Checks if the hero MC hits the healthpack.
this.gotoAndStop(2);//If it's true, then this healthpack will go to the empty frame, so it can't be picked up again.
if (_root.health<70) { //If your health is less than 70...
_root.health += 30; //Then, you'll get 30 HP's
} else { //But. what if the health is over 70/=70?
_root.health = 100; //Then it will be 100. Full health.

}
}
}

Great, now everything should work! Look out for more tutorials by me!