View Full Version : [fmx2004]Trigonomic Shooting
InsaneMonk
February 7th, 2004, 12:42 PM
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:
onLoad = function () {
i = 1;
bullet._visible = false;
};
onEnterFrame = function () {
x = Math.cos(sqr._rotation*Math.PI/180)*5;
y = Math.sin(sqr._rotation*Math.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++;
}
};
In the bullet:
onClipEvent (load) {
this._x = sqr._x;
this._y = sqr._y;
}
onClipEvent (enterFrame) {
this._x += _root.x;
this._y += _root.y;
}
here is the .fla:
SeiferTim
February 7th, 2004, 01:08 PM
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.
InsaneMonk
February 7th, 2004, 01:10 PM
And how would I make them stay static?
SeiferTim
February 7th, 2004, 02:20 PM
if (Key.isDown(Key.SPACE)) {
bullet.duplicateMovieClip("obj"+i, i);
this["obj"+i]._visible = true;
"variablea"+i = value;
"variableb"+i = value;
i++;
}
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.
InsaneMonk
February 7th, 2004, 03:59 PM
Ah! I see, but how can you call a variable with "i" attached to it? Can you really attach variables to variables?
SeiferTim
February 7th, 2004, 04:06 PM
I don't think the code would look exactly like my example, but i think it work similar to the way it would when you make duplicate movie clips...
Marz
February 7th, 2004, 06:41 PM
_root["variablea"+1i] =
is the correct way to use dynamic variables in Flash buddy ;)
SeiferTim
February 7th, 2004, 10:58 PM
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... ;)
InsaneMonk
February 8th, 2004, 12:32 PM
soooo... how do u create a "dynamic variable"?
SeiferTim
February 8th, 2004, 12:45 PM
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.
Make sense?
InsaneMonk
February 10th, 2004, 05:25 PM
yes it does. ive actually tried that and the bullet doesnt move at all... i think its a duplication problem. u helped alot though
lastchildz
April 27th, 2004, 10:21 PM
Hi Insane Monk !
I read this Cos' I am Having the same trouble ...
Couldn't figured it out until now , I tried different possibilities
but even with the help of Marz and SeiferTim it still doesn't make it !
Hope You can Help !!!
Thxxxx !
themos
April 28th, 2004, 03:58 AM
When you duplicate a movieclip does it duplicate it to the root of the movie or inside the movie your duplicating it from? If the latter is true then when you rotate the gun the whole gun moves dragging the bullet along behind it!
lastchildz
April 28th, 2004, 08:57 PM
aii , I made it worked now :
I was doing the condition test at the wrong place
So If anybody is interested here is the following code :
in the player:
onClipEvent (load) {
speed = 5;
bulletCounter = 1;
_root.bullet._visible=false;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x-= speed;
if(_xscale != -100) { // test the direction of the player
_xscale = -100;
}
}
if (Key.isDown(Key.RIGHT)) {
_x+= speed;
if(_xscale != 100) { // test the direction of the player
_xscale = 100;
}
}
if (Key.isDown(Key.DOWN)) {
_y+= speed;
}
if (Key.isDown(Key.UP)) {
_y-= speed;
}
if(Key.isDown(Key.SPACE)) {
bulletCounter++;
_root.bullet.duplicateMovieClip("bullet"+bulletCounter,bulletCounter);
}
}
on the bullet :
onClipEvent (load) {
if(_name != "bullet")
{
if (_root.player._xscale == 100 ){
bulletSpeed = 15 ;
this._xscale = 100 ;
_x=_root.player._x;
_y=_root.player._y;
}else if (_root.player._xscale == -100 ){
bulletSpeed = -15 ;
this._xscale = -100 ;
_x=_root.player._x;
_y=_root.player._y; }
}
function removebullet() { if( _x<0) this.removeMovieClip(); }
function removebullet2() { if( _x>500) this.removeMovieClip(); }
}
onClipEvent (enterFrame) {
if(_name != "bullet")
{
_x+= bulletSpeed;
removebullet();
removebullet2();
}
}
( I made a simple fla ... with just the moves and the shooting bit )
Thanx again !
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.