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
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