graylensman
February 20th, 2007, 09:56 AM
I'm working on a zombie shooter, with a top-down view. Think asteroids, except with zombies instead of rocks. So, I'm coding weapons fire. I can generate bullets all day and all of the night, and can get them going in the direction the hero's gun is pointing - except that once you change the rotation of the hero, the bullet vectors change too. Any ideas on how to keep the bullet on an unchanging vector? I've tried all sorts of stuff, but nothing works.
Here's the code so far:
ActionScript Code:
//2.20.07
//motion of the hero. This works fine.
function moveHero(speed, swivel) {
//left and right arrow keys rotate our plucky hero; up key moves him forward relative to his rotation
if (Key.isDown(Key.LEFT)) {
hero._rotation -= swivel;
} else if (Key.isDown(Key.RIGHT)) {
hero._rotation += swivel;
} else if (Key.isDown(Key.UP)) {
heroAngle = 90-hero._rotation;
xchange = Math.cos(Math.PI/180*heroAngle);
ychange = -Math.sin(Math.PI/180*heroAngle);
hero._x += xchange*speed;
hero._y += ychange*speed;
}
//call the function that makes him fire his weapon
if (Key.isDown(Key.SPACE)) {
shootIt();
}
}
///
/*the function to shoot the bullets. Attach movie clips of bullets, then impart vector and velocity
using the sine and consine functions, too. But currently the vector changes when the rotation of
the hero changes. */
//
//variables for bullet generation, and the speed of the bullets.
var b = 1;
var bSpeed=3;
function shootIt() {
//incrementing our bullet variable by one
b++;
//creating a variable to reference generated bullet instances
var bullet = "bullet"+b;
//putting bullets into action!
_root.attachMovie("bullet", bullet, getNextHighestDepth());
//setting bullet instance x & y to hero x & y
_root[bullet]._x = hero._x;
_root[bullet]._y = hero._y;
//onEnterFrame to put the bullets in motion -
//here's where the weirdness starts. When the hero rotates, the vector of the bullets changes to match.
_root[bullet].onEnterFrame = function() {
bulletAngle = 90-hero._rotation;
bXchange =Math.cos(Math.PI/180*bulletAngle)*5;
bYchange = -Math.sin(Math.PI/180*bulletAngle)*5;
this._x+=bXchange*bSpeed;
this._y+=bYchange*bSpeed;
//remove movie clip instances once they leave the stage
if (this._x>Stage.width || this._x<0 || this._y>Stage.height || this._y<0) {
this.removeMovieClip();
}
};
}
//actually get it all going!
this.onEnterFrame = function() {
moveHero(5, 10);
};
thanks in advance!
Here's the code so far:
ActionScript Code:
//2.20.07
//motion of the hero. This works fine.
function moveHero(speed, swivel) {
//left and right arrow keys rotate our plucky hero; up key moves him forward relative to his rotation
if (Key.isDown(Key.LEFT)) {
hero._rotation -= swivel;
} else if (Key.isDown(Key.RIGHT)) {
hero._rotation += swivel;
} else if (Key.isDown(Key.UP)) {
heroAngle = 90-hero._rotation;
xchange = Math.cos(Math.PI/180*heroAngle);
ychange = -Math.sin(Math.PI/180*heroAngle);
hero._x += xchange*speed;
hero._y += ychange*speed;
}
//call the function that makes him fire his weapon
if (Key.isDown(Key.SPACE)) {
shootIt();
}
}
///
/*the function to shoot the bullets. Attach movie clips of bullets, then impart vector and velocity
using the sine and consine functions, too. But currently the vector changes when the rotation of
the hero changes. */
//
//variables for bullet generation, and the speed of the bullets.
var b = 1;
var bSpeed=3;
function shootIt() {
//incrementing our bullet variable by one
b++;
//creating a variable to reference generated bullet instances
var bullet = "bullet"+b;
//putting bullets into action!
_root.attachMovie("bullet", bullet, getNextHighestDepth());
//setting bullet instance x & y to hero x & y
_root[bullet]._x = hero._x;
_root[bullet]._y = hero._y;
//onEnterFrame to put the bullets in motion -
//here's where the weirdness starts. When the hero rotates, the vector of the bullets changes to match.
_root[bullet].onEnterFrame = function() {
bulletAngle = 90-hero._rotation;
bXchange =Math.cos(Math.PI/180*bulletAngle)*5;
bYchange = -Math.sin(Math.PI/180*bulletAngle)*5;
this._x+=bXchange*bSpeed;
this._y+=bYchange*bSpeed;
//remove movie clip instances once they leave the stage
if (this._x>Stage.width || this._x<0 || this._y>Stage.height || this._y<0) {
this.removeMovieClip();
}
};
}
//actually get it all going!
this.onEnterFrame = function() {
moveHero(5, 10);
};
thanks in advance!