Character jump in two phases

I am having trouble creating a character jump in two phases. 

I only have an onButtonDown and an onButtonUp phase to create my jump. I don’t want to use an eventListener. 

How do I create the jump?

Making a jump is pretty easy with an eventListener, but in this case, I only have two frames to work with. A timer probably won’t work either, because the amount of time the player holds the jump button might be shorter or longer than the time it takes to jump and fall back down, so I can’t cancel it on the onButtonUp phase. 

PS: I have to include both the onButtonDown and onButtonUp phase, neither of them can be empty for it to not throw errors.

You need to use an event listener, but add code to have that listener ‘ignore’ the release if the jump has passed a certain ‘time age’ or has completed.
 

That way, if the user presses and releases right away, you can get one response and if the user holds the touch too long you get another response.

I made an example of this in April of this year, toanswer to this post:

POST:  https://forums.coronalabs.com/topic/68439-trying-to-program-a-good-jump

CODE:  https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/04/betterjump.zip

VIDEO

https://www.youtube.com/watch?v=2tz7I1j8WvI

In the above video the yellow player uses  a basic, ‘jump’, while the cyan player (bottom) uses a two phase jump with the parameter control I talked about above.

If the first series of jumps I hold the button/press down till the jump ends.

In the second series, I release early and the bottom player’s special jump code kicks in so you can see the difference from the top player.

PS - I’m surprised you don’t remember this post as you participated. :slight_smile:

Bonus - Be sure to see the forum post I linked to above and then watch the GDC video about ‘Building A Better Jump’.

Thank you, the code is good. But what should I do when programming to stop the jump? By this, I mean to keep the player from jumping multiple times in the air.

You need to detect the player is ‘jumping’ or ‘in the air’.

The easiest way to do this is with a foot.

There used to be a great article on the site about this, but the link is dead. :frowning:

However, there is a forum thread discussing it:

https://forums.coronalabs.com/topic/63597-character-jump-tutorial-question/

Also, I have an example… digging now.  Will link in a moment.

I answered this question (and another) originally in August 2015 (I believe the aforementioned article predates this).
 
The answer I give has three answers in one:

This is actually a question I’ve answered many times.  
 
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/08/oneWayPlatform.zip
 
You participated in the thread the last time I answered this:
https://forums.coronalabs.com/topic/67248-how-to-create-a-one-way-transparent-platform
 
Dude, you need to keep a log or something of the posts you read.   :wacko:
 
Also, don’t forget about my AskEd Code,  I’ve answered a TON of questions:
https://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd (216 + in this area alone)

Side Note : I realize that AskEd could really benefit from a easy to search index and video clips or something, but I just don’t have the time/money to do it.

If only the community was more spend $ for help minded this would get done.

Sorry…  :mellow:

I think I will use the vx, vy linearVelocity check. Also, If I am correct, I read that I should not check if the velocity is 0, rather a number that is very close to zero, like 0.05.

Plus, I will start looking more at the AskEd Code. Thanks!

It’s all good.  I know it is a lot to remember especially when you are learning a lot of new things at once.

Tip: Calling getVelocity() a lot can be expensive, but if it is for just one object you should be OK.

Also, that is correct.  0 is not a good ‘stopped velocity’

This is better:

local vx, vy = obj:getLinearVelocity() local threshold = 1 -- make bigger to make 'stopped' a greater velocity. local stopped = ( (vx ^2 + vy ^2) \<= threshold )

I noticed the word ‘expensive’ gets thrown around a lot and that there are certain functions that can dent fps and memory more than other ones. Can you list a few or lead me to a tutorial that lists a few?

For instance, I assume the boolean expression:

local stopped = ( (vx ^2 + vy ^2) \<= threshold )

Is more expensive than an if statement…

  1. Don’t worry too much about optimizing, especially if you’re new and especially early.  Early optimization is one great way to kill your game/app.

  2. Expensive - This is a pretty loose term and unfortunately, what is and is not expensive (costly in terms of memory and/or cpu cycles and/or bandwidth and or blocking accesses to resources/hardware) varies from situation to situation.  Also, it is based on experience not a list of known offenders.  

  3. As for the specific question about that line of code… I’m not sure if one of these is more expensive or not…

    local stopped = ( (vx ^2 + vy ^2) <= threshold ) … versus … local stopped if( (vx ^2 + vy ^2) <= threshold ) then stopped = true end

I did this: 

 local stopped = ( (vx ^2 + vy ^2) \<= 1 ) if stopped == true then local jumpVecY = -7 anim.gravityScale = 1 anim:applyLinearImpulse(0, jumpVecY \* anim.mass, anim.x, anim.y ) anim:setSequence("jump") anim:play() end

PS, got the jump to work. :slight_smile:

You need to use an event listener, but add code to have that listener ‘ignore’ the release if the jump has passed a certain ‘time age’ or has completed.
 

That way, if the user presses and releases right away, you can get one response and if the user holds the touch too long you get another response.

I made an example of this in April of this year, toanswer to this post:

POST:  https://forums.coronalabs.com/topic/68439-trying-to-program-a-good-jump

CODE:  https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/04/betterjump.zip

VIDEO

https://www.youtube.com/watch?v=2tz7I1j8WvI

In the above video the yellow player uses  a basic, ‘jump’, while the cyan player (bottom) uses a two phase jump with the parameter control I talked about above.

If the first series of jumps I hold the button/press down till the jump ends.

In the second series, I release early and the bottom player’s special jump code kicks in so you can see the difference from the top player.

PS - I’m surprised you don’t remember this post as you participated. :slight_smile:

Bonus - Be sure to see the forum post I linked to above and then watch the GDC video about ‘Building A Better Jump’.

Thank you, the code is good. But what should I do when programming to stop the jump? By this, I mean to keep the player from jumping multiple times in the air.

You need to detect the player is ‘jumping’ or ‘in the air’.

The easiest way to do this is with a foot.

There used to be a great article on the site about this, but the link is dead. :frowning:

However, there is a forum thread discussing it:

https://forums.coronalabs.com/topic/63597-character-jump-tutorial-question/

Also, I have an example… digging now.  Will link in a moment.

I answered this question (and another) originally in August 2015 (I believe the aforementioned article predates this).
 
The answer I give has three answers in one:

This is actually a question I’ve answered many times.  
 
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/08/oneWayPlatform.zip
 
You participated in the thread the last time I answered this:
https://forums.coronalabs.com/topic/67248-how-to-create-a-one-way-transparent-platform
 
Dude, you need to keep a log or something of the posts you read.   :wacko:
 
Also, don’t forget about my AskEd Code,  I’ve answered a TON of questions:
https://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd (216 + in this area alone)

Side Note : I realize that AskEd could really benefit from a easy to search index and video clips or something, but I just don’t have the time/money to do it.

If only the community was more spend $ for help minded this would get done.

Sorry…  :mellow: