Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

drag and rotate an object with a mouse click

New Here ,
Jul 01, 2017 Jul 01, 2017

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;    

}

TOPICS
ActionScript
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jul 01, 2017 Jul 01, 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;

 

...
Translate
LEGEND ,
Jul 01, 2017 Jul 01, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 01, 2017 Jul 01, 2017
LATEST

Thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 01, 2017 Jul 01, 2017
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines