2013-12-10 109 views
0

我想先開始詢問是否有人對AS3中解析數據/或會話變量的任何資源(書籍或示例)有任何建議。我研究了一些資源,但這些書或網站,並沒有真正涵蓋我提到的主題。

我想解析一些從php的JSON數據到AS3 VO,然後將VO輸入到矢量文件中,矢量文件將數據放入框中,然後將其作爲文件顯示。這些框將結果分成特定的數據。例如方框1(ID1,Name1,Location1)。方框2(ID2,名稱2,位置2)。當特定的盒子被擊中時,該盒子將會對該盒子(列表)的ID進行會話,然後對其進行解析,以便它進入下一個AS文件(從數據庫中提取細節)以顯示該特定列表的詳細信息。

在其他職位&海報的幫助下,我設法得到解析JSON回來,但我有2個錯誤,因爲我不知道哪個有效的向量我需要把它作爲參數。我可以有一些想法/建議,當我點擊該框時應該如何創建「會話ID」?

錯誤#1034:類型強制失敗:無法轉換 AS3 .vec ::矢量$到AS3 .vec.Vector .. 在這條線var s3:SearchVectorTest= new SearchVectorTest(Vector.<searchVO1>);在下面的文件sresultnologin 。 錯誤#1069:在com.clark.SearchVectorTest上找不到屬性0,並且沒有默認值。從這一行開始 mySearchVector[i].nobed = searchVOs[i].nobed;在searchVO1文件中 下面感謝您的時間。 SearchVO1

package com.clark 
{ 

    import flash.display.MovieClip; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 
    import flash.display.Stage; 
    import fl.controls.Button; 
    import flash.net.URLLoader; 
    import flash.net.URLRequest; 
    import flash.net.URLRequestMethod; 
    import flash.net.URLLoaderDataFormat; 
    import flash.net.URLVariables; 
    import flash.utils.*; 
    import flash.sampler.NewObjectSample; 




     public class searchVO1 extends MovieClip { 
     private var Arvariables:URLVariables; 
     private var SrSend:URLRequest; 
     private var SaLoader:URLLoader; 
      public var nobed:String; 
     public var zip:String; 
     public var Location:String; 
     public var price:String; 
      public var callMethod:Function; 

public var s1:searchpage = new searchpage(); 

       public function searchVO1():void{ 

addEventListener(Event.ADDED_TO_STAGE, onadded);     // init 
function onadded (event:Event):void{ 


      s1.x=-10; 
      s1.y=10; 

      addChild(s1); 

      s1.searchs.addEventListener(MouseEvent.CLICK, ValidateAndsearch); 


      // Build the varSend variable 
      SrSend = new URLRequest("http://localhost/search.php"); 
      SrSend.method = URLRequestMethod.POST; 
        Arvariables = new URLVariables; 
      SrSend.data = Arvariables; 

      SaLoader = new URLLoader(); 
      SaLoader.dataFormat = URLLoaderDataFormat.TEXT; 
      SaLoader.addEventListener(Event.COMPLETE,Asandler); 


// private methods 

      function Asandler(event:Event):void{ 

// retrieve data from php call 
var resultString :String = event.target.data; 
// parse result string as json object and cast it to array 
var resultArray :Array = JSON.parse(resultString) as Array; 
// get the length of the result set 
var len:int = resultArray.length; 

// create vector of SearchVO 
var searchVOs:Vector.<searchVO1> = new Vector.<searchVO1>(); 
// loop the result array 
for(var i:int = 0; i<len; i++) 
{ 
    searchVOs[i] = new searchVO1(); 
    searchVOs[i].nobed = resultArray[i].nobed; 
    searchVOs[i].zip = resultArray[i].zip; 
    searchVOs[i].Location = resultArray[i].Location; 
    searchVOs[i].price = resultArray[i].price; 
} 

// call a function to create your boxes 

// or maybe create your SearchVector class and pass it your search vector 
var mySearchVector:SearchVectorTest = new SearchVectorTest(searchVOs); 
    for(var i:int = 0; i<len; i++) 
mySearchVector[i].nobed = searchVOs[i].nobed; 
mySearchVector[i].zip = searchVOs[i].zip; 
mySearchVector[i].Location = searchVOs[i].Location; 
mySearchVector[i].price = searchVOs[i].price; 
     } 
    } 
    } 

    public function searchVOs(nobed:String, zip:String, location:String, price:String) 
     { 
      this.nobed  = nobed; 
      this.zip  = zip; 
      this.Location = Location; 
      this.price  = price; 
     } 
     public function ValidateAndsearch (event:MouseEvent):void { 

     // validate fields 

     Arvariables.nobed = s1.nobed.text; 
     Arvariables.zip = s1.zip.text; 
      Arvariables.Location = s1.Location.text; 
      Arvariables.price = s1.price.text; 
    SaLoader.load(SrSend); 
      var s7:sresultnologin = new sresultnologin()  
    removeChild(s1); 
      addChild(s7); 




     } 

     } 

} 

SearchVectorTest

package com.clark 
{ 
    import flash.display.MovieClip; 
    import flash.text.TextField; 
    import flash.text.TextFieldAutoSize; 
    import flash.text.TextFormat; 
    import flash.text.TextFormatAlign; 
    import flash.display.Sprite; 

    public class SearchVectorTest extends MovieClip 
    { 
    public function SearchVectorTest(test:Vector.<searchVO1>) 
     { 
      super(); 


       for (var j:int = 0; j < test.length; j++) 
      { 

       trace(test[j].nobed); 
       trace(test[j].zip); 
       trace(test[j].Location); 
       trace(test[j].price); 



     } 


      var currentY:int = 270; 

      for (var k:int = 0; k < test.length; k++) 
      { 
       var Bolder:Listing5 = new Listing5(); 

       Bolder.x=80; 

       var bf:TextField = new TextField(); 
       var bf1:TextField = new TextField(); 
       var bf2:TextField = new TextField(); 
       var bf3:TextField = new TextField(); 

       bf3.width = 100; 
       bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER); 

       bf.width = 100; 
       bf.autoSize = TextFieldAutoSize.CENTER; 
       bf1.width = 100; 
       bf1.autoSize = TextFieldAutoSize.CENTER; 
       bf2.autoSize = TextFieldAutoSize.CENTER; 
       bf3.autoSize = TextFieldAutoSize.CENTER; 
       bf3.width = 100; 
       bf1.y= bf.height+5; 





       bf.text = test[k].nobed; 
       bf1.text = test[k].zip; 
       bf2.text = test[k].Location; 
       bf3.text = test[k].price; 

       bf1.x = (Bolder.height-bf.height)*.5 

       bf3.x = (Bolder.height-bf.height)*.5 


       bf.x = (Bolder.height-bf.height)*.5 
       bf.y = (Bolder.height-bf.height)*.15 
       Bolder.addChild(bf); 
       Bolder.addChild(bf1); 
       Bolder.addChild(bf2); 
       Bolder.addChild(bf3); 



       Bolder.y = currentY; 

       addChild(Bolder); 
       currentY += Bolder.height + 35; 
      } 

     } 

    } 

} 

sresultnologin

package com.clark 

{ 

    import flash.display.*; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 
    import flash.display.Stage; 
    import fl.controls.Button; 




     public class sresultnologin extends MovieClip { 

public var s1:Searchreult = new Searchreult(); 

       public function sresultnologin(){ 
        // init 



      addEventListener(Event.ADDED_TO_STAGE, onadded); 
     function onadded (event:Event):void{ 



      s1.x=-10; 
      s1.y=10; 

      addChild(s1); 
     } 

var s3:SearchVectorTest= new SearchVectorTest(Vector.<searchVO1>); 
       addChild (s3); 


      } 

} 
} 
+1

奇怪的是,你有一個變量searchVO,它的名字和函數searchVOs的名字相同,你沒有searchVO1類的編譯錯誤?另一件事,你在searchVO1類中創建searchVO1向量?你的SearchVectorTest類已經包含了你的結果,爲什麼你想在第二個循環中添加它? –

+0

我遇到的唯一錯誤是錯誤#1069:在mySearchVector [i] .nobed = searchVOs [i] .nobed上找不到屬性0; 我創建了searchVO1來逐步解析JSON數據到矢量。我不知道如何。目前它在結果中顯示結果,但是如果我刪除了這些結果,它們都沒有通過。我不知道如何解決它 – Benyaman

+1

你不必做mySearchVector [i] .nobed = searchVOs [i] .nobed;因爲mySearchVector已經在構造函數中傳遞了所有的VO –

回答

1

當你mySearchVector對象未添加到顯示列表,所有這不能顯示的內容...

試試吧,它必須工作:

function Asandler(event:Event):void 
{ 

    // retrieve data from php call 
    var resultString :String = event.target.data; 
    // parse result string as json object and cast it to array 
    var resultArray :Array = JSON.parse(resultString) as Array; 
    // get the length of the result set 
    var len:int = resultArray.length; 

    // create vector of SearchVO 
    var searchVOs:Vector.<searchVO1> = new Vector.<searchVO1>(); 
    // loop the result array 
    for(var i:int = 0; i<len; i++) 
    { 
     searchVOs[i]   = new searchVO1(); 
     searchVOs[i].nobed = resultArray[i].nobed; 
     searchVOs[i].zip  = resultArray[i].zip; 
     searchVOs[i].Location = resultArray[i].Location; 
     searchVOs[i].price = resultArray[i].price; 
    } 


    // create your SearchVector class and pass it your search vector 
    var mySearchVector:SearchVectorTest = new SearchVectorTest(searchVOs); 
    // add your searchVector to the display list 
    addChild(mySearchVector); 

    // REMOVE THIS BLOCK !! 
    /* 
    for(var i:int = 0; i<len; i++) 
     mySearchVector[i].nobed = searchVOs[i].nobed; 
     mySearchVector[i].zip = searchVOs[i].zip; 
     mySearchVector[i].Location = searchVOs[i].Location; 
     mySearchVector[i].price = searchVOs[i].price; 
    } 
    */ 
} 
+0

我在VO字段中添加了框,所以我刪除了VectorTest字段,一切都在VO文件內完成。但我會試一試 – Benyaman

+0

啊,我愛你!兩種方式都有效! – Benyaman

1

var s3:SearchVectorTest= new SearchVectorTest(Vector.<searchVO1>);是你發送一個參考一類不是那種類型的對象的實例。

更改爲

var s3:SearchVectorTest= new SearchVectorTest(new Vector.<searchVO1>()); 

var s3:SearchVectorTest= new SearchVectorTest(Vector.<searchVO1>([])); 
+0

嗨,好的!這部分工作,想知道你是否有任何關於錯誤#1069:在mySearchVector [i] .nobed = searchVOs [i] .nobed上找不到屬性0的想法。我不知道我如何創建一個searchvector類並傳遞該信息。 – Benyaman

+0

ow,那是因爲「mySearchVector」是一個MovieClip而不是一個List?你正在嘗試迭代一個動畫片段...不是一個動畫片段列表? – WORMSS

相關問題