我创建了一个Flash CC Professional文件,用于加载包含链接,多个文本部分和图像链接的XML数据。当用户通过程序和文本进行处理时,相应的图像应加载到名为imgholder的mc中。我已经从xml文件跟踪var'img'到增量图像加载器,但仍然无法弄清楚为什么图像没有加载到容器中。 ......哦,并没有抛出任何错误。
HELP !!提前谢谢!
第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 :(得分: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
}
每当您决定显示图片时,您最好等到它被加载,比如显示&#34;正在加载,请等待&#34;如果用户太快点击测验,或者在开始测验之前等待所有加载器完成。还需要处理错误。