Problems w/ my function... [MX]

I made a function for a ball that allows a ball to be shot at an angle (for a game) and it works fine. When I try to add gravity, no effect takes place, that is to say that there are no gravitation effects… here’s my function…

[AS] sht = function () {
dy += 0.2;
dx = Math.cos(ang)(cannon._xscale)/10;
dy = Math.sin(ang)
(cannon._xscale)/10;
ball._rotation = ang*(180/Math.PI);
ball._x += dx;
ball._y += dy;
};[/AS]

can anyone find a mistake with my AS? it’s probably something stupid…

-mike

“it’s probably something stupid…”

yes, yes it is :wink:

you are adding the .2 before you actually set dy. That means the += .2 is useless. put dy += .2 AFTER you set dy

oh yea… I missed that, but, that didn’t seem to fix it. Still, there is no arc/gravitational effect even after the change… here’s the whole code maybe it’s something else that’s just as dumb…:stuck_out_tongue:

[AS]ang = 0;
spd = 0.015;
pwr = 1;
shoot = 0;
sht = function () {
dx = Math.cos(ang)(cannon._xscale)/10;
dy = Math.sin(ang)
(cannon._xscale)/10;
ball._rotation = ang*(180/Math.PI);
ball._x += dx;
ball._y += dy;
dy += 0.2;
};
onEnterFrame = function () {
// depths
cannon.swapDepths(2);
tank.swapDepths(3);
// angle of cannon
if (Key.isDown(Key.UP)) {
ang -= spd;
}
if (Key.isDown(Key.DOWN)) {
ang += spd;
}
if (ang<-3.15) {
ang = -3.15;
}
if (ang>0) {
ang = 0;
}
cannon._rotation = ang*(180/Math.PI);
// x scale of cannon
if (Key.isDown(Key.RIGHT)) {
cannon._xscale += pwr;
}
if (Key.isDown(Key.LEFT)) {
cannon._xscale -= pwr;
}
if (cannon._xscale<50) {
cannon._xscale = 50;
}
if (cannon._xscale>150) {
cannon._xscale = 150;
}
// data stuff in text boxes
power = cannon._xscale-50;
angle = Math.round((ang/3.15)*-180);
// ball movement
if (Key.isDown(Key.SPACE)) {
shoot += 1;
}
if (shoot>0) {
spd = 0;
pwr = 0;
ball.onEnterFrame = sht;
}
};[/AS]

hope you guys can find something…

-mike

someone mentioned on the forums that you cant incremate a variable by a value smaller than .5… i’ll check for the link :slight_smile:

here… see sinfiniti’s post :slight_smile:

http://www.kirupaforum.com/forums/showthread.php?s=&threadid=16877&highlight=0.5

nope, that’s not it… i tried 0.5 , 1 and 2… none of them work.:stuck_out_tongue:

maybe it’s got something to do with the fact that the function is set in an if statemen?
[AS]
if (Key.isDown(Key.SPACE)) {
shoot += 1;
}
if (shoot>0) {
ball.onEnterFrame = sht;
}
[/AS]
sht is the function that causes the ball to move in the proper direction :

[AS]sht = function () {
dx = Math.cos(ang)(cannon._xscale)/10;
dy = Math.sin(ang)
(cannon._xscale)/10;
ball._rotation = ang*(180/Math.PI);
ball._x += dx;
ball._y += dy;
dy += 1;
};[/AS]

is that the problem? and if it is, how can i overcome it?

  • Michael

[AS]
if (Key.isDown(Key.SPACE)) {
shoot += 1;
}
if (shoot>0) {
sht;
}
[/AS]

Errm… You don’t even need the ball.onEnterFrame statement… Just call the function my main man :wink:

playamarz

Also… While I’m at it… You might wanna move the ball to the end of your barrel as soon as someone shoots like that then… :slight_smile:

playamarz

I tried calling the function like you told me playamarz, but it just caused things to get worse. I changed my function so that instead of “ball." I used "this.” and i left the function call as it was. when i called the function the way you told me, the ball was never shot, not to mention any gravitational effect… I’m really getting confused now:(. is there anyone who can help???