AS3 Global and local coordinate transformations

Source: Internet
Author: User

In most games that require user clicks, the conversion of coordinates is a method that must be mastered.

First in a 700x700 stage to create 2 blocks, the red large square A is 600x600, located in (50,50), green small square B is 300x300. A is the parent container of B, and B is located relative to the stage (200,200).

Next, do a series of tests to illustrate how these 2 methods work.
Let's say we point to the center point of the B block, which is global (350,350), A (300,300), B (150,150), and see what the results will look like:

Private Function OnClickHandler (e:mouseevent): void
{

E.stoppropagation ();
var stagepoint:point = new Point (Stage.mousex, Stage.mousey);
Trace (stagepoint.x);//350
Trace (STAGEPOINT.Y);//350
Trace (Stage.globaltolocal (stagepoint). x);//350
Trace (Stage.globaltolocal (stagepoint). y);//350
Trace (Smallshape.globaltolocal (stagepoint). x);//150
Trace (Smallshape.globaltolocal (stagepoint). y);//150
Trace (Bigshape.globaltolocal (stagepoint). x);//300
Trace (Bigshape.globaltolocal (stagepoint). y);//300
var localpoint:point = new Point (E.localx, e.localy);
Trace (localpoint.x);//150
Trace (LOCALPOINT.Y);//150
Trace (Stage.localtoglobal (localpoint). x);//150
Trace (Stage.localtoglobal (localpoint). y);//150
Trace (Smallshape.localtoglobal (localpoint). x);//350
Trace (Smallshape.localtoglobal (localpoint). y);//350
Trace (Bigshape.localtoglobal (localpoint). x);//200
Trace (Bigshape.localtoglobal (localpoint). y);//200

}

The test results show that the Globaltolocal method invokes the same global coordinate (350,350) by whom it is converted to whose local coordinates. The Localtoglobal method is slightly more complicated, and whoever calls Localtoglobal is the equivalent of adding the caller's coordinates to the stage on the basis of Localpoint (150,150).

After understanding how these 2 methods work, it's easy to say that we're going to turn it into a block by clicking on the B-block position:

Private Function OnClickHandler (e:mouseevent): void
{

var localpoint:point = new Point (E.localx, e.localy);

var stagepoint:point = Smallshape.localtoglobal (Localpoint),//because the clicked position is within the B block, so called by B

Localpoint = bigshape.globaltolocal (stagepoint);//Click on the coordinates of the A block

}

AS3 Global and local coordinate transformations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.