3 Replies Latest reply on Jun 13, 2008 8:05 AM by SpartanTom

    Difficulties with referencing controls in ActionScript (buttons, etc.)


      I'm working on a simple application that's going to transfer information back and forth between an embedded device with a Microchip PIC18 process running a simple TCP/IP stack. I've created a very simple class to test the functionality of everything and get started with Flex:

      <beginning of my class, in my project SMUComm.as>

      package {
      import flash.display.Sprite;
      import flash.events.DataEvent;
      import flash.events.MouseEvent;
      import flash.net.XMLSocket;

      public class SMUComm extends Sprite
      private var _mySMU_IP:String;
      private var _mySMU_TCP_PORT:int;
      private var _sendXMLSocket:XMLSocket = new XMLSocket();

      public function SMUComm(smuip:String, smuport:int)
      _mySMU_IP = smuip;
      _mySMU_TCP_PORT = smuport;
      _sendXMLSocket.connect(_mySMU_IP, _mySMU_TCP_PORT);
      _sendXMLSocket.addEventListener(flash.events.DataEvent.DATA, dataHandler);
      earthButton.addEventListener(flash.events.MouseEvent.CLICK, earthClicked);
      marsButton.addEventListener(flash.events.MouseEvent.CLICK, marsClicked);
      statusBox.text = "Waiting...";

      public function earthClicked():void
      _sendXMLSocket.send("Hello from Earth!\n");
      statusBox.text="Data sent from Earth...";

      public function marsClicked():void
      _sendXMLSocket.send("Hello from Mars!\n");
      statusBox.text="Data sent from Mars...";

      public function dataHandler():void
      statusBox.text = "Data was rec'd...";

      </end of my class>

      In another file in my project, I've set up some simple controls using the Design view and instantiated my class:

      <beginning of main.mxml>

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

      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">

      <mx:Label x="10" y="10" text="Simple Flash Test" width="380" height="36" fontFamily="Verdana" fontSize="20" fontWeight="bold" textAlign="center"/>
      <mx:Panel x="76.5" y="68" width="247" height="212" layout="absolute" title="Buttons Which Send Messages">
      <mx:Button id="earthButton" x="10" y="10" label="Earth"
      width="100" height="100" labelPlacement="top" enabled="true"/>
      <mx:Button id="marsButton" x="117" y="10" label="Mars"
      width="100" height="100" labelPlacement="top" enabled="true" />
      <mx:Text x="10" y="118" text="Text" width="207" height="44" enabled="true" id="statusBox" textAlign="center"/>

      mySMUComm:SMUComm = new SMUComm("", 1987);


      </end of Main.mxml>

      The problem I'm having is that all of my references to any of the controls, earthButton, marsButton, and statusBox, throw the error:

      1120: Access of undefined property (name of control)

      Am I referring to my controls incorrectly? From the help files, it sounded like you just had to use their "id" attribute, but clearly something's wrong...

      Any help would be greatly appreciated, thanks in advance!

      Tom Morrisey