1 Reply Latest reply on Nov 5, 2009 6:14 PM by Ratsnackbar

    Need help making a function callable by multiple objects;

    MudkipsAreCool

      My code is below. My problem is that, while everything is fine and dandy for the first row of objects (checkbox, NumStepper, Textinput box), I am unable to change that function to work for the second row. How can I make it so that something like this could happen

       

      public function doAge(which:String):void

      {

           if AgeInput(which)=

       

      Like that. It would be so easy to define a variable that could be plugged into one of the values, so that for the second row, the button could be

       

      click="doAge(Two)"

       

      So that the function would be repeated but instead of using AgeInputOne and PriceOne etc.. it would use AgeInputTwo and PriceTwo

       

      How would I go about doing something like this so that it is callable by each button?

       

      Here is my code, the indentation is a bit off because of the copy paste but you get the point:

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

       

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application

       

       

      xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" borderStyle="outset"

      >

       

       

       

      <mx:Script>

      <![CDATA[

       

       

      public var

      AgeGroup:String;

       

       

      public var

      Price:int;

       

       

       

      public function doAge():

      void

      {

       

       

      if

      (AgeInputOne.value <= 12)

      {

      AgeGroup =

       

      "Child"

      ;

      Price = 10;

      }

       

       

      else if

      (AgeInputOne.value >= 13 && AgeInputOne.value <=18)

      {

      AgeGroup =

       

      "Student"

      ;

      Price = 12;

      }

       

       

      else if

      (AgeInputOne.value >= 18 && AgeInputOne.value <=64)

      {

      AgeGroup =

       

      "Adult"

      ;

      Price = 15;

      }

       

       

      else if

      (AgeInputOne.value >=65 && AgeInputOne.value <=98)

      {

      AgeGroup =

       

      "Senior"

      ;

      Price = 8;

      }

       

       

      else if

      (AgeInputOne.value >= 99)

      {

      AgeGroup =

       

      "Cheater"

      ;

      Price = 9001;

      }

      }

       

       

       

      public function getPrice():

      void

      {

      (PriceOne.text = String(Price))

      }

      ]]>

       

       

      </mx:Script>

       

       

       

      <mx:Canvas x="38" y="30" width="836" height="414" borderStyle="outset" borderColor="#4999D0"

      >

       

       

       

      <mx:CheckBox x="10" y="38" id="ShowOne"

      />

       

       

      <mx:NumericStepper x="32" y="38" id="AgeInputOne" minimum="0" maximum="99" enabled="{ShowOne.selected}"

      />

       

       

      <mx:Label x="123" y="38" text="$"

      />

       

       

      <mx:TextInput id="PriceOne" x="136" y="38" width="68" editable="false

      "

      enabled="

       

      {ShowOne.selected}"

      />

       

       

      <mx:Button enabled="{ShowOne.selected}" click="doAge(), getPrice()" x="212" y="38" label="$$$" width="49"

      />

       

       

       

      <mx:CheckBox x="10" y="68" id="ShowTwo"

      />

       

       

      <mx:NumericStepper x="32" y="68" id="AgeInputTwo" minimum="0" maximum="99" enabled="{ShowTwo.selected}"

      />

       

       

      <mx:Label x="123" y="68" text="$"

      />

       

       

      <mx:TextInput id="PriceTwo" x="136" y="68" width="68" editable="false

      "

      enabled="

       

      {ShowTwo.selected}"

      />

       

       

      <mx:Button enabled="{ShowTwo.selected}" click="doAge(), getPrice()" x="212" y="68" label="$$$" width="49"

      />

       

       

      </mx:Canvas>

       

       

       

      </mx:Application>

        • 1. Re: Need help making a function callable by multiple objects;
          Ratsnackbar Level 2

          I would break the Age functions into their own class (Perhaps called PersonData) with a setAgeGroup(age:uint) function which takes the age and sets an internal private varialbe stAgeGroup.  Then use a getAgeGroup():String function to get the age group.

           

          In the setAgeGroup() function test the age and change the stAgeGroup only when required.

           

          Use a different setPrice() and getPrice() function for the price within the same class or a different class.

           

          Make the getAgeGroup() and getPrice() bindable.

           

          Then on creation of the current class create two new PersonData instances.

           

          public var personData1:PersonData = new PersonData();

          public var personData2:PersonData = new PersonData();

           


          In the first numeric stepper on change call PersonData1.setAgeGroup(value);

          In the second call PersonData2.setAgeGroup(value);

           

          same deal for the price, however I would run those through a formatter and validator.

           

          Anything that needs to read the data can bind to the getAgeGroup() and getPrice() values using the change events.  Because the age group only changes when a particular age is input you will also have fewer events fired for that value.

           


          Not sure if that is entirely clear but I hope it helps.

           

          -Joe