PDA

View Full Version : Where to assign functions



Pneumonic
December 4th, 2004, 04:49 PM
Ok. I'm still working on my tank game here and I've coded all the mathematics, but I'm unsure of where to assign the functions so that I can go ahead and have the bullet follow my parabola. My cannon has the instance name of cannon, and my cannonball has the instance name of cannonball. Here's my code:


//Define all the variables needed globally first
length = 75
cannon.vel = 10;
cannon._rotation = 0;
x0 = Math.cos(cannon._rotation) + length;
y0 = Math.sin(cannon._rotation) + length;
g = 9.8;

//This determines v0 & creates the parabola
parabola = function(){
//returns the distance traveled on the x axis in m/s
v0x = cannon.vel * Math.cos(cannon._rotation);
//returns the distance traveled on the y axis in m/s
v0y = cannon.vel * Math.sin(cannon._rotation);
}

//Define these variables to make solving Quad eq. easier
a = .5*g;
b = v0y;
c = y0;

//This does the Quadratic Equation to give you the time in the air
quad_eq = function(){
t1 = -b + Math.sqrt(b^2-4*a*c)/(2*a);
t2 = -b - Math.sqrt(b^2-4*a*c)/(2*a);
//Now determine answer is greater and assign it to t, which is time
if(t1>t2){
t = t1;
}else{
t = t2;
}
}

//this gives you the x position at the end of travel
xpos = x0 + v0x*t;

//this determines the y position at the end of travel
yposition = function(){
//dividing the initial velocity of y by gravity gives you new time
t2 = v0y/g;
//this gives you the maximum height
ypos = y0 + v0y * t2 - a * t2^2;
}

//Redefine variable to keep math terms correct
vx = v0x;
//Determines final velocity vector
vy = v0y - g*t;

vectors = function(){
velocity = Math.sqrt(vx^2+vy^2);
direction = Math.atan(vy/vx);
}

//Place all code for key events here
cannon.onEnterFrame = function(){
//this will happen each frame rotating the
//cannon left and right with the arrow keys

if(Key.isDown(Key.RIGHT)){
cannon._rotation +=1;
angle.text = cannon._rotation;//this updates the angle text field
}//end if

if(Key.isDown(Key.LEFT)){
cannon._rotation -= 1;
angle.text = cannon._rotation;//this updates the angle text field
}//end if

if(Key.isDown(Key.UP)){
cannon.vel += 1;
power.text = cannon.vel;//this updates the power text field
}//end if

if(Key.isDown(Key.DOWN)){
cannon.vel -= 1;
power.text = cannon.vel;//this updates the power text field
}//end if

//this attaches the onRelease to the button
//without attaching it to the button directly
fire.onRelease = function(){
this.attachMovie("cannonball","cannonball",0);
cannonball._x = x0;
cannonball._y = y0;
}
}

power.text = cannon.vel;
angle.text = cannon._rotation;


Also, I have x0 and y0 set the way I do so that I can attach the cannonball movie clip to the tip of my cannon, but it doesn't seem to work. I still have to add my hit.Test to, but I am gonna wait for that. Any help is appreciated. Thanks!

Elbudster
December 5th, 2004, 12:41 AM
please post the .fla

Dr Warm
December 5th, 2004, 06:16 AM
Yeah i'm not totally sure what you're trying to do, you want it so the bullet will follow the parabola? But it's not working?

do you just want cannonball.onEnterFrame = parabola(); or something?

SeiferTim
December 5th, 2004, 07:00 AM
I'm a little confuzzled too... you're asking where you need to type the functions you're going to access? Generally, somewhere above the code that calls it, I tend to put all mine at the top of the frame's AS Panel, right after I set all my global variables, and before doing anything else like:


global1=1;
global2=2;

function doSomething (somevar) {
doSomething = somevar;
};

onClipEvent (enterFrame) {
global2+=doSomething(global1);
}


Ta-Da!
I probably didn't understand what you were actually asking though... so this probably won't help at all.

Pneumonic
December 5th, 2004, 01:44 PM
actually that somewhat did help...kind of. And yes Dr. Warm, but I think I've got it down pat now. I'll post the code later when I'm not so pressed for time.