# Trouble using a function

**URL:** <https://forum.kirupa.com/t/trouble-using-a-function/64921>\
**Category:** Uncategorized\
**Created:** [September 21, 2004, 1:42pm UTC](https://forum.kirupa.com/t/trouble-using-a-function/64921 "2004-09-21T13:42:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoMan](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@JoMan](https://forum.kirupa.com/u/JoMan)\
**Post date:** [September 21, 2004, 1:42pm UTC](https://forum.kirupa.com/t/trouble-using-a-function/64921/1 "2004-09-21T13:42:08Z")

</div>

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:

```php

_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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 21, 2004, 2:42pm UTC](https://forum.kirupa.com/t/trouble-using-a-function/64921/2 "2004-09-21T14:42:49Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 21, 2004, 3:04pm UTC](https://forum.kirupa.com/t/trouble-using-a-function/64921/3 "2004-09-21T15:04:40Z")

</div>

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

peace

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 21, 2004, 3:47pm UTC](https://forum.kirupa.com/t/trouble-using-a-function/64921/4 "2004-09-21T15:47:11Z")

</div>

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

```php

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…

```php

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 21, 2004, 5:01pm UTC](https://forum.kirupa.com/t/trouble-using-a-function/64921/5 "2004-09-21T17:01:25Z")

</div>

Thanks again Marz :D!
