AS3 Drag Object Implementation method

Source: Internet
Author: User

AS3 Drag Object Implementation method

There are startdrag functions inside the flash as3.0
Mc.startdrag (False, new rectangle (0, 0, 100, 0));
So MC This component can only drag the drag range in the x-axis direction is 0-100
Mc.startdrag (False, new Rectangle (10, 20, 100, 200));
So the MC is able to drag in the x direction 10-100 y axis 20-200 such a rectangular area
I hope it will help you.

User1.addeventlistener (Mouseevent.mouse_down, MOUSEDOWN1)

function Mousedown1 (event:mouseevent): void
{
User1.startdrag (False, new rectangle (0, 0, 100, 0));
}

Instance Code

Package
{
Import Flash.display.sprite;
Import Flash.display.displayobject;
Import flash.events.mouseevent;
Import Flash.geom.point;
Import Flash.filters.drops tutorial Hadowfilter;

public class Classdrag extends Sprite
{
private Var _red:sprite;
private Var _blue:sprite;
private Var _green:sprite;
private Var _white:sprite;

Object being dragged raw coordinates
private Var _startinglocation:point;

Constructors
Public Function Classdrag ()
{
Init ();
}

Class
Private Function init (): void
{
_red = new Sprite ();
Createrectangle (_red, 0xff0000, 10, 10, 20, 20);
_blue = new Sprite ();
Createrectangle (_blue, 0X0000FF, 10, 35, 20, 20);
_green = new Sprite ();
Createrectangle (_green, 0X00CC00, 10, 60, 20, 20);
_white = new Sprite ();
Createrectangle (_white, 0xf0f0f0, MB, m, m, false);
}

Create a moment type
Private Function Createrectangle (Rectangle:sprite, Color:uint, X:uint, Y:uint, Width:uint, height:uint, Drag:boolean = true): void
{
Rectangle.graphics.beginfill (color);
Rectangle.graphics.drawrect (x, y, width, height);
Rectangle.graphics.endfill ();
Addchild (Rectangle);

if (drag)
{
Listening for events
Rectangle.addeventlistener (Mouseevent.mouse_down, pickup);
Rectangle.addeventlistener (mouseevent.mouse_up, place);
}
}

Drag
Private function Pickup (event:mouseevent): void
{
Save original coordinates
_startinglocation = new Point ();
_startinglocation.x = Event.target.x;
_STARTINGLOCATION.Y = Event.target.y;

Start dragging
Event.target.startdrag ();
Add Shadow
Event.target.filters = [New Dropshadowfilter ()];
Drag objects to the front display
Setchildindex (Displayobject (event.target), numchildren-1);
}

Drag End
Private Function Place (event:mouseevent): void
{
Drag End
Event.target.stopdrag ();
Clear Shadow
Event.target.filters = null;

Detect if the target area has been moved
if (Event.target.droptarget = = _white)
{
var color:uint;
Switch (event.target)
{
Case _red:color = 0xff0000; Break
Case _blue:color = 0x0000ff; Break
Case _green:color = 0x00cc00; Break
}

Re-create the target area
_white.graphics.clear ();
Createrectangle (_white, color, of,, and false);
}

Drag and drop objects back to position
Event.target.x = _startinglocation.x;
Event.target.y = _STARTINGLOCATION.Y;
}
}
}

And how to limit his drag range, that is, when the object is dragged to the right, the original left side of the display after all the things, then can not drag left.

First, take the object you want to drag to an instance name: I use MC1 here (MC1 on stage)
One, MC1 horizontal drag
Write on the first frame of the main timeline:

This.addeventlistener (EVENT.ENTER_FRAME,FUN1)
function Fun1 (evt:event): void{
Mc1.x=mousex
}

Second, MC1 vertical drag

This.addeventlistener (EVENT.ENTER_FRAME,FUN1)
function Fun1 (evt:event): void{
Mc1.y=mousey
}

Oh, just to change the x to Y;
You can add some conditions inside to implement the restriction of the drag range, for example:

This.addeventlistener (EVENT.ENTER_FRAME,FUN1)
function Fun1 (evt:event): void{
if (mc1.x<250) {//when the coordinates of the MC1 are less than 250, drag horizontally, you can change to another condition
Mc1.x=mousex
}
}

Oh, the general idea is such, there is nothing to understand the place, welcome to cross-examine Hello, my code is like this.
I want me to start horizontal drag when I click on User1, stop when I release, and limit the range that it can drag

User1.addeventlistener (Mouseevent.mouse_down, MOUSEDOWN1)

function Mousedown1 (event:mouseevent): void
{
User1.startdrag ();
}

User1.addeventlistener (mouseevent.mouse_up, mousereleased1);
 
Function mousereleased1 (event:mouseevent): void
{
     user1.stopdrag (); br>    
}    answer var aa=0       //act as switch
User1.addeventlistener (Mouseevent.mouse_down, mousedown1)  
function Mousedown1 (event:mouseevent): void {
    aa=1  //switch on
}
User1.addeventlistener (mouseevent.mouse_up, mousereleased1 );
Function Mousereleased1 (event:mouseevent): void {
     aa=0 //Switch off
}
AddEventListener (event.enter_frame,fun1)
function fun1 (evt:event): void{
 if (aa==1) {
 user1.x =mousex//here is the level, change x to Y is perpendicular to the
 }
 }   

  Ask the last question is, if I am inside an original, how do I control the behavior in another form in the scene?
I remember the previous AS2 in which root can be done
is a.a_1.a_1_1, in this a_1_1 to control   B, and b_1?0.0?    answer this is the path of the problem
AS2 inside if you want to control the x  of a on stage you should write: _root.a._x= your control
   also you have to control the A_1_1 x in the a_1 on the stage A, that is _root.a.a_1.a_1_1.x= You can write this in your control
As3: MovieClip (Root). a.x= your control
                    MovieClip (root). a.a_1.a_1_1.x= your control
The above are all absolute paths, So no matter where you are in the movie you can control the

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.