PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Import images Flash using XML
A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Import images Flash using XML

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    22

    Import images Flash using XML

    Hi,

    I have a quiz working with XML questions but I want to load images into it too.
    This is the AS3 code:

    Actionscript Code:
    var questions:Array=new Array();
    var answers:Array=new Array();

    var lader:URLLoader = new URLLoader();
    lader.addEventListener(Event.COMPLETE, loadXML);
    lader.load(new URLRequest("sample.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;
            answers[i]=[myxml.ques[i].op1,myxml.ques[i].op2,myxml.ques[i].op3];
     
            }
            gotoAndPlay(2);
            }
    stop();

    /*
    trace(questions[1]);
    trace(answers[1]);
    trace(answers[1][0]);
    trace(answers[1][1]);
    trace(answers[1][2]);
    */

    That is reading out this questions from XML:

    Code:
    <ques>     
    
    <q1>Where is Copenhagen?</q1>     
    
         <op1>Denmark</op1>     
         <op2>Sweden</op2>     
         <op3>Finland</op3>   
    
    </ques>   
    
    <ques>     
    
    <q1>Where is Lima?</q1>     
    
         <op1>Peru</op1>     
         <op2>Argentina</op2>     
         <op3>Chile</op3>   
    
    </ques> 
    
    <ques>     
    
    <q1>Where is Amsterdam?</q1>     
    
         <op1>Netherlands</op1>     
         <op2>Belgium</op2>     
         <op3>Chile</op3>   
    
    </ques>
    
    <ques>     
    
    <q1>Where is Brussel?</q1>     
    
         <op1>Belgium</op1>     
         <op2>Netherlands</op2>     
         <op3>France</op3>   
    
    </ques>
    
    <ques>     
    
    <q1>Where is Paris?</q1>     
    
         <op1>France</op1>     
         <op2>Netherlands</op2>     
         <op3>Poland</op3>   
    
    </ques>
    How can I load external images using this ActionScript code?
    Tnx

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    var questions:Array=new Array();
    var answers:Array=new Array();
    var imgs:Array=new Array();
    var lader:URLLoader = new URLLoader();
    lader.addEventListener(Event.COMPLETE, loadXML);
    lader.load(new URLRequest("sample.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;
    		answers[i]=[myxml.ques[i].op1,myxml.ques[i].op2,myxml.ques[i].op3];
    		imgs[i]=myxml.ques[i].img;
    	}
    	trace(imgs[1]);
    	trace(questions[1]);
    	trace(answers[1][0]);
    	trace(answers[1][1]);
    	trace(answers[1][2]);
    	gotoAndPlay(2);
    }
    stop();
    Code:
      <ques>
        <img>map1.jpg</img>
        <q1>Where is Copenhagen?</q1>
        <op1>Denmark</op1>
        <op2>Sweden</op2>
        <op3>Finland</op3>
      </ques>
      <ques>
        <img>map2.jpg</img>
        <q1>Where is Lima?</q1>
        <op1>Peru</op1>
        <op2>Argentina</op2>
        <op3>Chile</op3>
      </ques>
      <ques>
        <img>map3.jpg</img>
        <q1>Where is Amsterdam?</q1>
        <op1>Netherlands</op1>
        <op2>Belgium</op2>
        <op3>Chile</op3>
      </ques>

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    22
    Hi,

    Thnx for your reply! I did the changes you made and they doesn't seem te work. When you trace the [img] it shows nothing so something isnt working correctly. The question and answers are traced correctly.

    And the second frame I have this code

    Code:
    var qno=5;var rnd1; var rnd2;
    tick.visible=false;cross.visible=false;
    var right_answers=0;//var wrong_answers=0;
    
    function change_question(){
    	if(tick.visible==true){right_answers=right_answers+50;} 
    	//if(cross.visible==false){wrong_answers=wrong_answers--;}
    	if(qno==questions.length){gotoAndPlay(2);}else{
    	tick.visible=false;cross.visible=false;
    	rnd1=Math.ceil(Math.random()*3);
    	rnd2=Math.ceil(Math.random()*questions.length)-1;
    	q.text=questions[rnd2];
    	if(questions[rnd2]=="x"){change_question();}
    	questions[rnd2]="x";
    	enable_disable(1);
    	if(rnd1==1){opt1.text=answers[rnd2][0];opt2.text=answers[rnd2][1];opt3.text=answers[rnd2][2];}
    	if(rnd1==2){opt1.text=answers[rnd2][2];opt2.text=answers[rnd2][0];opt3.text=answers[rnd2][1];}
    	if(rnd1==3){opt1.text=answers[rnd2][1];opt2.text=answers[rnd2][2];opt3.text=answers[rnd2][0];}
    	}}
    	
    function enable_disable(a){
    	if(a==0){shade1.mouseEnabled=false;shade2.mouseEnabled=false;shade3.mouseEnabled=false;}
    	if(a==1){shade1.mouseEnabled=true;shade2.mouseEnabled=true;shade3.mouseEnabled=true;}}
    	
    change_question();
    
    next_b.addEventListener(MouseEvent.CLICK, ButtonAction1);
    function ButtonAction1(eventObject:MouseEvent) {qno++;change_question();} 
    
    shade1.addEventListener(MouseEvent.CLICK, ButtonAction2);
    shade2.addEventListener(MouseEvent.CLICK, ButtonAction3);
    shade3.addEventListener(MouseEvent.CLICK, ButtonAction4);
    
    function ButtonAction2(eventObject:MouseEvent) {enable_disable(0);if(rnd1==1){tick.visible=true;tick.y=shade1.y}else{cross.visible=true;cross.y=shade1.y}}
    function ButtonAction3(eventObject:MouseEvent) {enable_disable(0);if(rnd1==2){tick.visible=true;tick.y=shade2.y}else{cross.visible=true;cross.y=shade2.y}}
    function ButtonAction4(eventObject:MouseEvent) {enable_disable(0);if(rnd1==3){tick.visible=true;tick.y=shade3.y}else{cross.visible=true;cross.y=shade3.y}}
    
    stop();
    But I dont think I have to change anything here or do I?

    Can somebody help me out with this?

    Thank you
    Last edited by Joepiooo; 04-20-2010 at 02:38 AM.

  4. #4
    Senior Member
    Join Date
    Aug 2006
    Posts
    322

    dawsonk AS code is perfect if you have a well formed xml file

    <?xml version="1.0" ?>
    <quiz>
    <ques>
    <img>map1.jpg</img>
    <q1>Where is Copenhagen?</q1>
    <op1>Denmark</op1>
    <op2>Sweden</op2>
    <op3>Finland</op3>
    </ques>
    <ques>
    <img>map2.jpg</img>
    <q1>Where is Lima?</q1>
    <op1>Peru</op1>
    <op2>Argentina</op2>
    <op3>Chile</op3>
    </ques>
    <ques>
    <img>map3.jpg</img>
    <q1>Where is Amsterdam?</q1>
    <op1>Netherlands</op1>
    <op2>Belgium</op2>
    <op3>Chile</op3>
    </ques>
    </quiz>

    It will much easier to give the solution if you submit a sample.fla here (i am using Cs3).

    marlopax

  5. #5
    Junior Member
    Join Date
    Apr 2009
    Posts
    22
    Yes your right this is not the best way to show.

    In the attachment are the .fla and .xml

    Tnx
    Attached Files Attached Files

  6. #6
    Senior Member
    Join Date
    Aug 2006
    Posts
    322
    some error in your zip file......

    marlopax

  7. #7
    Junior Member
    Join Date
    Apr 2009
    Posts
    22
    Ohw that happens a few times before...

    Here is a link that contains the .fla and .xml
    Its in a .rar file

    http://www.megaupload.com/?d=BB7EZFBO

    Thank you for the help
    Last edited by Joepiooo; 04-20-2010 at 10:26 AM.

  8. #8
    Senior Member
    Join Date
    Aug 2006
    Posts
    322
    your fla is in Cs4, I'm using Cs3

  9. #9
    Junior Member
    Join Date
    Apr 2009
    Posts
    22
    Ohw ok, here is the CS3 file

    http://www.megaupload.com/?d=04WB67N4

    Tnx for your patience

  10. #10
    Senior Member
    Join Date
    Aug 2006
    Posts
    322

    loading an Image XML

    Create a mc named imageArea on stage.

    Code of 2nd Frame:

    var qno=5;
    var rnd1;
    var rnd2;
    tick.visible=false;
    cross.visible=false;
    var right_answers=0;//var wrong_answers=0;
    var Holder:Loader = new Loader();
    function change_question() {
    if (tick.visible==true) {
    right_answers=right_answers+1234;
    }
    //if(cross.visible==false){wrong_answers=wrong_answe rs--;}
    if (qno==questions.length) {
    gotoAndPlay(2);
    } else {
    tick.visible=false;
    cross.visible=false;
    rnd1=Math.ceil(Math.random()*3);
    rnd2=Math.ceil(Math.random()*questions.length)-1;
    var urlrHome:URLRequest = new URLRequest(imgs[rnd2]);
    Holder.load(urlrHome);
    Holder.contentLoaderInfo.addEventListener(Event.CO MPLETE, movieLoaded);
    function movieLoaded(event:Event):void {
    imageArea.addChild(Holder);
    }
    q.text=questions[rnd2];
    if (questions[rnd2]=="x") {
    change_question();
    }
    questions[rnd2]="x";
    enable_disable(1);
    if (rnd1==1) {
    opt1.text=answers[rnd2][0];
    opt2.text=answers[rnd2][1];
    opt3.text=answers[rnd2][2];
    }
    if (rnd1==2) {
    opt1.text=answers[rnd2][2];
    opt2.text=answers[rnd2][0];
    opt3.text=answers[rnd2][1];
    }
    if (rnd1==3) {
    opt1.text=answers[rnd2][1];
    opt2.text=answers[rnd2][2];
    opt3.text=answers[rnd2][0];
    }
    }
    }
    function enable_disable(a) {
    if (a==0) {
    shade1.mouseEnabled=false;
    shade2.mouseEnabled=false;
    shade3.mouseEnabled=false;
    }
    if (a==1) {
    shade1.mouseEnabled=true;
    shade2.mouseEnabled=true;
    shade3.mouseEnabled=true;
    }
    }
    change_question();
    next_b.addEventListener(MouseEvent.CLICK, ButtonAction1);
    function ButtonAction1(eventObject:MouseEvent) {
    qno++;
    change_question();
    }

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

    function ButtonAction2(eventObject:MouseEvent) {
    enable_disable(0);
    if (rnd1==1) {
    tick.visible=true;
    tick.y=shade1.y;
    } else {
    cross.visible=true;
    cross.y=shade1.y;
    }
    }
    function ButtonAction3(eventObject:MouseEvent) {
    enable_disable(0);
    if (rnd1==2) {
    tick.visible=true;
    tick.y=shade2.y;
    } else {
    cross.visible=true;
    cross.y=shade2.y;
    }
    }
    function ButtonAction4(eventObject:MouseEvent) {
    enable_disable(0);
    if (rnd1==3) {
    tick.visible=true;
    tick.y=shade3.y;
    } else {
    cross.visible=true;
    cross.y=shade3.y;
    }
    }

    stop();



    XML:

    <?xml version="1.0" ?>
    <all>
    <ques>
    <img>map1.jpg</img>
    <q1>Where is Seoul?</q1>
    <op1>South Korea</op1>
    <op2>North Korea</op2>
    <op3>Japan</op3>
    </ques>
    <ques>
    <img>map2.jpg</img>
    <q1>Where is Bucharest?</q1>
    <op1>Romania</op1>
    <op2>Hungary</op2>
    <op3>Poland</op3>
    </ques>
    <ques>
    <img>map3.jpg</img>
    <q1>Where is Copenhagen?</q1>
    <op1>Denmark</op1>
    <op2>Sweden</op2>
    <op3>Finland</op3>
    </ques>
    <ques>
    <img>mier.jpg</img>
    <q1>Where is Lima?</q1>
    <op1>Peru</op1>
    <op2>Argentina</op2>
    <op3>Chile</op3>
    </ques>
    <ques>
    <img>map4.jpg</img>
    <q1>Where is Amsterdam?</q1>
    <op1>Netherlands</op1>
    <op2>Belgium</op2>
    <op3>Chile</op3>
    </ques>
    <ques>
    <img>map5.jpg</img>
    <q1>Where is Brussel?</q1>
    <op1>Belgium</op1>
    <op2>Netherlands</op2>
    <op3>France</op3>
    </ques>
    <ques>
    <img>map6.jpg</img>
    <q1>Where is Paris?</q1>
    <op1>France</op1>
    <op2>Netherlands</op2>
    <op3>Poland</op3>
    </ques>
    <ques>
    <img>map7.jpg</img>
    <q1>Where is London?</q1>
    <op1>Great Britain</op1>
    <op2>Ireland</op2>
    <op3>America</op3>
    </ques>
    <ques>
    <img>map8.jpg</img>
    <q1>Where is New York?</q1>
    <op1>USA</op1>
    <op2>Great Britain</op2>
    <op3>Brasil</op3>
    </ques>
    <ques>
    <img>map9.jpg</img>
    <q1>Where is Ankara?</q1>
    <op1>Turkey</op1>
    <op2>Iran</op2>
    <op3>Iraq</op3>
    </ques>
    <ques>
    <img>map10.jpg</img>
    <q1>Where is Arnhem?</q1>
    <op1>Netherlands</op1>
    <op2>Luxemburg</op2>
    <op3>Liechtenstein</op3>
    </ques>
    <ques>
    <img>map11.jpg</img>
    <q1>Who is Obama</q1>
    <op1>President of USA</op1>
    <op2>President of Great Britain</op2>
    <op3>President of Bahama's</op3>
    </ques>
    <ques>
    <img>map12.jpg</img>
    <q1>What is 1+1</q1>
    <op1>2</op1>
    <op2></op2>
    <op3>1</op3>
    </ques>
    </all>


    This will be a beginner method I coded here. You will have to go for more advance code for complex xml structure.

    Hope this will work for now.

    Enjoy


    marlopax
    Last edited by marlopax; 04-21-2010 at 04:17 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center