I have code like this:

touchX = e.stageX;
touchY = e.stageY;

I also have a player movieclip that I'm trying to make shoot lasers. So I got the MovieClip to appear and rotate according to the player's location and the user's touchX and touchY. How do I make the laser MovieClip stretch according to the distance between the player's coordinates and the touchX and touchY coordinates?


Simply have the pivot point in the center of your laser clip. Calculate the distance between the player and the input.

var dx:Number = touchX - player.x; //Assuming SAME coordinate space
var dy:Number = touchY - player.y; // Else use localToGlobal
var halfdist:Number = Math.sqrt(dx*dx+dy*dy)/2;

Now scale your movieclip by distance.

   laser.width = halfdist;

Rotate your laser

var rot:Number = Math.atan2(dy,dx); 
laser.rotation = rot * (180*Math.PI);

Place your Laser by rotation:

 laser.x = Math.cos(rot) * halfdist + player.x;
 laser.y = Math.sin(rot) * halfdist + player.y;
  • \$\begingroup\$ This code is good, but edit it and change Math.Pi to math.PI, the i should be capital. Also, when I run this code, the scaleX and scaleY seem to change. \$\endgroup\$ – user25191 Apr 18 '13 at 22:08
  • \$\begingroup\$ Well im only touching the Width though. scaleY shouldn't be affected \$\endgroup\$ – Sidar Apr 18 '13 at 22:16
  • \$\begingroup\$ EDIT the height changes because the object is rotating... \$\endgroup\$ – user25191 Apr 18 '13 at 22:27
  • \$\begingroup\$ Rotation shouldn't affect the scale. \$\endgroup\$ – Sidar Apr 18 '13 at 22:28
  • 1
    \$\begingroup\$ @Abe You're right, width and height have effect on the bounding box. You can also try to calculate the ratio and apply it on scaleX. \$\endgroup\$ – Sidar Apr 19 '13 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy