Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > Adobe Flash

Flash Reference Guide

Hosted by

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Working with Boolean Objects in ActionScript

Last updated Aug 22, 2003.

If you’re looking for more up-to-date information on this topic, please visit our Flash article, podcast, and store pages.

Do you watch television games shows? I love 'em. A popular show from England that's now tearing up the charts in the states on NBC is “Deal Or No Deal?” where the contestant chooses between only two options. Whenever you have just two choices, be they "true or false,” “left or right,” or “up or down," you are confronting a Boolean, which actually has many practical applications in Flash.

Using True and False in Your Code

Use of Boolean statements is the first step towards creating decision driven code. Code that acts on logical choices. With the Boolean object you have the logical choice of true or false. A Boolean statement can be easily declared in ActionScript. The following demonstrates this:

var myBoolean:Boolean = true; 

The example above declares a new variable called “myBoolean.” The object type is defined as a Boolean. If the object is a Boolean it can only have one of two different states: true or false. In this example the state is set to true. True can also be defined as the number 1, with false being equal to the number 0.

Let's expand the example above. The following ActionScript declares the value of an integer to being the same as the value of the declared Boolean object “myBoolean.”

var myBoolean:Boolean = true;
var myInt:int = int(myBoolean);
trace(myINT); // output: 1 

The value of myInt can be easily changed by “myBoolean.” The following example demonstrates this:

var myBoolean:Boolean = false;
var myInt:int = int(myBoolean);
trace(myINT); // output: 0 

Using Boolean Objects in Your ActionScript

Above you were introduced to the fundamentals of Boolean objects. Now, let's add Boolean objects to some of our ActionScript.

The following ActionScript is a simple collision detection class you can use for any movie. The key is that a Boolean object is used to detect the collision because either the collision occurred or it didn't.

class collision extends MovieClip{
  //lower number, higher precision, lower perfomances
  public static var def:Number=10;
  public static var visibleMode=true; //put this to true to see the spots

  //divides shape in boxes (side=def) and attaches a movieClip
  //in the library called hotSpot <-you have to build this by your own
  //just a rectangle of any size will do the trick.
  function collision(){
    for(var X=0; X<Math.round(this._width/def); X++){
      for(var Y=0; Y<Math.round(this._height/def); Y++){
this.attachMovie("hotSpot","hotSpot_"+X+"_"+Y,this.getNextHighestDepth(),{_x:def*X,_y:def*Y,_width:def-1,_height:def-1,_visible:visibleMode});//def-1 to avoid spot's overlap
if(chkArea(X,Y-1) && chkArea(X,Y+1) && chkArea(X-1,Y) && chkArea(X+1,Y))

  //when invoked returns a boolean True if in the shape is present in
  //the box analized. False if not.
  function chkArea(X:Number,Y:Number):Boolean{
    var chkSpot = new Object({x:(X*def)+def/2,y:(Y*def)+def/2});
    return true;
    return false;

  //to check if to shape are Really overlapping
  //hitTests the bounding first, if they are overlapping
  //continues by hitTesting every hotSpot with the ones from
  //the obj Movieclip that you want to test.
  function chkCollision(obj:MovieClip):Boolean{
      for(var i in this){
        if(i.substr(0,8)== "hotSpot_"){
          for(var j in obj){
                trace("collision detected!")
                return true;
      trace("no collision detected!");
      return false;
      trace("no collision detected!");
      return false;

To use this class, build a movieclip called hotSpot and export it with the same name. Don't forget to write "collision" in the AS2 linkage field.

The following example script uses a Boolean object to detect if an XML file has loaded. If the file fails to load, then the Boolean object searches for the thrown failure message.

System.useCodepage = true;
var my_xml:XML = new XML();
my_xml.ignoreWhite = true;
var debug_:String;
var XMLED = false;
my_xml.onLoad = function(success:Boolean) {
  if (success) {
    if (my_xml.status == 0) {
      xl = my_xml.firstChild.childNodes;
      //trace([ev, ev.length]);
      //trace("XML was loaded and parsed successfully");
      debug_ = "XML was loaded and parsed successfully";
      if (my_xml.loaded) {
        //trace("successfully: "+this.loaded);
        XMLED = true;
    } else {
      //trace("XML was loaded successfully, but was unable to be parsed.");
      debug_ = "XML was loaded successfully, but was unable to be parsed.";
    var errorMessage:String;
    switch (my_xml.status) {
      case 0 :
      errorMessage = "No error; parse was completed successfully.";
      case -2 :
      errorMessage = "A CDATA section was not properly terminated.";
      case -3 :
      errorMessage = "The XML declaration was not properly terminated.";
      case -4 :
      errorMessage = "The DOCTYPE declaration was not properly terminated.";
      case -5 :
      errorMessage = "A comment was not properly terminated.";
      case -6 :
      errorMessage = "An XML element was malformed.";
      case -7 :
      errorMessage = "Out of memory.";
      case -8 :
      errorMessage = "An attribute value was not properly terminated.";
      case -9 :
      errorMessage = "A start-tag was not matched with an end-tag.";
      case -10 :
      errorMessage = "An end-tag was encountered without a matching start-tag.";
      default :
      errorMessage = "An unknown error has occurred.";
    //trace("status: "+my_xml.status+" ("+errorMessage+")");
    debug_ = "status: "+my_xml.status+" ("+errorMessage+")";
    delete my_xml;
  } else {
    //trace("Unable to load/parse XML. (status: "+my_xml.status+")");
    debug_ = "Unable to load/parse XML. (status: "+my_xml.status+")";

Working with Boolean Objects in the Future

As you have seen with this introduction, a Boolean object can be a useful and powerful tool in your code. When you are working with solutions that require a true or false response, just ask yourself: Deal or no deal?