views:

48

answers:

1

I am trying to create a "meter bar" in Flash. I am creating an interactive house in Flash. Inside the house are objects that consume power. (ie. light bulb, computer, stove, etc...) Those objects are toggle buttons (created using MovieClips). (The objects can be toggled ON/OFF.) When ON--there is a "power value" associated with each house object (light bulb would be 1, computer would be 2, the tv would be 3...)

I am trying to create a "meter bar" (that looks like a preloader bar) that visually shows how much power in the house is being used. Having a full meter bar--would be having all the house objects turned on.

Not sure where to start. Any ideas as far as direction or any tutorials/examples you might find helpful would be appreciated.

+1  A: 

You could create a custom event that would be dispatched each time an item is clicked. The event would have two properties, the power level and a status property.

When this event is received, you can update the value of your meter bar. if the status value is "Off" , decrement the meter bar value by the value of power , if "On" , increment by the value of power.

I would create two classes - ToggleClickButton , would have a status property that is updated by each click - CustomEvent , as explained above

Of course you have to add the missing elements ( meterLevel , meterBar etc... )

The CustomEvent can be avoided if it all happens within the same class , in such case you just need to know the clicked button status and pass this to a function with the powerData value.


function toggleClick(event:MouseEvent):void
{
   var button:ToggleClickButton = event.currentTarget as ToggleClickButton;
   dispatchEvent( new CustomEvent( powerData ,  button.status) );
}

function customEventListener(event:CustomEvent ):void
{
   if( event.status == 0 )
  {
      meterLevel -= event.powerData;
  }else {

     meterLevel += event.powerData;
   }

   updateMeterDisplay();
}

function updateMeterDisplay()
{
   //for instance if you have a maxPower and a meterBar maxHeight
   meterBar.height = meterLevel * (maxHeight/maxPower );
}

PatrickS