Flash Basic Boolean Variable Tutorial

Booleans can be used for many reasons when creating applications and games in Flash – including pause menus – so today I’ll show you how to create a Boolean, what it is and what it does.

Let’s start by creating a Boolean variable. It sounds pretty daunting but can be extremely helpful for things like pause screens and rapid fire weaponry. So, start a new Flash document and place this code on your main timeline:

var shooting = false;
onMouseDown = function () {
shooting = true;
};
onMouseUp = function () {
shooting = false;
};

What we are doing here is declaring a variable called ’shooting’ and starting it off as false. A Boolean is a type of variable that returns true and false values instead of numbers like a normal variable.

The second part of the code shows that when the mouse is pressed down, we want shooting to be true. The third part resets the shooting variable to false when the mouse button is relieved. If you create a dynamic textbox on your stage and give it the variable name ’shooting’ (minus the quotes) by selecting the textbox and then looking in the properties panel (F9), you can see it change as you press the mouse button down and release it. Obviously it is up to you to apply actions for the code. For instance if you wanted a movieclip with the instance name of ‘ball’ to move to the right when shooting is true, then you would have this code:

var shooting = false;
onMouseDown = function () {
shooting = true;
};
onMouseUp = function () {
shooting = false;
};
onEnterFrame = function () {
if (shooting == true) {
ball._x += 5;
}
};

It is relatively simple. If you have a code on a player movieclip to make him move or something similar, you can always add:

if(paused==false){
//actions

…before the movement code that only allows the following code to work if paused is false. Of course if paused is true, then the code will cease to function until pause is returned back to false. Pause menus have been fairly scary for alot of new developers and when getting involved with creating Flash games can seem like a daunting task, however Booleans do make this extremely user friendly and easy, and once you’ve created your first code snippet using a Boolean variable you’ll wonder how you ever operated without them!

This was just a quick tutorial as I haven’t posted for a little while, however I thought this would definately help someone out.

Download tutorial source file: Mediafire

~Chaz :)

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 3.50 out of 5)
Loading ... Loading ...

  • Share/Bookmark

Similar Articles:

Leave a Reply