PDA

View Full Version : [AS3] help shooting in the direction of the mouse


Jorhfm
09-07-2009, 10:05 PM
Hi
I have a pretty simple question, but that's taking alot of time to solve: I have a movieclip that moves using the arrows keys, and rotate always facing the mouse. I wanna make it shoot in the direction of the mouse, but I have no idea of how to do that.
Here's my code on the first frame of my .fla:


import teste.Player;

var pla:Player = new Player();

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);

function keyHandler(event:KeyboardEvent):void
{
if(pla._right == true){
jog.x = jog.x - pla.speed;
pla._right = false;
}

if(pla._left == true){
jog.x = jog.x + pla.speed;
pla._left = false;
}

if(pla._up == true){
jog.y = jog.y - pla.speed;
pla._up = false;
}

if(pla._down == true){
jog.y = jog.y + pla.speed;
pla._down = false;
}
pla.Move(event);
}

jog.addEventListener(Event.ENTER_FRAME, mouseHandler);

function mouseHandler(event:Event):void {
jog.rotation = pla._angle - 90;

pla.trata_Mouse(event);
}

And here's my code for the Player.as class:

package teste{

import flash.display.*;
import flash.ui.Keyboard;
import flash.ui.Mouse;
import flash.events.*;

public class Player extends MovieClip {

public var _right:Boolean = false;
public var _left:Boolean = false;
public var _up:Boolean = false;
public var _down:Boolean = false;
public var speed:int = 3;

public var _angle:Number = 0;
public var radiansToDegrees:Number = 180/Math.PI;

public var tiro_x:int = 0;
public var tiro_y:int = 0;
public var bullet_speed:int = 5;
public var bullet_dir:Number = 0;


public function Move(event:KeyboardEvent) {
if (event.keyCode == 37 && _right == false) {
_right = true;
}
if (event.keyCode == 39 && _left == false) {
_left = true;
}
if (event.keyCode == 38 && _up == false) {
_up = true;
}
if (event.keyCode == 40 && _down == false) {
_down = true;
}

}

public function trata_Mouse(event:Event) {
_angle = Math.atan2(mouseY-event.target.y, mouseX-event.target.x);

_angle *= radiansToDegrees;

}
}
}


Thanks for the attention

kaptain kosher
09-08-2009, 01:05 AM
For the 15th time lol


var dX:int = Mouse.x - player.x;
var dY:int = Mouse.y - player.y;
var newRotation:int = Math.atan2(dY, dX) * 180 / Math.PI;
bullet.rotation = newRotation;

Imjake9
09-08-2009, 01:47 AM
I'm not the original poster of this thread, but thanks a lot for that code, kaptain kosher! I was working on a project a while ago when I needed that and couldn't find it. Still, could you give an example? For instance, use the code in context. My only problem with that code, is that when my mouse clicks farther away from the player, the bullet moves much faster. I understand why, but since I have never been very good at angles in Flash, I can't figure out how to fix that. Any help would be appreciated!

Jorhfm
09-08-2009, 04:01 AM
For the 15th time lol


var dX:int = Mouse.x - player.x;
var dY:int = Mouse.y - player.y;
var newRotation:int = Math.atan2(dY, dX) * 180 / Math.PI;
bullet.rotation = newRotation;


I must be really dumb, but I can't use this code yet. Of course I'm using it wrong, but could you give an example of how to use this code?

Thanks

sam.uk.net
09-08-2009, 04:35 PM
Mouse.x

?

No such thing. Use mouseX and mouseY to access the mouses coordinates on the flash stage.

sam.uk.net