Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 20th, 2012  |  syntax: None  |  size: 5.89 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. AS3 getting a textField.text on Click Event
  2. //on the cycle
  3. MiniInfo.addEventListener(MouseEvent.CLICK, OpenWorkDetails);
  4.  
  5. //further down the code
  6. public function OpenWorkDetails(e:MouseEvent):void
  7.         {
  8.         trace(MiniInfo.IDTrabalhoField.text);
  9.         //If I figure this number out, I will change it to addChild
  10.         }
  11.        
  12. package
  13. {
  14.     import flash.display.Sprite;
  15.     import flash.events.Event;
  16.     import flash.events.MouseEvent;
  17.     import flash.net.URLLoader;
  18.     import flash.net.URLRequest;
  19.     import flash.net.URLRequestMethod;
  20.     import flash.net.URLVariables;
  21.  
  22.     [SWF(width="275", height="175")]
  23.     public class Main extends Sprite
  24.     {
  25.         private var _phpPath:String = "http://localhost/stackoverflow/minibiginfos/getBigInfo.php";
  26.         private var _bigInfo:BigInfo;
  27.  
  28.         public function Main():void
  29.         {
  30.             if (stage) init();
  31.             else addEventListener(Event.ADDED_TO_STAGE, init);
  32.  
  33.         }// end function
  34.  
  35.         private function init(e:Event = null):void
  36.         {  
  37.             removeEventListener(Event.ADDED_TO_STAGE, init);
  38.  
  39.             var xml:XML = <miniInfos>
  40.                               <miniInfo id="1" text="Mini Info 1" />
  41.                               <miniInfo id="2" text="Mini Info 2" />
  42.                               <miniInfo id="3" text="Mini Info 3" />
  43.                           </miniInfos>;
  44.  
  45.             for (var i:uint = 0; i < xml.children().length(); i++)
  46.             {
  47.                 var miniInfo:MiniInfo = new MiniInfo(xml.miniInfo[i].@id,
  48.                                                      xml.miniInfo[i].@text);
  49.  
  50.                 miniInfo.x = 25;
  51.                 miniInfo.y = 25 + ((miniInfo.height + 25) * i);
  52.                 addChild(miniInfo);
  53.  
  54.                 miniInfo.addEventListener(MouseEvent.CLICK, onMiniInfoClick);
  55.  
  56.             }// end for
  57.  
  58.         }// end function
  59.  
  60.         private function onMiniInfoClick(e:MouseEvent):void
  61.         {
  62.             loadBigInfo(MiniInfo(e.currentTarget).id);
  63.  
  64.         }// end function
  65.  
  66.         private function loadBigInfo(id:int):void
  67.         {
  68.             if (!_bigInfo)
  69.             {
  70.                 _bigInfo = new BigInfo();
  71.                 _bigInfo.x = 150;
  72.                 _bigInfo.y = 25;
  73.                 addChild(_bigInfo);
  74.  
  75.             }// end if
  76.  
  77.             var urlVariables:URLVariables = new URLVariables();
  78.             urlVariables.miniInfoID = id;
  79.  
  80.             var urlRequest:URLRequest = new URLRequest(_phpPath);
  81.             urlRequest.method = URLRequestMethod.POST;
  82.             urlRequest.data = urlVariables;
  83.  
  84.             var urlLoader:URLLoader = new URLLoader(urlRequest);
  85.             urlLoader.addEventListener(Event.COMPLETE, onURLLoaderComplete);
  86.             urlLoader.load(urlRequest);
  87.  
  88.         }// end function
  89.  
  90.         private function onURLLoaderComplete(e:Event):void
  91.         {
  92.             var urlLoader:URLLoader = URLLoader(e.currentTarget);
  93.  
  94.             var urlVariables:URLVariables = new URLVariables(urlLoader.data);
  95.  
  96.             _bigInfo.text = urlVariables.text;
  97.  
  98.  
  99.         }// end function
  100.  
  101.     }// end class
  102.  
  103. }// end package
  104.  
  105. import flash.display.Sprite;
  106. import flash.text.TextField;
  107.  
  108. internal class MiniInfo extends Sprite
  109. {
  110.     private var _id:int;
  111.     private var _text:String
  112.     private var _width:Number = 100;
  113.     private var _height:Number = 25;
  114.  
  115.     override public function get width():Number { return _width }
  116.     override public function get height():Number { return _height }
  117.     public function get id():int { return _id }
  118.     public function get text():String { return _text }
  119.  
  120.     public function MiniInfo(id:int, text:String):void
  121.     {
  122.         _id = id;
  123.         _text = text;
  124.  
  125.         graphics.beginFill(0xE1E1E1);
  126.         graphics.drawRect(0, 0, _width, _height);
  127.         graphics.endFill();
  128.  
  129.         var textField:TextField = new TextField();
  130.         textField.x = textField.y = 5;
  131.         textField.text = text;
  132.         textField.mouseEnabled = false;
  133.         addChild(textField);
  134.  
  135.     }// end function
  136.  
  137. }// end class
  138.  
  139. internal class BigInfo extends Sprite
  140. {
  141.     private var _width:Number = 100;
  142.     private var _height:Number = 125;
  143.     private var _textField:TextField;
  144.  
  145.     public function get text():String { return _textField.text }
  146.     public function set text(text:String):void { _textField.text = text; }
  147.  
  148.     public function BigInfo()
  149.     {
  150.         graphics.beginFill(0xE1E1E1);
  151.         graphics.drawRect(0, 0, _width, _height);
  152.         graphics.endFill();
  153.  
  154.         _textField = new TextField();
  155.         _textField.width = 90;
  156.         _textField.wordWrap = true;
  157.         _textField.x = _textField.y = 5;
  158.         _textField.mouseEnabled = false;
  159.         addChild(_textField);
  160.  
  161.     }// end function
  162.  
  163. }// end class
  164.        
  165. <?php
  166.  
  167.     if(!empty($_POST))
  168.     {
  169.         $bigInfos = array
  170.                     (
  171.                         array
  172.                         (
  173.                             'miniInfoID' => 1,
  174.                             'text' => "This is the big info for mini info 1"
  175.                         ),
  176.                         array
  177.                         (
  178.                             'miniInfoID' => 2,
  179.                             'text' => "This is the big info for mini info 2"
  180.                         ),
  181.                         array
  182.                         (
  183.                             'miniInfoID' => 3,
  184.                             'text' => "This is the big info for mini info 3"
  185.                         )
  186.                     );
  187.  
  188.         $text;
  189.  
  190.         for($i = 0; $i < sizeof($bigInfos); $i++)
  191.         {
  192.             foreach($bigInfos as $item)
  193.             {
  194.                 if($item['miniInfoID'] == (int)$_POST['miniInfoID'])
  195.                 {
  196.                     $text = $item['text'];
  197.                     break;
  198.                 }
  199.  
  200.             }// end foreach
  201.  
  202.             break;
  203.  
  204.         }// end for
  205.  
  206.         print "text=$text";
  207.  
  208.     }// end if
  209.  
  210. ?>
  211.        
  212. public function OpenWorkDetails(e:MouseEvent):void{        
  213.     trace((e.currentTarget as MiniInfo).IDTrabalhoField.text);
  214. }