Trouble using a function

Hello again. Well, I decided to put my jumping code into a function instead of a onClipEvent handler, but something is wrong. When I press the space bar(jumping key), he keeps on moving up until I release the key. Here is the code:


_root.onEnterFrame = function() {
 jumping();
 vel_y = 0;
 jump = false;
};
function jumping() {
 if (Key.isDown(Key.LEFT)) {
  hero._x -= 5;
 }
 if (Key.isDown(Key.RIGHT)) {
  hero._x += 5;
 }
 if (Key.isDown(Key.SPACE) && !jump) {
  vel_y = 10;
  jump = true;
 }
 if (jump == true) {
  vel_y -= 1;
  if (vel_y<=-10) {
   vel_y = -10;
  }
  hero._y -= vel_y;
 }
 if (_root.ground.hitTest(hero._x, hero._y+15, true)) {
  vel_y = 0;
  jump = false;
 }
 if (!_root.ground.hitTest(hero._x, hero._y+15, true) && !jump) {
  fall += 1;
  if (fall>10) {
   fall = 10;
  }
  hero._y += fall;
 }
}

If anyone could please help me, I would be very grateful. Thanks in advance!

peace

That’s because you declared the original jump variable outside of the jumping function and it’s not globally set… That’s all I can see right now… then again I’m not doing the hottest in the feeling department so… Blargh

Also… All your variables in the onEnterFrame function… I would suggest placing those in an onLoad function instead…

All right, I shall try that. Thanks marz, and get well soon :).

peace

Take what I said with an open mind… But that would solve the problem… However… This will…


if (Key.isDown(Key.SPACE) && !jump) { 
  vel_y = 10; 
  jump = true; 
}

Take out the vel_y = 10; line and put that somewhere else… Or… try this instead…


if(Key.isDown(Key.SPACE))
{
   if(!jump)
   {
      vel_y = 10;
   }
   jump = true;
}

As surprising as it may seem… When you hold down the spacebar… I think it just loops everything inside, without doing a check out the if statement over and over again… This should stop that from happening I believe… If you can’t figure out how to gte vel_y out of there.

Thanks again Marz :D!