Hi.
I’m working on my charter jumps right now. And thanks to things in this forums or found on the web I have something that -mostly- works.
The goal is to make a Mario style jump that keeps going up as long as you keep the button pressed (within a certain height limit), and you can’t jump in the air.
Here is the ‘touch’ code:
[lua]–what happens on touch
function touched( event )
local vx, vy = (Player[1]):getLinearVelocity();
–touch on the left, jump
if( event.x < (_W/2) ) then
–make sure you’re in the ground
if ( event.phase == “began” and vy <= 0 ) then
–determine how high can we get
Player[1].jumpLimit = Player[1].y - _jumpPotency;
Player[1].jumpMode = true;
elseif ( event.phase == “ended” or event.phase == “canceled” ) then
Player[1].jumpMode = false;
else
print("THE VY = “…vy…” THE VX = "…vx);
end
end
–touch on the right, slide.
–not implemented yet…
end[/lua]
And this is the code that actually does the jumping (located in the updateHero function):
[lua]–apply jumping force
if Player[1].jumpMode then
if Player[1].y >= Player[1].jumpLimit then
–only apply force when below height limit
Player[1]:translate( 0, _jumpVelocity );
–Player[1]:applyForce( -0, _jumpVelocity*6, Player[1].x, Player[1].y );
–Player[1]:setLinearVelocity( 0, _jumpVelocity *10);
else
–when we are high enough, start falling
Player[1].jumpMode = false;
end
end [/lua]
(Note how I’m testing with translate, applyForce and setLinearVelocity to make the jump. So far translate just looks and feels better than the other two).
My problem is that I can’t find a sure-fire way to tell if I’m on the ground. I know the most used method is to check for collision with a floor object and set a flag like hero.onGround or something similar, this doesn’t work on my case, because my floor is in constant movement and there is a possibility that will collide from the sides and not the top (so you can be on contact with floor, but still falling).
What I’m doing right now is check for the y velocity ( local vx, vy = (Player[1]):getLinearVelocity(); ) under the logic that if the velocity is 0 it means that I’m not falling and on the ground. This however, only works some of the time, it’s not 100% accurate.
Suggestions on how to fix this? (comments on my current method are also welcome).
[import]uid: 136402 topic_id: 29248 reply_id: 329248[/import]
When you say it “fumbles” a bit, is this only jumping from slopes? How are you doing the jump, by applying impulse? Do you have a screenshot that shows your character?