Object intersection detection example

Source: Internet
Author: User

In Flex programming, we sometimes need to determine whether two objects are intersecting using the obj1.hittestobject (obj2) method.

The following is a specific example.

 

Code

<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" bordercolor = "# ffffff">
<Mx: SCRIPT>
<! [CDATA [
Import MX. Controls .*;
Import MX. Core .*;

Private var lines: uicomponent = new uicomponent ();
Private var lockx: Int = 0;
Private var locky: Int = 0;
Private var ismove: Boolean = false;

Private function draw (): void {

Lines. ID = "line_1 ";
Lines. Graphics. linestyle (2, 0x0099ff, 1 );
Lines. Graphics. moveTo (100,100 );
Lines. Graphics. lineto (300,300 );
Stage. addeventlistener (mouseevent. mouse_down, function (E: mouseevent ){
VaR OBJ: uicomponent=uicomponent(e.tar get );
If(e.tar get is uicomponent & obj. ID = "line_1 ")
{
Lockx = mouseX-lines.x;
Locky = mouseY-lines.y;
Ismove = true;
}
});
Stage. addeventlistener (mouseevent. mouse_move, mousemovehandler );
Stage. addeventlistener (mouseevent. mouse_up, function (E: mouseevent)
{
Ismove = false;
});

Addelement (lines );
}

Private function mousemovehandler (Event: mouseevent): void {
If (ismove)
{
Lines. x = This. mousex-lockx;
Lines. Y = This. mousey-locky;
If (lines. hittestobject (mycanvas ))
{
Lblmiddle. Text = "in ";
}
Else
{
If (lblmiddle. Text = "coming in ")
{
Lblmiddle. Text = "out ";
}
}
}
}
]>
</MX: SCRIPT>

<Mx: canvas id = "mycanvas" x = "200" Y = "200" width = "100" Height = "100" alpha = "0.5" backgroundcolor = "0xff0000"> </MX: canvas>
<Mx: button x = "20" Y = "20" label = "Draw line" Click = "Draw ()"/>
<Mx: label x = "300" Y = "300" width = "400" Height = "200" fontsize = "72" color = "0xff0000" id = "lblmiddle"/>


</MX: Application>

 

Whether the test point and the object are intersection.

Mycanvas. hittestpoint (X, Y, true)

The target object of mycanvas.

 

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.