I am quite new to actionscript, and a bit stuck now. I am trying to make an arrow that is fixed at one end, but the pointy end should be draggable with mouse drag, thus streching and rotating the arrow. It would be also great if I could keep the triangle tip of the arrow from changing size while dragging it. I thought about making a movieclip composed of the tip and of the line separately, the line doing all the "stretching" while the tip simply follows around. I am just not sure how.
Most of the docs I found about mouse drag are about moving complete element not just one part while staying attached to the rest. I did find something about rotating an arrow with mouse drag here, but it's only partially helpful to my problem as it says nothing about making the arrow bigger at the same time.
Does anyone have an idea about how to realise that?
Here is one way to do this (what I would deem the easiest).
arrowInner
.arrowInner
selected/focused, hit F8 (or Modify -> Convert To Symbol) to wrap that object inside another MovieClip - hit OK on dialog box (do not check any of the options).arrowOuter
. Double click it to edit it, and align arrowInner
so the anchor point at the base of the arrow (same as you did before inside arrowInner
).Now it's time code, open the code editor on the main timeline, and paste the following (see the code comments for explanations).
//we want a function to fun every frame tick of the applicaiton
this.addEventListener(Event.ENTER_FRAME, enterFrame);
//create some helper vars that are used in the enterFrame handler
//arrowPoint is just the point of the base of the outer arrow
var arrowPoint:Point = new Point(arrowOuter.x,arrowOuter.y);
//this will store the current mouse point
var mousePoint:Point = new Point();
//this will store the radian rotation of the arrow needed to point it at the mouse
var radians:Number;
function enterFrame(e:Event):void {
//set the global mouse point
mousePoint.x = stage.mouseX;
mousePoint.y = stage.mouseY;
//calculate the distance between the two points (mouse and arrow base).
//set the height of the inner arrow to that distance
arrowOuter.arrowInner.height = Point.distance(arrowPoint, mousePoint);
//get the angle needed for the arrow to point at the mouse.
radians = Math.atan2(stage.mouseY - arrowOuter.y, stage.mouseX - arrowOuter.x);
//convert the radians to degrees, add 90 to compensate for the starting position of the arrow
arrowOuter.rotation = 90 + (radians * (180/ Math.PI));
}
If 9-slice scaling isn't your thing (it's not mine), then it's only a little more work:
Create your arrow shaft and arrow head as separate pieces. Give them the instance names head
and shaft
respectively. Create the arrow so it's pointing to the right.
Select them both, and nest them into a MovieClip (F8). Give that new container movie clip an instance name of arrow
, and make sure it's anchor point is the left most part of the shaft in middle (the opposite end from the arrow point).
use the following code:
this.addEventListener(Event.ENTER_FRAME, enterFrame);
var arrowPoint:Point = new Point(arrow.x, arrow.y);
var mousePoint:Point = new Point();
var radians:Number;
var distance:Number;
function enterFrame(e:Event):void {
mousePoint.x = stage.mouseX;
mousePoint.y = stage.mouseY;
distance = Point.distance(arrowPoint, mousePoint);
//stretch the shaft to the full distance less the size of the arrow head
arrow.shaft.width = distance - arrow.head.width;
//move the arrow head to the end of the shaft
arrow.head.x = arrow.shaft.width;
radians = Math.atan2(stage.mouseY - arrow.y, stage.mouseX - arrow.x);
arrow.rotation = radians * (180/ Math.PI);
}