Farban
03-04-2009, 07:55 PM
Hey there I get the feeling im so near to fixing this. The bullet fires as told too but the problem is that i cant make the bullet fire towards the mouse curser. The player rotates to the mouse cursor i just want the bullet to fire towards the cursor too. i have looked at a big range of tutorials but the code is so different from mine and i have been pulling my hair out on this one.
So i thought if someone looked at the code they may be able to help
here is the player.as file
//our package... simply put, the directory structure to this file
package game
{
//list of our imports these are classes we need in order to
//run our application.
import flash.display.MovieClip;
import flash.display.Stage;
import game.utils.KeyObject;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
//our Engine class it extends MovieClip
public class player extends MovieClip
{
private var stageRef:Stage;
private var key:KeyObject;
private var xd:Number;
private var yd:Number;
private var radAngle:Number;
private var speed:Number = 5;
private var fireTimer:Timer; //causes delay between fires
private var canFire:Boolean = true; //can you fire a laser
//our constructor function. This runs when an object of
//the class is created
public function player(stageRef:Stage) : void
{
this.stageRef = stageRef;
key = new KeyObject(stageRef);
fireTimer = new Timer(300, 1);
fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
if (key.isDown(Keyboard.LEFT))
x -= speed;
else if (key.isDown(Keyboard.RIGHT))
x += speed;
if (key.isDown(Keyboard.UP))
y -= speed;
else if (key.isDown(Keyboard.DOWN))
y += speed;
if (key.isDown(Keyboard.SPACE))
fireBullet();
if (x > stageRef.stageWidth)
{
x = stageRef.stageWidth;
}
else if (x < 0)
{
x = 0;
}
if (y > stageRef.stageHeight)
{
y = stageRef.stageHeight;
}
else if (y < 0)
{
y = 0;
}
xd = this.x-stage.mouseX; /*leave this(suggested)*/
yd = this.y-stage.mouseY; /*leave this(suggested)*/
// finds the x and y difference of the sprite and the mouse
radAngle = Math.atan2(yd, xd); /*leave this(suggested)*/
// finds the angle in radians (flash works in radians not degrees)
this.rotation = int(radAngle*360/(Math.PI*2)-90); /*leave this(suggested)*/
// uses PI because radians work with PI (not the tasty kind either)
}
private function fireBullet() : void
{
//if canFire is true, fire a bullet
//set canFire to false and start our timer
//else do nothing.
if (canFire)
{
stageRef.addChild(new bullet(stageRef, x , y - 10));
canFire = false;
fireTimer.start();
}
}
private function fireTimerHandler(e:TimerEvent) : void
{
//timer ran, we can fire again.
canFire = true;
}
}
}
here is the bullet.as file
package game
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class bullet extends MovieClip
{
private var stageRef:Stage;
private var bulletSpeed:Number = 16;
public function bullet (stageRef:Stage, x:Number, y:Number) : void
{
this.stageRef = stageRef;
this.x = x;
this.y = y;
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function loop(e:Event) : void
{
//move bullet up
this.y -= bulletSpeed;
this.x -= bulletSpeed;
if (y < 0)
removeSelf();
}
private function removeSelf() : void
{
removeEventListener(Event.ENTER_FRAME, loop);
if (stageRef.contains(this))
stageRef.removeChild(this);
}
}
}
any help would be greatly appriciated
thanks
So i thought if someone looked at the code they may be able to help
here is the player.as file
//our package... simply put, the directory structure to this file
package game
{
//list of our imports these are classes we need in order to
//run our application.
import flash.display.MovieClip;
import flash.display.Stage;
import game.utils.KeyObject;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
//our Engine class it extends MovieClip
public class player extends MovieClip
{
private var stageRef:Stage;
private var key:KeyObject;
private var xd:Number;
private var yd:Number;
private var radAngle:Number;
private var speed:Number = 5;
private var fireTimer:Timer; //causes delay between fires
private var canFire:Boolean = true; //can you fire a laser
//our constructor function. This runs when an object of
//the class is created
public function player(stageRef:Stage) : void
{
this.stageRef = stageRef;
key = new KeyObject(stageRef);
fireTimer = new Timer(300, 1);
fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
public function loop(e:Event) : void
{
if (key.isDown(Keyboard.LEFT))
x -= speed;
else if (key.isDown(Keyboard.RIGHT))
x += speed;
if (key.isDown(Keyboard.UP))
y -= speed;
else if (key.isDown(Keyboard.DOWN))
y += speed;
if (key.isDown(Keyboard.SPACE))
fireBullet();
if (x > stageRef.stageWidth)
{
x = stageRef.stageWidth;
}
else if (x < 0)
{
x = 0;
}
if (y > stageRef.stageHeight)
{
y = stageRef.stageHeight;
}
else if (y < 0)
{
y = 0;
}
xd = this.x-stage.mouseX; /*leave this(suggested)*/
yd = this.y-stage.mouseY; /*leave this(suggested)*/
// finds the x and y difference of the sprite and the mouse
radAngle = Math.atan2(yd, xd); /*leave this(suggested)*/
// finds the angle in radians (flash works in radians not degrees)
this.rotation = int(radAngle*360/(Math.PI*2)-90); /*leave this(suggested)*/
// uses PI because radians work with PI (not the tasty kind either)
}
private function fireBullet() : void
{
//if canFire is true, fire a bullet
//set canFire to false and start our timer
//else do nothing.
if (canFire)
{
stageRef.addChild(new bullet(stageRef, x , y - 10));
canFire = false;
fireTimer.start();
}
}
private function fireTimerHandler(e:TimerEvent) : void
{
//timer ran, we can fire again.
canFire = true;
}
}
}
here is the bullet.as file
package game
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
public class bullet extends MovieClip
{
private var stageRef:Stage;
private var bulletSpeed:Number = 16;
public function bullet (stageRef:Stage, x:Number, y:Number) : void
{
this.stageRef = stageRef;
this.x = x;
this.y = y;
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function loop(e:Event) : void
{
//move bullet up
this.y -= bulletSpeed;
this.x -= bulletSpeed;
if (y < 0)
removeSelf();
}
private function removeSelf() : void
{
removeEventListener(Event.ENTER_FRAME, loop);
if (stageRef.contains(this))
stageRef.removeChild(this);
}
}
}
any help would be greatly appriciated
thanks