AS3.0 instance learning is familiar with the new event mechanism and the application of Addchild _flash as

Source: Internet
Author: User
Tags addchild
First of all declare: This is a rookie, just contact AS3 soon, many ideas have not had time to instill, these case are from the internet down, but because the explanation is English, unfavorable to our study, I will act as a translater, by the way can let oneself consolidate knowledge. The level is limited, the mistake unavoidably, welcome prawns shrimp, Big bird bird to correct.

Let's get down to business:

Case 1: Familiar with the new event mechanism and the use of Addchild

Description: Drag the villain to the skateboard, and then drag the skateboard, you can find that the villain has been glued together with the skateboard.
Demo: http://www.live-my-life-with-yuyi.com/as3_cases/changing_parents/

Code:
Copy Code code as follows:

Boarder_mc.addeventlistener (Mouseevent.mouse_down, drag);
Boarder_mc.addeventlistener (mouseevent.mouse_up, drop);
Red_mc.addeventlistener (Mouseevent.mouse_down, drag);
Red_mc.addeventlistener (mouseevent.mouse_up, drop);
Blue_mc.addeventlistener (Mouseevent.mouse_down, drag);
Blue_mc.addeventlistener (mouseevent.mouse_up, drop);

AS3 has adopted a new monitoring mechanism, and noted that each MC's mouse button is used in the same function, which needs to be in the function to judge the occurrence of the event, so as to make corresponding processing.

Copy Code code as follows:

function drag (event:mouseevent): void
{
if (Event.target.name = = "BOARDER_MC")
{
AddChild (BOARDER_MC);
Event.target.startDrag (TRUE);
boarder_mc.x = MouseX;
Boarder_mc.y = Mousey;
}
Else
{
Event.target.startDrag ();
}
}

Through Event.target.name to get the object name of the event, AS3 no root,addchild equivalent to a MC moved to the stage, is actually Timeline0.addchild (MC), At the same time ensure that the MC is on the top floor of the stage (AS3 in the deep management).
To get the object that the event occurs through Event.target

Then look at the drop function.

Copy Code code as follows:

function Drop (event:mouseevent): void
{
Event.target.stopDrag ();
if (Boarder_mc.hittestobject (RED_MC))
{
Red_mc.addchild (BOARDER_MC);
boarder_mc.x = 0;
BOARDER_MC.Y = 0;
}
else if (Boarder_mc.hittestobject (BLUE_MC))
{
Blue_mc.addchild (BOARDER_MC);
boarder_mc.x = 0;
BOARDER_MC.Y = 0;
}
}
When the mouse bounces, what also regardless, first stop dragging, if the villain and the skateboard has coincident area, will put the villain into the skateboard, skateboarding like a container,boarder_mc became RED_MC child, will Boarder_ The MC's x and Y coordinates are clear 0 to put the villain in the correct position on the skateboard.

If the villain has become the child of the skateboard, then drag the skateboard again, because of another addchild (BOARDER_MC), so the skateboard's parent has become the stage.

The entire Case 1 analysis to this end, involves little knowledge, mainly is familiar with the AS3 grammar.
Package download

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.