Hi, I am currently making a game in which you are in an overhead view. You can rotate, and I need to be able to shoot in w/e direction you are facing. I have a code, and when I shoot, it does shoot in the direction you are facing, but If you change direction, the bullet also changes direction. I know why, and I need desperate help. Here is the code:
In the frame:
[AS]onLoad = function () {
i = 1;
bullet._visible = false;
};
onEnterFrame = function () {
x = Math.cos(sqr._rotation*Math.PI/180)5;
y = Math.sin(sqr._rotationMath.PI/180)*5;
if (Key.isDown(Key.UP)) {
sqr._rotation += 5;
}
if (Key.isDown(Key.DOWN)) {
sqr._rotation -= 5;
}
if (Key.isDown(Key.SPACE)) {
bullet.duplicateMovieClip(“obj”+i, i);
this[“obj”+i]._visible = true;
i++;
}
};[/AS]
Now I’m not the best with math at all… but it almost looks like you’re using the same variables for the moving bullet, as you are for the moving gun… which means that as the gun itself moves, so will the bullet…
I would make a seperate set of variables that will be set, and then stay static when the bullet is fired to determine the Slope the bullet will move towards it’s target.
something similar to that, I’m at work… I’m not at a computer with Flash (or anything) installed right now to give you a better explanation than that… hopefully someone else can elaborate a little, and lend a hand to you… sorry.
Yeah, I figured it was something like that… man… I’ve been away from the coding for too long now… that’s what you get for having to type up a design doc before advancing to the actual coding…
By “Dynamic” I mean that the variable changes. In your case, the variables that point the gun turret in the right direction are “Dynamic”, because they change everytime the player presses the button.
By “Static”, I mean that once the variable is set, they don’t change any more.
Which would result in the following: When the player hits [Space] the bullet gets told to move in the direction that the gun is pointing, but everytime the gun moves, the bullet is sent different information, and then it moves in the circular motion you mentioned. If you make it Static, then the bullet is told which way to go when [Space] is hit, but that information never changes, and the bullet will keep heading in the same direction, since they will be seperate variables.