【问题标题】:loading image from xml issue as3从 xml 问题 as3 加载图像
【发布时间】:2016-06-13 07:56:49
【问题描述】:

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

帮助!!提前致谢!

第 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();

【问题讨论】:

  • XML 存储文本,而不是二进制数据,所以无论myxml.ques[i].image 中的什么数据都应该首先被解析并检索图像。我看不出你是如何从那个领域处理什么的。
  • 抱歉,我想这超出了我的专业范围。 xml 中的 .image 将 URL 存储到我要显示的图像。当函数 change_question 被触发时,我希望显示相应的图像,我只是不知道从那里去哪里。我可以在 XML 中创建一个单独的加载器,但是当我尝试将它与相应的文本绑定时,我迷路了。谢谢!

标签: xml actionscript-3 flash


【解决方案1】:

那么,您的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
}

当您决定显示图像时,最好等到它加载完毕,如果用户点击测验太快,请说“正在加载,请稍候”,或者在启动之前等待所有加载器完成测验。还需要错误处理。

【讨论】:

    猜你喜欢
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多