Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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;
share|improve this answer
    
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. –  user25191 Apr 18 '13 at 22:08
    
Well im only touching the Width though. scaleY shouldn't be affected –  Sidar Apr 18 '13 at 22:16
    
EDIT the height changes because the object is rotating... –  user25191 Apr 18 '13 at 22:27
    
Rotation shouldn't affect the scale. –  Sidar Apr 18 '13 at 22:28
1  
@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. –  Sidar Apr 19 '13 at 0:39
show 8 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.