PDA

View Full Version : spawning bullets



sam_vickery
March 29th, 2009, 03:15 PM
I have made a top down shooter game in space.
I have made all of the movement.
I have come across my first problem spawning the bullets in the right place

I have made it so that the bullets spawn from the centre of the space ship but I want to know is there a way to make them a certain distance away from the space ship.

Here is the code on the main frame



var speed = 0;
var shiprotation = 0;
var rotationspeed = 0.1;
var accel = 0.1;
var maxrotationspeed = 5;
var maxspeed = 10;
var friction = 0.9;
function moveship() {
if (Key.isDown(Key.LEFT)) {
shiprotation -= rotationspeed;
}
if (Key.isDown(Key.RIGHT)) {
shiprotation += rotationspeed;
}
if (shiprotation>maxrotationspeed) {
shiprotation = maxrotationspeed;
}
if (shiprotation<-maxrotationspeed) {
shiprotation = -maxrotationspeed;
}
if (Key.isDown(Key.UP)) {
speed += accel;
}
if (Key.isDown(Key.DOWN)) {
speed -= speed/30;
}
if (speed>maxspeed) {
speed = maxspeed;
}
ship._rotation += shiprotation;
xspeed *= friction;
yspeed *= friction;
shiprotation *= friction;
xspeed = speed*Math.sin((ship._rotation+90)*(Math.PI/180));
yspeed = speed*Math.cos((ship._rotation+90)*(Math.PI/180));
map._x -= xspeed;
map._y += yspeed;
}
var shipstate = "idle";
function changestate() {
if (Key.isDown(Key.CONTROL) && shipstate != "guns") {
ship.gotoAndStop(2);
}
if (Key.isDown(Key.CONTROL) && shipstate == "weapons") {
ship.gotoAndStop(3);
}
if (shipstate == "weapons") {
accel = 0.1;
rotationspeed = 0.5;
} else {
accel = 0.5;
rotationspeed = 0.1;
}
}
var shootready = true;
var count = 0;
var bullets = [];
var shoottimer = 0;
var shootdelay = 0.5*30;
function shooting() {
if (Key.isDown(Key.SPACE) && shootready == true) {
bullet = attachMovie("bullet", "bullet"+count, count);
bullet._x = ship._x;
bullet._y = ship._y;
count++;
shootready = false;
bullets.push(bullet);
}
shoottimer++;
if (shoottimer>shootdelay) {
shootready = true;
shoottimer = 0;
}
}
_root.onEnterFrame = function() {
moveship();
changestate();
shooting();
};


any help will be great.


thanks

neilmmm
March 29th, 2009, 04:26 PM
you could do something like this


bullet._x = ship._x+=whatevervalue;
bullet._y = ship._y+=whatevervalue;


or you could put an alphaed out dot where you want them to appear and have


bullet._x = ship.spot._x;
bullet._y = ship.spot._y;

sam_vickery
March 29th, 2009, 04:36 PM
I tried that but the value will change around the rotation.

Is there any way to find out what the value may be

therobot
March 29th, 2009, 11:35 PM
I tried that but the value will change around the rotation.

Is there any way to find out what the value may be

Trig. You already should have the angle the gun is pointed at and you should already know the length of the gun (the hypotenuse).


(edited from original post)
x = math.cos(angle) * hypotenuse;
y = math.tan(angle) * x;

bullet.x = gun.x + x;
bullet.y = gun.y + y;

sam_vickery
March 30th, 2009, 11:58 AM
Trig. You already should have the angle the gun is pointed at and you should already know the length of the gun (the hypotenuse).


(edited from original post)
x = math.cos(angle) * hypotenuse;
y = math.tan(angle) * x;

bullet.x = gun.x + x;
bullet.y = gun.y + y;

thanks for the help but I could quite get it.

Here is the .fla and .swf files to show you.