2011-04-26 90 views
1

所以,我回來了更多與我的Flash應用程序有關的問題。AS3在點擊事件中獲取textField.text

我有一個成功的幾個影片剪輯列表,即「MiniInfos」,包含一些「作品」的一些細節。這些被添加到一個循環中的階段,並且每次我需要添加一個eventlistener(MouseEvent.CLICK)以便能夠單擊它以顯示更多細節。問題是,要知道接下來應該出現哪些細節,我需要訪問包含ID的TextField,以便將它傳遞給下一個Movieclip(可以稱之爲「BigInfo」)。

所以,我知道這聽起來很混亂,但生病嘗試總結。

有幾個MiniInfos添加到舞臺上與TextField的像 「ID:1,ID:2,ID:3」 等

我希望能夠點擊一個又一個影片剪輯(BigInfo)獲取添加到舞臺上更多的細節。要知道哪些信息我應該拉上PHP,我需要知道我點擊的MiniInfo的ID。

我IDEIA(沒有工作)爲:

//on the cycle 
MiniInfo.addEventListener(MouseEvent.CLICK, OpenWorkDetails); 

//further down the code 
public function OpenWorkDetails(e:MouseEvent):void 
     { 
     trace(MiniInfo.IDTrabalhoField.text); 
     //If I figure this number out, I will change it to addChild 
     } 

所以,我得到一個錯誤1120:未定義的屬性MiniInfo的訪問。

我意識到這個代碼並不是最好的,所以如果你們有一個不同的解決方案,隨時分享。我仍然學習閃光燈。

謝謝。

馬克福克斯。

回答

2

我創建了一個小型閃存應用程序來展示不同的方法可以採取:

FLASH

Main.as(文檔類):

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 
    import flash.net.URLLoader; 
    import flash.net.URLRequest; 
    import flash.net.URLRequestMethod; 
    import flash.net.URLVariables; 

    [SWF(width="275", height="175")] 
    public class Main extends Sprite 
    { 
     private var _phpPath:String = "http://localhost/stackoverflow/minibiginfos/getBigInfo.php"; 
     private var _bigInfo:BigInfo; 

     public function Main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 

     }// end function 

     private function init(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 

      var xml:XML = <miniInfos> 
           <miniInfo id="1" text="Mini Info 1" /> 
           <miniInfo id="2" text="Mini Info 2" /> 
           <miniInfo id="3" text="Mini Info 3" /> 
          </miniInfos>; 

      for (var i:uint = 0; i < xml.children().length(); i++) 
      { 
       var miniInfo:MiniInfo = new MiniInfo(xml.miniInfo[i][email protected], 
                xml.miniInfo[i][email protected]); 

       miniInfo.x = 25; 
       miniInfo.y = 25 + ((miniInfo.height + 25) * i); 
       addChild(miniInfo); 

       miniInfo.addEventListener(MouseEvent.CLICK, onMiniInfoClick); 

      }// end for 

     }// end function 

     private function onMiniInfoClick(e:MouseEvent):void 
     { 
      loadBigInfo(MiniInfo(e.currentTarget).id); 

     }// end function 

     private function loadBigInfo(id:int):void 
     { 
      if (!_bigInfo) 
      { 
       _bigInfo = new BigInfo(); 
       _bigInfo.x = 150; 
       _bigInfo.y = 25; 
       addChild(_bigInfo); 

      }// end if 

      var urlVariables:URLVariables = new URLVariables(); 
      urlVariables.miniInfoID = id; 

      var urlRequest:URLRequest = new URLRequest(_phpPath); 
      urlRequest.method = URLRequestMethod.POST; 
      urlRequest.data = urlVariables; 

      var urlLoader:URLLoader = new URLLoader(urlRequest); 
      urlLoader.addEventListener(Event.COMPLETE, onURLLoaderComplete); 
      urlLoader.load(urlRequest); 

     }// end function 

     private function onURLLoaderComplete(e:Event):void 
     { 
      var urlLoader:URLLoader = URLLoader(e.currentTarget); 

      var urlVariables:URLVariables = new URLVariables(urlLoader.data); 

      _bigInfo.text = urlVariables.text; 


     }// end function 

    }// end class 

}// end package 

import flash.display.Sprite; 
import flash.text.TextField; 

internal class MiniInfo extends Sprite 
{ 
    private var _id:int; 
    private var _text:String 
    private var _width:Number = 100; 
    private var _height:Number = 25; 

    override public function get width():Number { return _width } 
    override public function get height():Number { return _height } 
    public function get id():int { return _id } 
    public function get text():String { return _text } 

    public function MiniInfo(id:int, text:String):void 
    { 
     _id = id; 
     _text = text; 

     graphics.beginFill(0xE1E1E1); 
     graphics.drawRect(0, 0, _width, _height); 
     graphics.endFill(); 

     var textField:TextField = new TextField(); 
     textField.x = textField.y = 5; 
     textField.text = text; 
     textField.mouseEnabled = false; 
     addChild(textField); 

    }// end function 

}// end class 

internal class BigInfo extends Sprite 
{ 
    private var _width:Number = 100; 
    private var _height:Number = 125; 
    private var _textField:TextField; 

    public function get text():String { return _textField.text } 
    public function set text(text:String):void { _textField.text = text; } 

    public function BigInfo() 
    { 
     graphics.beginFill(0xE1E1E1); 
     graphics.drawRect(0, 0, _width, _height); 
     graphics.endFill(); 

     _textField = new TextField(); 
     _textField.width = 90; 
     _textField.wordWrap = true; 
     _textField.x = _textField.y = 5; 
     _textField.mouseEnabled = false; 
     addChild(_textField); 

    }// end function 

}// end class 

PHP

getBigInfo。PHP:

<?php 

    if(!empty($_POST)) 
    { 
     $bigInfos = array 
        (
         array 
         (
          'miniInfoID' => 1, 
          'text' => "This is the big info for mini info 1" 
         ), 
         array 
         (
          'miniInfoID' => 2, 
          'text' => "This is the big info for mini info 2" 
         ), 
         array 
         (
          'miniInfoID' => 3, 
          'text' => "This is the big info for mini info 3" 
         ) 
        ); 

     $text; 

     for($i = 0; $i < sizeof($bigInfos); $i++) 
     { 
      foreach($bigInfos as $item) 
      { 
       if($item['miniInfoID'] == (int)$_POST['miniInfoID']) 
       { 
        $text = $item['text']; 
        break; 
       } 

      }// end foreach 

      break; 

     }// end for 

     print "text=$text"; 

    }// end if 

?> 

以下是閃光燈應用程序運行的一個圖像:

minibiginfos flash application

1
public function OpenWorkDetails(e:MouseEvent):void{   
    trace((e.currentTarget as MiniInfo).IDTrabalhoField.text); 
} 
+0

錯誤:1119:通過與靜態類型MiniInfo參考可能未定義的屬性IDTrabalhoField的訪問。 我需要導入一些東西嗎? – FoxLift 2011-04-26 13:33:21

+0

@Marco Fox - 什麼是miniinfo? IDTrabalhoField如何在那裏聲明? – www0z0k 2011-04-26 13:41:42

+0

IDTrabalhoField是名爲MiniInfo的MovieClip中的動態文本字段(正確命名爲IDTrabalhoField,我知道它的工作原理,因爲我之前使用過)。這個miniInfo在一段時間內通過addChild添加到舞臺中。所有這些都在「容器」MovieClip中。 我知道它的混亂,所以生病圖表更好: MainSWF.swf> Container.as> MiniInfo(影片剪輯) – FoxLift 2011-04-26 13:46:09

0

你肯定MainFInfo,你MainINfo.addEventListener和OpenWOrkDetails都在同一個影片剪輯?

您正在收到的錯誤意味着MainInfo不存在於同一個MovieClip中。 lee

0

您的問題可能是MiniInfo未在範圍內聲明。如果它是一個類實例,那麼它會起作用,但是如果在函數內部聲明瞭MiniInfo,那麼只要退出該函數,它就會「被遺忘」。這就是爲什麼你必須做www0z0k做的事情,並使用事件的currentTarget屬性。

+0

MiniInfo doesent有一個AS文件,如果我做一個爲了它,它會開始工作嗎? – FoxLift 2011-04-26 13:40:26