A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Global Coordinates

  1. #1
    Junior Member
    Join Date
    Mar 2005
    Location
    Canterbury
    Posts
    22

    Global Coordinates

    Hi all, I'm trying to develop my Actionscript coding skills by getting right in there and trying to program a game from scratch. All is well so far but I'm stuck, I hope this isn't too low-level a question.

    I've got separate movie clips on the stage and I've managed to get it to detect when they hit, and act appropriately. However, I need to at that point access or assign to variables the point on the stage that the object (a 'bullet') is when it hits, rather than the point that the object is within its own movieclip (the bullet path), since it's one object within another movie clip. If I assign collisionx = this._x; to the 'bullet', I get the distance across the bullet path movieclip, rather than the global position of that object on the stage. Is there an expression to get these x and y values?

    Thanks in advance.

  2. #2
    http://www.in3d.eu Kostas Zotos's Avatar
    Join Date
    Jul 2007
    Location
    Athens - Greece
    Posts
    408
    Hi,

    If I understood well.. you need to use the "localToGlobal()" method (there is also the "globalToLocal()" for the inverse conversion):

    Create and place a movie clip on the stage (_root scene) and then aceess its action script and insert this code:

    Code:
    onClipEvent (load) {
    	var Point = new Object(); //We create a new point object to keep our coordinates.
    }
    
    
    onClipEvent (mouseDown) {
    	if (this.hitTest(_root._xmouse, _root._ymouse, true)) {
    		Point.x = _xmouse;  //Get the Local x where the mouse down		
                      Point.y = _ymouse;
    		trace("Local x: "+Point.x+",  Local y: "+Point.y);
    		
    		localToGlobal(Point); //Convert the Local coordinates to Global
    		trace("Global x: "+Point.x+",  Global y: "+Point.y+"\n");
    	}
    }
    1) We initially get the cordinates to our Point object [ Point.x=_xmouse, Point.y=_ymouse]
    2) We convert them to appropriate system [localToGlobal(Point)]
    3) After conversion the Point object's x and y are actually the new Global values and can access them like this: Point.x (is now the Global x) and Point.y is the Global y position.

    Kostas
    Last edited by Kostas Zotos; 07-12-2007 at 02:54 PM.
    K. Zotos online portfolio: http://www.in3d.eu

  3. #3
    Junior Member
    Join Date
    Mar 2005
    Location
    Canterbury
    Posts
    22
    Thanks Costaz, that works perfectly.

    ADE

  4. #4
    Junior Member
    Join Date
    Mar 2005
    Location
    Canterbury
    Posts
    22

    Global to Local again

    Hi again, I seemed to get the localToGlobal method working at first though now I seem to have problems again with what should be a simple bit of code.

    The movieclip 'guideonatom' is an object within the movieclip 'atomonstage'. This code accurately traces the object's coordinates within the object, then after performing the localToGlobal function I had hoped it would then produce different values for a global position of the object after I had used the trace method. However, it produces the same values after. What is wrong with this piece of coding?

    newpoint = new object();
    newpoint.x = _root.atomonstage.guideonatom._x;
    newpoint.y = _root.atomonstage.guideonatom._y;

    trace ("Local offset of guide object on atom : "+newpoint.x+" "+newpoint.y);

    localToGlobal(newpoint);

    trace ("Global position of guide object on stage: "+newpoint.x+" "+newpoint.y);

    Thanks in advance,

    ADE

  5. #5
    Junior Member
    Join Date
    Mar 2005
    Location
    Canterbury
    Posts
    22
    I've just got it to work but by putting the code in the instance of the object itself rathet than in a seperate frameaction. I don't know why this should be but at least it's working. I'm still developing my understanding of what you can and can't do between objects and where it's best to put the coding.

    ADE

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