FLASH as mouse dragging and rotating MC tutorial

Source: Internet
Author: User
Tags time interval

On the Internet to see a game both drag and drop the MC and the MC, drag and drop with the mouse event to achieve, and rotation needs to use the shift+ mouse click event. It always feels too much trouble. So I hope I can find a way to pull and rotate at the same time with the mouse.

It turns out that mouse_down+mouse_up is equivalent to a click event. That is, each release of the left mouse button, stop dragging, the MC will rotate once, and this is obviously not the expected effect.

So, it begins to understand why the programmer added the SHIFT key to the listening.

But can't this function be implemented without the SHIFT key? I started thinking about how to distinguish mouse_down+mouse_up from click.

Method One: Set the time interval. Time is not very sure, pass away.

Method Two: Using the relative position of the mouse. If the MC is not moved, it is considered the click event. Positive Solution!

code is as follows: package{


import flash.display. *;


import Flash.geom. Point;


import flash.events.*;





public class Main extends sprite{


private Var mc:mc;


private Var Clickoffset:point;


private Var Mouseloc:point;





Public Function Main () {


mc=new MC ();


AddChild (MC);


mc.x=200;


mc.y=200;


Mc.addeventlistener (Mouseevent.click, rot);


Mc.addeventlistener (Mouseevent.mouse_down,startdrag);


Mc.addeventlistener (event.enter_frame., drag);


Stage.addeventlistener (Mouseevent.mouse_up,stopdrag);


}


Private Function StartDrag (e:mouseevent) {


mouseloc=new Point (E.stagex,e.stagey);


clickoffset=new Point (E.STAGEX-MC.X,E.STAGEY-MC.Y);


}


Private Function Drag (e:event) {


if (clickoffset==null) return;


Mc.x=mousex-clickoffset.x;


Mc.y=mousey-clickoffset.y;


}


Private Function Stopdrag (e:mouseevent) {


Clickoffset=null;


}


Private Function Rot (e:mouseevent) {


if (!mouseloc.equals (E.stagex,e.stagey)) return;


mc.rotation+=30;





}


}


}

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.