Copy link to clipboard
Copied
Hello,
I would like to be able to click on an object and drag it, then rotate it on its axis until I like its position. I have only be able yo drag and drop, I do not know how to implement a continuous rotation. All my shapes are movie clips. Thank you in advance, help is appreciated.
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
var rotationSpeed:int = 0;
var rotationValue:int = 0;
var shape:Array = [square,triangle,circle];
for (var i = 0; i < shape.length; i++)
shape.buttonMode = true;
for (var n = 0; n < shape.length; n++)
shape
function beginDrag(event:MouseEvent):void
{
event.target.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
function endDrag(event:MouseEvent):void
{
event.target.stopDrag();
}
stage.addEventListener(MouseEvent.CLICK, clickStage);
function clickStage(event:MouseEvent):void
{
event.target.rotation +=10;
}
There is a more fun way you can do this. Make a movieclip on stage and give it a name of shape. Put this script into the Actions for that frame:
import flash.events.MouseEvent;
shape.addEventListener(MouseEvent.MOUSE_DOWN, pressed);
var rotationValue: Number = shape.rotation;
var startMouseAngle: Number;
function pressed(e: MouseEvent) {
var dx: Number = e.stageX - shape.x;
var dy: Number = e.stageY - shape.y;
startMouseAngle = Math.atan2(dy, dx) * 180 / Math.PI;
rotationValue = shape.rotation;
...
Copy link to clipboard
Copied
There is a more fun way you can do this. Make a movieclip on stage and give it a name of shape. Put this script into the Actions for that frame:
import flash.events.MouseEvent;
shape.addEventListener(MouseEvent.MOUSE_DOWN, pressed);
var rotationValue: Number = shape.rotation;
var startMouseAngle: Number;
function pressed(e: MouseEvent) {
var dx: Number = e.stageX - shape.x;
var dy: Number = e.stageY - shape.y;
startMouseAngle = Math.atan2(dy, dx) * 180 / Math.PI;
rotationValue = shape.rotation;
stage.addEventListener(MouseEvent.MOUSE_MOVE, updaterot);
stage.addEventListener(MouseEvent.MOUSE_UP, stoprot);
}
function updaterot(e: MouseEvent) {
var dx: Number = e.stageX - shape.x;
var dy: Number = e.stageY - shape.y;
shape.rotation = rotationValue + Math.atan2(dy, dx) * 180 / Math.PI - startMouseAngle;
}
function stoprot(e: MouseEvent) {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, updaterot);
stage.removeEventListener(MouseEvent.MOUSE_UP, stoprot);
}
You can then drag the movieclip to rotate it, and it will rotate to exactly where you move the mouse.
Copy link to clipboard
Copied
Thank you!
Copy link to clipboard
Copied
Find more inspiration, events, and resources on the new Adobe Community
Explore Now