繁体   English   中英

从XML问题as3加载图像

[英]loading image from xml issue as3

我创建了一个Flash CC Professional文件,该文件加载XML数据,其中包括链接,多个文本部分和图像链接。 当用户通过程序和文本前进时,相应的图像应加载到名为imgholder的mc中。 我已经从xml文件中将var'img'跟踪到了增量图像加载器,但是仍然无法弄清楚为什么图像没有加载到容器中。 ...哦,它不会引发任何错误。

救命!! 提前致谢!

框架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();

因此,您在image字段中有一个URL? 您肯定需要加载该图像,但是为了知道它涉及什么问题,您需要将问题编号链接到该图像。 您可以使用“说Dictionary来做到这一点,该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
}

每当决定显示图像时,如果用户单击测验的速度过快,则最好等到图像载入后再显示“正在加载,请稍候”,或者简单地等待所有加载程序完成,甚至不开始测验。 错误处理也是必需的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM