Skip navigation
Currently Being Moderated

reposition a movieclip on stage with coordinates from an xml

Aug 4, 2012 9:58 AM

Hello i am new to this forum, and kinda a beginner with actionscript. what i'm trying to do is reposition a movieclip already with the coordinates that will be in an XML file on my server. But so far no luck.

 

 

This is what i have for AS3

 

var my_x:Number;

var my_y:Number;

var myCoord:XML;

 

sample_mc.x = my_x;

sample_mc.y = my_y;

 

var myXMLLoader:URLLoader = new URLLoader();

myXMLLoader.load(new URLRequest("coords.xml"));

myXMLLoader.addEventListener(Event.COMPLETE, processXML);

 

 

function processXML (e:Event):void{

var myXML:XML = new XML(e.target.data);

myCoord = myXML.position.pos1;

 

my_x = myCoord.@XPOSITION;

my_y = myCoord.@YPOSITION;

 

}

 

 

And the XML file

 

< position>

 

 

< pos1 XPOSITION="100" YPOSITION="55">

< /pos1>

 

 

< /position>

 

let me know if anyone can correct what i'm doing wrong, please.

 
Replies
  • kglad
    72,198 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 4, 2012 10:24 AM   in reply to 4morrone

    use:

     

     

     

    var my_x:Number;

    var my_y:Number;

    var myCoord:XML;

     

    sample_mc.x = my_x;

    sample_mc.y = my_y;

     

    var myXMLLoader:URLLoader = new URLLoader();

    myXMLLoader.load(new URLRequest("coords.xml"));

    myXMLLoader.addEventListener(Event.COMPLETE, processXML);

     

     

    function processXML (e:Event):void{

    var myXML:XML = new XML(e.target.data);

    myCoord = myXML.pos1;

     

    my_x = Number(myCoord.@XPOSITION);

    my_y = Number(myCoord.@YPOSITION);

     

    }

     

     

     
    |
    Mark as:
  • kglad
    72,198 posts
    Jul 21, 2002
    Currently Being Moderated
    Aug 4, 2012 10:56 AM   in reply to 4morrone

    are you assigning an x,y for MC?

     

    p.s.  if sample_mc is MC, you need to assign its x,y after loading of the xml and those variables are defined.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 4, 2012 10:57 AM   in reply to 4morrone

    If sample_mc is the movieclip you want to place, you need to wait until after the xml file is loaded and processed before assigning it to those values.  If you change the mc_x/mc_y values, the sample_mc will not automatically adjust with them.  It is only set when it is assigned to the values they currently hold.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 4, 2012 12:30 PM   in reply to 4morrone

    In your processXML function you would assign the values...

     

    function processXML (e:Event):void{

        var myXML:XML = new XML(e.target.data);

        myCoord = myXML.pos1;

        sample_mc.x = my_x = Number(myCoord.@XPOSITION);

        sample_mc.y = my_y = Number(myCoord.@YPOSITION);

    }

     

    I only include the mc_x and mc_y in the event you use them for some other purpose

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 4, 2012 3:18 PM   in reply to 4morrone

    If I run your code as shown and repair the xml to not have spaces in the start of the tags, then I get an error regarding :

     

    TypeError: Error #1034: Type Coercion failed: cannot convert XMLList@267470a1 to XML.

     

    for line the line where you assign myCoord.  This tells me that you need to EITHER declare myCoord as an XMLList object instead of an XML object, as in...

     

       var myCoord:XMLList;

     

    OR, in the function you need to force myCoord to be an XML object, as in...

     

        myCoord = new XML(myXML.pos1);

     

    Have you been getting error messages?

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 4, 2012 4:03 PM   in reply to 4morrone

    You can help others to help you by providing the complete error messages that you are getting when you post your questions/responses.  It can save alot of time finding the problem. 

     

    You should mark kGlad's response as being helpful.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 4, 2012 5:14 PM   in reply to 4morrone

    Just in case... When I mentioned you should mark kGlad's response as helpful I was not intending you to use the...

     

    Was this helpful? Yes  No

     

    that you see in the posting.  As the original poster you have have the ability to award points to people that help you.  While I am not sure what it looks like, it is not the bit I showed above.  There should be other options to mark a posting as correct or helpful.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 4, 2012 5:16 PM   in reply to 4morrone

    Yes, or both for different postings that helped you (only one correct can be awarded).

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 4, 2012 5:52 PM   in reply to 4morrone

    THere used to be a limit of two helpful, and there may still be, but I think it has slipped a gear for awhile now and might allow more.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 5, 2012 4:41 AM   in reply to 4morrone

    Thanks for testing that... it's good info to know.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points