Course 7: Flash as _ Drag and Collision Detection

Source: Internet
Author: User

This article was originally published by CERNET sanbos. Please keep this information for Reprinted!

This series of Flash tutorials is designed for flash beginners by the flash mutual classroom of China tutorial network. For more tutorials and exercises, click here. Here are series of tutorials and exercises, some teachers commented and guided the exercises. Welcome to our friends!

Last section: Flash as getting started Lesson 7 section 4: depth of video editing

Section 5 drag and Collision Detection

An important advantage of as is the ability to implement interactive animation. Some special effects can be made without the use of As, but Interactive Animation cannot be implemented without the use of. Dragging MC is an example of interactive animation.

Startdrag () function or movieclip. startdrag () method:
You can use startdrag () to achieve the drag effect. Format:
MC. startdrag (a Boolean value, left, top, right, bottom );
All parameters in this method are optional. The following describes these parameters:
A boolean value: true or false. When set to true, the mouse position is locked at the registration point of the MC to be dragged. Can this parameter be useful if we want to customize the mouse pointer?
The following four edges determine the range in which MC can be dragged. It is useful to think about the parameters for controlling slide?

Perform the following exercises:

Custom mouse pointer:
Create a MC and draw one or find a mouse image you like. Should the image have a sharp position as the tip of the needle? Align the tip with the cross in the window. Return to the main scenario, drag the MC onto the stage, open the attribute panel, the Instance name is: mouse_mc; open the frame action panel input:

Mouse. Hide ();
Mouse_mc.startdrag (true );

Test the film and you will find that your mouse has become your favorite image.

Stopdrag () method:

The above describes how to start to drag MC. When you start to drag, you must stop dragging. This method is to stop dragging. Let's look back at the process of dragging a MC. Generally, you can drag the MC by clicking the left mouse button. After you open the mouse, stop dragging. Is that true? Therefore, we usually place startdrag () in the onpress event of MC (when you click the mouse), and stopdrag () in the onrelease event (when you open the mouse.

Sliding Control bar:

Sometimes we use sliding control bars, such as controlling the volume and playback progress. The slider needs to be dragged.
Use a rectangle tool to draw a slim rectangle, and fill the pen strokes with your favorite. Do you want to do anything, at least one? Convert it to MC, double-click it to enter the editing status, open the alignment panel, select the left alignment relative to the stage, return to the main scenario, open the attribute panel, and the Instance name is blt_mc. Then draw a very short rectangle and use the slider to convert it to MC. It is also the same as the slim rectangle. It is just the center alignment, and the final instance name is hk_mc.
Move blt_mc to the location where you want it to be, and move hk_mc to blt_mc.

On the frame action panel, enter:

L = blt_mc. _ x;
R = L + blt_mc. _ width;
H = blt_mc. _ y;
Hk_mc.onpress = function (){
This. startdrag ("true", l, H, R, H );
}
Hk_mc.onrelease = function (){
Stopdrag ();
Num = (hk_mc. _ x-blt_mc. _ x)/blt_mc. _ width;
}

Test the film. Drag the slider to find that the slider can only be dragged within the range of the progress bar. This is because the startdrag () method uses four parameters: l, R, and H. They are the X position of the left and right sides of the progress bar and the Y position. The Slider's moving range is limited to this range.

As shown in the code above, when the mouse is opened, a num variable is generated, which is divided by the length of the progress bar by the X coordinate of the slider minus the X coordinate of the progress bar, in this way, we can get a 0 ~ 1. With this number, we can use it for any object that requires numerical control.

 

_ Droptarget attributes:

This may happen. We drag an object and want to know if it is dragged to another object. For example, we drag an apple to a plate, we may want to know if the apple is dragged to the plate. If it is an apple, it is positioned on the plate. If it is not an apple, it will fall into the abyss. The _ droptarget attribute of MC returns the name of the MC that is overlapped by itself. For example, when an apple is dragged to a plate, the plate is pressed under the plate, and the _ droptarget attribute of the apple will return the name of the plate. However, the name returned by it is in the format of a backslash, so you need to use eval () to convert it to a standard format, such as: eval (Apple. _ droptarget) the result is: plate.

Familiarize yourself with this exercise:
Draw two gardens on the stage. Different colors are converted to MC. The instance names are mcw.mc and mc2_mc respectively. Right-click mcw.mc> arrange> move to the top layer. Open the action panel and enter:

MCX = mcw.mc. _ x;
Mcy = mcw.mc. _ y;
Mc1_mc.onpress = function (){
This. startdrag (true );
}
Mc1_mc.onrelease = function (){
Stopdrag ();
If (eval (mc1_mc. _ droptarget) = mc2_mc ){
This. _ x = mc2_mc. _ x;
This. _ y = mc2_mc. _ y;
} Else {
This. _ x = MCX;
This. _ y = mcy;
}
}

Test the video. You can drag it to mc2_mc and move it to mc2_mc. If you don't open the mouse on mc2_mc, you can move it back to the original position.

Another way to check whether a MC is dragged to another MC is collision detection. The first sentence in the Code hides the system mouse, the second sentence enables you to drag your image, and the mouse pointer is positioned at the tip of the needle. Note that you cannot drag the second Mc at a time because only one MC can be dragged at a time. When you drag the second MC, your mouse image will not be dragged any more.

_ Droptarget attributes:

This may happen. We drag an object and want to know if it is dragged to another object. For example, we drag an apple to a plate, we may want to know if the apple is dragged to the plate. If it is an apple, it is positioned on the plate. If it is not an apple, it will fall into the abyss. The _ droptarget attribute of MC returns the name of the MC that is overlapped by itself. For example, when an apple is dragged to a plate, the plate is pressed under the plate, and the _ droptarget attribute of the apple will return the name of the plate. However, the name returned by it is in the format of a backslash, so you need to use eval () to convert it to a standard format, such as: eval (Apple. _ droptarget) the result is: plate.

Familiarize yourself with this exercise:
Draw two gardens on the stage. Different colors are converted to MC. The instance names are mcw.mc and mc2_mc respectively. Right-click mcw.mc> arrange> move to the top layer. Open the action panel and enter:

MCX = mcw.mc. _ x;
Mcy = mcw.mc. _ y;
Mc1_mc.onpress = function (){
This. startdrag (true );
}
Mc1_mc.onrelease = function (){
Stopdrag ();
If (eval (mc1_mc. _ droptarget) = mc2_mc ){
This. _ x = mc2_mc. _ x;
This. _ y = mc2_mc. _ y;
} Else {
This. _ x = MCX;
This. _ y = mcy;
}
}

Test the video. You can drag it to mc2_mc and move it to mc2_mc. If you don't open the mouse on mc2_mc, you can move it back to the original position.

Another way to check whether a MC is dragged to another MC is collision detection.

Hittest () method:
This method checks whether MC interacts with a certain point or another MC ). Returns true if intersection occurs. Otherwise, returns false.

Usage: 1. intersection with a certain point: MC. hittest (X, Y, true or false );
This checks whether MC is intersecting with the points (x, y) determined by X and Y in parentheses. If the Boolean value is true, the actual chart range of MC is checked. If the Boolean value is false, the system checks whether the outer box of MC is equal to (x, y.
2. Intersection of MC and MC: MC. hittest (another MC ).

Change the above exercise to the following code to get the same result:

MCX = mcw.mc. _ x;
Mcy = mcw.mc. _ y;
Mc1_mc.onpress = function (){
This. startdrag (true );
}
Mc1_mc.onrelease = function (){
Stopdrag ();
If (this. hittest (mc2_mc )){
This. _ x = mc2_mc. _ x;
This. _ y = mc2_mc. _ y;
} Else {
This. _ x = MCX;
This. _ y = mcy;
}

}

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.