PDA

View Full Version : [AS3] how to make a bullet fire as player rotates


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

spooner
03-04-2009, 09:41 PM
General rule of thumb is the more code you post, the less likely people are to read it..

try to simplify your problem either in words or in succinct code, you're more likely to get an answer..

in general terms, usually you create an instance of a bullet class, position it at the origin of the bullet (where the player is) and give it the velocity you want, which would be some scalar representation of delta x and delta y between the player and your mouse cursor..

Farban
03-04-2009, 10:10 PM
I will try to make things more simple. Although im sure I cant paste any less code because of the relevance of the code currently up there and i thought if all of the code for the as was there it would help in solving the problem.

The problem with the swf file:

The player rotates with the mouse movement but as the space button is pressed the bullet always fires north no matter where the player is facing.

I just need to somehow fix the rotation of the bullet to the rotation of the player so that when the player fires a bullet it will travel in a right direction.

I am very new at actionscript 3 and this project has a deadline in three weeks so i dont want to compromise.

hope i made it a little clearer

Ciubhran
03-04-2009, 11:20 PM
in the bullet class, send the player's rotation as an argument to the constructor, and then just copy it to the bullet's rotation.

this.rotation = player_rotation;


this means if the player is facing south-west when the shot is made, the bullet will face south-west and continue in that direction until its rotation has been changed. you just have to copy the player's rotation when the bullet is fired to the bullets own rotation variable (which already exists in MovieClips, nothing you need to create).

Here is an example from my own game, which helps you with the propagation of the bullet. This is the code for what happens when I press 'W', which is Move Forward (speed is anything you want it to be):


Hero.y += speed * Math.sin((Hero.rotation - 90) * (Math.PI / 180));
Hero.x += speed * Math.cos((Hero.rotation - 90) * (Math.PI / 180));