views:

179

answers:

1

I am trying to figure out how to dynamically populate textfields in Flash. The text in the fields will either say ON or OFF based on the value of bstatus.

var bstatus will either have a value of 0 or 1

I have the following textfields listed below. I'm not sure if the syntax is correct, but I was thinking that the text fields would be in an array, and I would create a for loop--that will go through the array in order to have the fields populated.

   var textFields:Array = new Array();
   textFields[0] = compTxt.text;
   textFields[1] = bathLightTxt.text;
   textFields[2] = computerLightTxt.text;
   textFields[3] = kitchenLight.text;
   textFields[4] = livingLightTxt.text;
   textFields[5] = descLightTxt.text;
   textFields[6] = laundryLightTxt.text;
   textFields[3] = ovenTxt.text;
   textFields[8] = tvTxt.text; 
   textFields[9] = washerTxt.text; 

I'm thinking that that the textFields Array will go inside a function called populateFields()

 // function populateFields ------------------------------------------------------------------
  function populateFields(bstatus:int)
{

   var displayText:String="";   

   var textFields:Array = new Array();
   textFields[0] = compTxt.text;
   textFields[1] = bathLightTxt.text;
   textFields[2] = computerLightTxt.text;
   textFields[3] = kitchenLight.text;
   textFields[4] = livingLightTxt.text;
   textFields[5] = descLightTxt.text;
   textFields[6] = laundryLightTxt.text;
   textFields[3] = ovenTxt.text;
   textFields[8] = tvTxt.text; 
   textFields[9] = washerTxt.text; 


  if (bstatus == 0) {
   // textfield will say OFF
          displayText = "OFF";

  } else if (bstatus == 1) {
  // textfield will say ON
            displayText = "ON";

     }


  /*
   for (var i:int=0;i<textFields.length;i++)
   {
                      // do something

   }
  */


}

Entire Code:

package {

        import flash.display.MovieClip;
         import flash.events.MouseEvent;
         import flash.events.Event;
      import flash.events.EventDispatcher; //event dispatcher

     public class House extends MovieClip

     {
     // List Objects inside Treehouse here....
        //private var comp:MovieClip; // comp is a property of TreeHouse
     //private var light:MovieClip;
     //var comp:HouseObjects = new HouseObjects(); //do this inside another class


     var HouseObjects:Array = new Array(); // creates HouseObjects array
     var onList:Array = [];

     var obj_num:int = 10;

     var power:int; // holds value of individual House Objects
     var bstate:int; // 0 or 1 (ON or OFF)
     var bstatus:int;
        var userInput:int; // stores user data (of selected data); holds value of e.currentTarget.power
     var currentPower:int; // stores current power
     //var objName:String;
     var objSelect:Object;

     // Constructor--------------------------------------------------------------------
     public function House()


     {
      var currentPower:int = 0;

      HouseObjects[0] = new Comp();
      HouseObjects[1] = new Light(); // bathroom light
      HouseObjects[2] = new LightB(); // computer area light
      HouseObjects[3] = new LightC(); // kitchen light
      HouseObjects[4] = new LightD(); // living room light
      HouseObjects[5] = new LightE(); // description light
      HouseObjects[6] = new LightF(); // laundry room light
      HouseObjects[7] = new Oven();
      HouseObjects[8] = new Tv();
      HouseObjects[9] = new Washer();
      //HouseObjects[10] = new Car();

      //onList.push(objName);
      //trace(objName);

      //onList.push(HouseObjects[1]);
      trace("Tracing onList Array: " + onList);

      // list properties of the objects -----------------------------------------------------------------
      HouseObjects[0].power = 2; // amount of power
      HouseObjects[0].name = "comp";
      //comp.bstate = 0; // button state

      HouseObjects[1].power = 1; // amount of power
      HouseObjects[1].name = "light"; 

      HouseObjects[2].power = 1; // amount of power
      HouseObjects[2].name = "lightB"; 

      HouseObjects[3].power = 1; // amount of power
      HouseObjects[3].name = "lightC";

      HouseObjects[4].power = 1; // amount of power
      HouseObjects[4].name = "lightD";

      HouseObjects[5].power = 1; // amount of power
      HouseObjects[5].name = "lightE";

      HouseObjects[6].power = 1; // amount of power
      HouseObjects[6].name = "lightF";

      HouseObjects[7].power = 3; // amount of power
      HouseObjects[7].name = "oven"; 

      HouseObjects[8].power = 4; // amount of power
      HouseObjects[8].name = "tv"; 

      HouseObjects[9].power = 5; // amount of power
      HouseObjects[9].name = "washer"; 

      //HouseObjects[9].power = 6; // amount of power
      //HouseObjects[9].name = "car";
       // end of list properties of the house objects -----------------------------------------------------



      for (var i:int=0;i<obj_num;i++)
      {

      HouseObjects[i].buttonMode = true;

      //add event listeners -- listens to functions that are called  
        HouseObjects[i].addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
        HouseObjects[i].addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
        HouseObjects[i].addEventListener(MouseEvent.CLICK, toggleClick);
        HouseObjects[i].addEventListener(MouseEvent.CLICK, toggleClick);
        HouseObjects[i].bstate = 0;

       stage.addChild(HouseObjects[i]); // add House Objects to stage ------------------------------

        }// end of for loop ------------------------------------------------------------------------

       trace("tracing...");
       //computer's position
       HouseObjects[0].x = 585;
       HouseObjects[0].y = 233;

       //bathroom light's position
       HouseObjects[1].x = 340;
       HouseObjects[1].y = 161;

      //computer lightB's position
       HouseObjects[2].x = 579;
       HouseObjects[2].y = 158;

       //computer lightC's position
       HouseObjects[3].x = 316;
       HouseObjects[3].y = 368;

       //computer lightD's position
       HouseObjects[4].x = 657;
       HouseObjects[4].y = 367;

       //computer lightE's position
       HouseObjects[5].x = 517;
       HouseObjects[5].y = 549;

       //computer lightF's position
       HouseObjects[6].x = 531;
       HouseObjects[6].y = 1000;

       //oven's position
       HouseObjects[7].x = 380;
       HouseObjects[7].y = 449;

       //tv's position
       HouseObjects[8].x = 543;
       HouseObjects[8].y = 423; 

       //washer's position
       HouseObjects[9].x = 637;
       HouseObjects[9].y = 1155; 

     } // end of Constructor -----------------------------------------------------------

     // function rollOver -------------------------------------------------------------- 
     function rolloverToggle(e:MouseEvent) {  
      if (e.currentTarget.currentFrame == 1)
      e.currentTarget.gotoAndStop(2);
        if (e.currentTarget.currentFrame == 3)
      e.currentTarget.gotoAndStop(4);
     }

     // function rollOut ----------------------------------------------------------------- 
     function rolloutToggle(e:MouseEvent) {
      if (e.currentTarget.currentFrame == 2)
      e.currentTarget.gotoAndStop(1);
      if (e.currentTarget.currentFrame == 4)
      e.currentTarget.gotoAndStop(3); 
     }

     // function toggleClick-------------------------------------------------------------
       function toggleClick(e:MouseEvent) {

       // On MouseEvent gotoAndStop(Frame Number)
       if (e.currentTarget.currentFrame == 2)
        {
      e.currentTarget.gotoAndStop(3);
      e.currentTarget.bstate = 1;
        }

        if (e.currentTarget.currentFrame == 4)
        {
      e.currentTarget.gotoAndStop(1);
      e.currentTarget.bstate = 0;
        }  

       // trace statements -------------------------------------------------------
       //trace("movieClip Instance Name = " + e.currentTarget); // [object Comp]
       //trace(houseArray[e.currentTarget.name]); // comp
       trace("using currentTarget: " + e.currentTarget.name); // comp
       //trace("powerData: " + powerData); // power of user data
       //trace("houseArray: " + houseArray[0]); // the 0 index of house array
      // trace(e.currentTarget.power); // currentTarget's power************
       //trace ("bstate in click function: " + e.currentTarget.bstate);
       //objName = e.currentTarget.name;


       trace("target: " + e.currentTarget);

       bstatus = e.currentTarget.bstate;
       userInput = e.currentTarget.power;
       objSelect = e.currentTarget;
       trace("objSelect: " + objSelect);
      // end of trace statements -------------------------------------------------

       calcPower(userInput, bstatus);
       //trace("i am bstatus: " + bstatus);
       //trace("i am power: " + userInput);

       populateFields(bstatus);

       sendPhp();

       //trackItems(objSelect, bstatus);

       } // end of function toggleClick ----------------------------------------------------

    // function calcPower ------------------------------------------------------------------
     function calcPower(userInput:int, bstatus:int):void 
     {
      //trace("i arrived");
      //trace("power: " + userInput + " and " + "bstate: " + bstatus);
      if (bstatus == 0) {
       trace("i have been clicked OFF");
         currentPower -= userInput; // if OFF then minus
      trace("currentPower: " + currentPower);

      } else if (bstatus == 1) {
       trace("i have been clicked ON");
         currentPower += userInput; // if OFF then minus
      trace("currentPower: " + currentPower);
      }  

     }

     // function populateFields ------------------------------------------------------------------
      function populateFields(bstatus:int)
    {

       var displayText:String="";



       var textFields:Array = new Array();
       textFields[0] = compTxt.text;
       textFields[1] = bathLightTxt.text;
       textFields[2] = computerLightTxt.text;
       textFields[3] = kitchenLight.text;
       textFields[4] = livingLightTxt.text;
       textFields[5] = descLightTxt.text;
       textFields[6] = laundryLightTxt.text;
       textFields[3] = ovenTxt.text;
       textFields[8] = tvTxt.text; 
       textFields[9] = WasherTxt.text; 


      if (bstatus == 0) {
       // textfield will say OFF

      } else if (bstatus == 1) {
      // textfield will say ON
      }


      /*
       for (var i:int=0;i<textFields.length;i++)
       {
                          // do something

       }
      */

    }

     // function pwrPercentage ------------------------------------------------------------------
      /*function pwrPercentage()
    {
    }
     */

     // function trackItems ------------------------------------------------------------------
      function trackItems(objSelect:Object, bstatus:int):void 
     {
      trace("i arrived in trackItems");
      trace("tracing objSelect in function trackItems: " + objSelect);
      //trace("tracing Array onList :" + onList);
      //trace("power: " + userInput + " and " + "bstate: " + bstatus);
      if (bstatus == 0) {
       //onList.removeItemAt(onList.getItemIndex(objSelect));
      // remove from ON list (array) pop

      // call function removeArrayItem
      removeArrayItem(objSelect);    

      } else if (bstatus == 1) {
       //trace("adding items to list");
      onList.push(objSelect);
      //add to ON list (array) push  
      }  
      trace("Array:" + onList);
      //return array .... only have one array...contain objects that are ONLY ON  
     }

    // function removeArrayItem ------------------------------------------------------------------ 
     function removeArrayItem(objSelect:Object):void
    {
      var arrayLength:int = onList.length;

      // Searches item in array
      for (var i:int=0; i<arrayLength; i++)
      {
       // Finds item and removes it
       if (onList[i] == objSelect)
       {
          onList.splice(i, 1);
          ///*/**/*/trace("Item Removed: " + onList[i]);
          trace("Array Updated: " + onList);
       }
      }

    }

    // function frameloop ------------------------------------------------------------------
    /*  function frameloop(e:Event)
      {
      }
    */

    // function sendPhp ------------------------------------------------------------------
     function sendPhp(e:MouseEvent):void
     {

      // send vars to php
      var request:URLRequest = new URLRequest("write_xml.php"); // the php file to send data to
      var variables:URLVariables = new URLVariables(); // create an array of POST vars

      variables['bathLight'] = compTxt.text;
      variables['bathLight'] = bathLightTxt.text; 
      variables['computer'] = computerLightTxt.text; 
      variables['kitchenLight'] = kitchenLight.text;
      variables['livingLight'] = livingLightTxt.text; 
      variables['descLight'] = descLightTxt.text;
      variables['laundryLight'] = laundryLightTxt.text; 
      variables['oven'] = ovenTxt.text; 
      variables['tv'] = tvTxt.text;
      variables['washer'] = washerTxt.text;  

      request.data = variables; // send the vars to the data property of the requested url (our php file)
      request.method = URLRequestMethod.POST; // use POST as the send method
      try
      {
       var sender:URLLoader = new URLLoader();
       sender.load(request); // load the php file and send it the variable data
       navigateToURL(new URLRequest("vars.xml"), '_blank'); //show me the xml
      } 
      catch (e:Error) 
      {
       trace(e); // trace error if there is a problem
      }
     }


     // function called when user clicks on update button-----------------------------------
     /*function updateStage():void
     {
     for (var i:int = 0; i<=onList.length;i++) { 
     addChild(onList[i]);
     }

     }*/


     } //end of class

    } // end of package
A: 

You will be better off keeping track of the reference to your textboxes in order to populate them afterwards:

var textFields:Array = new Array();
textFields[0] = compTxt;
textFields[1] = bathLightTxt;
textFields[2] = computerLightTxt;
textFields[3] = kitchenLight;
textFields[4] = livingLightTxt;
textFields[5] = descLightTxt;
textFields[6] = laundryLightTxt;
textFields[3] = ovenTxt;
textFields[8] = tvTxt; 
textFields[9] = washerTxt; 

I tried it out this way:

import fl.controls.*;
var textFields:Array = new Array(9);
textFields[0] = new TextInput();
textFields[1] = new TextInput();
textFields[2] = new TextInput();
textFields[3] = new TextInput();
textFields[4] = new TextInput();
textFields[5] = new TextInput();
textFields[6] = new TextInput();
textFields[7] = new TextInput();
textFields[8] = new TextInput();
textFields[9] = new TextInput();

var i:uint = 0;
for(i = 0; i < textFields.length; ++i){
   textFields[i].text = "Set text at " + i;
   trace(textFields[i].text);
}
jdecuyper