2016-03-01 80 views
0

我創建了一個加載XML數據的Flash CC Professional文件,其中包含鏈接,多個文本部分和圖像鏈接。當用戶通過程序和文本前進時,相應的圖像應該加載到名爲imgholder的mc中。我已經將xml文件中的var'img'追蹤到了增量圖像加載器,但仍然無法弄清楚爲什麼圖像沒有加載到容器中。哦,它不會拋出任何錯誤。從xml問題加載圖像as3

幫助!提前致謝!

1個AS3:

var questions:Array=new Array(); 
var answers:Array=new Array(); 
var numb:Array=new Array(); 
var rtext:Array=new Array(); 
var img:Array = new Array(); 



//XML loader 
var loader:URLLoader = new URLLoader(); 
loader.addEventListener(Event.COMPLETE, loadXML); 
loader.load(new URLRequest("UT_quiz.xml")); 
function loadXML(e:Event):void 
{ 
     var myxml = new XML(e.target.data); 
     var loop =myxml.ques.length(); 
       for (var i=0;i<loop;i++){ 
        questions[i]=myxml.ques[i].q1; 
        numb[i]=myxml.ques[i].qnum; 
        answers[i]=[myxml.ques[i].op1,myxml.ques[i].op2,myxml.ques[i].op3,myxml.ques[i].op4]; 
        rtext[i]=myxml.ques[i].riddle; 
        img[i]=myxml.ques[i].image; 


       }//loop 
       gotoAndPlay(2); 

} 
stop(); 

2幀AS3:

import flash.text.TextField; 
import flash.utils.getTimer; 
import flash.events.Event; 

var qno=0; var rnd1; 

function change_question(){ 
       tick.visible=false;cross.visible=false; 
       rnd1=Math.ceil(Math.random()*4); 
       q.text=questions[qno]; 
       qnum.text=numb[qno]; 
       rtxt.text=rtext[qno]; 
       imgholder.MovieClip=img[qno]; 
       if(rnd1==1){opt1.text=answers[qno][0];opt2.text=answers[qno][1];opt3.text=answers[qno][2];opt4.text=answers[qno][3];} 
       if(rnd1==2){opt1.text=answers[qno][3];opt2.text=answers[qno][0];opt3.text=answers[qno][1];opt4.text=answers[qno][2];} 
       if(rnd1==3){opt1.text=answers[qno][2];opt2.text=answers[qno][3];opt3.text=answers[qno][0];opt4.text=answers[qno][1];} 
       if(rnd1==4){opt1.text=answers[qno][1];opt2.text=answers[qno][2];opt3.text=answers[qno][3];opt4.text=answers[qno][0];} 
       }//function change_question 

function enable_disable(a){ 
    if(a==0){shade1.mouseEnabled=false;shade2.mouseEnabled=false;shade3.mouseEnabled=false;shade4.mouseEnabled=false;} 
    if(a==1){shade1.mouseEnabled=true;shade2.mouseEnabled=true;shade3.mouseEnabled=true;shade4.mouseEnabled=true;}} 

change_question(); 


next_b.addEventListener(MouseEvent.CLICK, ButtonAction1); 
function ButtonAction1(eventObject:MouseEvent) { 

     qno++;change_question(); 

if(MovieClip(root).currentGalleryItem == 4){ 
      MovieClip(root).currentGalleryItem--; 
      MovieClip(root).slideRight(); 
     } 
    } 

shade1.addEventListener(MouseEvent.CLICK, ButtonAction2); 
shade2.addEventListener(MouseEvent.CLICK, ButtonAction3); 
shade3.addEventListener(MouseEvent.CLICK, ButtonAction4); 
shade4.addEventListener(MouseEvent.CLICK, ButtonAction5); 

function ButtonAction2(eventObject:MouseEvent) {if(rnd1==1){tick.visible=true;tick.y=shade1.y;tick.x=shade1.x; score+=50; scorecounter.text = score.toString();}else{cross.visible=true;cross.y=shade1.y;cross.x=shade1.x; score-=25; scorecounter.text = score.toString();}} 
function ButtonAction3(eventObject:MouseEvent) {if(rnd1==2){tick.visible=true;tick.y=shade2.y;tick.x=shade2.x; score+=50; scorecounter.text = score.toString();}else{cross.visible=true;cross.y=shade2.y;cross.x=shade2.x; score-=25; scorecounter.text = score.toString();}} 
function ButtonAction4(eventObject:MouseEvent) {if(rnd1==3){tick.visible=true;tick.y=shade3.y;tick.x=shade3.x; score+=50; scorecounter.text = score.toString();}else{cross.visible=true;cross.y=shade3.y;cross.x=shade3.x; score-=25; scorecounter.text = score.toString();}} 
function ButtonAction5(eventObject:MouseEvent) {if(rnd1==4){tick.visible=true;tick.y=shade4.y;tick.x=shade4.x; score+=50; scorecounter.text = score.toString();}else{cross.visible=true;cross.y=shade4.y;cross.x=shade4.x; score-=25; scorecounter.text = score.toString();}} 



var score:Number = 0; 

function init():void { 
    if (score < 0) { 
     score == 0; 
    } 
    scorecounter.text = score.toString(); 
} 


/*var gameStartTime:uint; 
var gameTime:uint; 

function currTime(e:Event) { 
    gameStartTime = getTimer(); 
    gameTime = 0; 
} 

addEventListener(event.ENTER_FRAME,showTime); 

function showTime(e:Event) { 
    gameTime = getTimer()-gameStartTime; 
    gameTimeField.text = "Time: "+gameTime; 
}*/ 

init(); 

stop(); 
+0

XML存儲文本,而不是二進制數據,所以無論myxml.ques [i] .image中的任何數據應該先被解析並檢索圖像。我不知道你在這個領域是如何以及如何處理的。 – Vesper

+0

對不起,我想這是超出我的專業知識。來自xml的.image將URL存儲到我正在顯示的圖像中。當函數change_question被激發時,我想要顯示相應的圖像,我只是不知道從哪裏去。我可以在XML中創建一個單獨的加載器,但是當我試圖將它與相應的文本綁定時,我迷失了方向。謝謝! –

回答

0

所以,你有一個網址,你image領域?您肯定需要加載該圖片,但爲了知道它與哪個問題相關,您需要將問題號碼鏈接到圖片。你可以用一個說Dictionary來做到這一點,它會保存從生成的圖像加載器對象到問題編號的鏈接。舉個例子:

public var ll:Dictionary=new Dictionary(); // place alongside other global arrays 
// this will store our links 
... 
for (var i=0;i<loop;i++){ 
    questions[i]=myxml.ques[i].q1; 
    numb[i]=myxml.ques[i].qnum; 
    var imgsrc:String=myxml.ques[i].image; // img will hold images, and will be filled later 
    var l:URLLoader = new URLLoader(); 
    l.addEventListener(Event.COMPLETE, loadImage); 
    l.load(new URLRequest(imgsrc)); 
    ll[l]=i; // create a link 
}//loop 
... 
function loadImage(e:Event):void { 
    var l:URLLoader=e.target as URLLloader; 
    if (!l) return; // safety check 
    if (!ll[l]) return; // unfilled dictionary - BAD, should take measures if this triggers 
    var num:int=ll[l]; // retrieve link 
    img[num]=l; // fill the image 
    l.removeEventListener(Event.COMPLETE, loadImage); // clean up 
} 

當你決定要顯示的圖像,你最好等到它的加載,說顯示「加載中,請稍候」如果用戶點擊測驗太快,或者在普通等待所有裝載機甚至在開始測驗之前完成。錯誤處理也是必需的。