Flash ActionScript 2.0 Tutorial – Making a Health Bar – Part 3/Making a Button

This tutorial is a direct sequel to the tutorial,
“Making a Health Bar Part 1 and 2,” but it can also be used by
itself for anyone who wants to know how to make a button.

Step 1 – Placing the button

To start your button, click on your text tool and make
sure its set to Static Text. Now click anywhere on the field
that you want your button to be and just write something. Can
be “Button” or “Hurt Yourself” or “Shoot Me” it doesn’t matter.

Step 2 – Converting it to a Symbol

Click on your button and press f8 or right-click and on
the list click on Convert to Symbol. Now in the window that
pops up click on Type and make sure it’s set to Button. The
registration doesn’t matter but I don’t know I always keep it in
the middle. Now name it whatever you want and press Enter or
click OK. Again as before, you can call it what you want but
it’s always best to keep the name as relative as possible. When
you make a full blown project that has many buttons, you’re going
to want to know exactly which button your messing with whenever
you go back to change something.

Step 3 – Adding your Code

Now click on your button and press f9 or right-click
and on the list click on Actions. In the window that pops up
you will want to add this code:

on(release){
health-=10;
}

Line 1 – on(release){ – is saying that every time the
button is pressed, whatever commands are in the brackets will
be executed. Line 3 will always be the end of your code in a
button. So

on(release){
//everything you
//want it to do
//will go here
}

Line 2 – health-=10; – is saying that it will lower the
health variable by 10. By itself it won’t do anything but
placing it in the brackets says that every time the button is
pressed it will be executed and therefore bring the health bar
down by 10.There are other ways to write this line like

_root.health = _root.health – 10;

This way can be used to add other variables like attack
strength vs. defense type deals like

_root.health = _root.health – (_root.attack –
_root.defense);

But that’s for another day so anyways. That’s the end of
this tutorial. Now just press ctrl + enter to start your
program and click your button to see if it works. Those of you
who went straight to this tutorial I am assuming you already
have something that you’re adding the button to but if not you
can go to the “Making a Health Bar” tutorial to give your
button something to do. If anyone has any issues getting this
to work or are confused in any way please feel free to contact
me at [email protected] (yes it’s real haha).

Yeah this tutorial was long and could’ve easily been
made ten times shorter but I wanted to make sure I got every
little detail cause when I was learning, I read many tutorials
that claimed they were beginner friendly but then didn’t even
tell you that you had to make your objects into symbols before
adding code. They just said,” Make a box and add this code.”
And I was saying, “uuuum.” So yeah haha. Thanks for reading and
good luck.


People also view

Leave a Reply

Your email address will not be published. Required fields are marked *