Skip to main content
Participant
July 1, 2017
Answered

drag and rotate an object with a mouse click

  • July 1, 2017
  • 2 replies
  • 1866 views

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.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);

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;    

}

This topic has been closed for replies.
Correct answer Colin Holgate

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.

2 replies

Colin Holgate
Inspiring
July 1, 2017
Colin Holgate
Colin HolgateCorrect answer
Inspiring
July 1, 2017

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.

code17Author
Participant
July 2, 2017

Thank you!