Can't figure out jumping, player getting stuck

First post, and can I say thanks to the many people who have provided tutorials and other advice - it’s been a great help getting started!

I’ve come across an issue with what seems like such an easy thing for other people to do: player jumping.

Basically, I’m trying to create a Doodle Jump style continuous jump upon collision with a static object. This is working fine in principle, however when coming down the player gets stuck just above the ground - just kind of hangs there in the air. This can be overcome by moving left or right, or by touching the screen anywhere (there’s that d-pad stop function in there).

Any help you all can give on overcoming this would be great - no doubt it’s something very simple, but I only started two days ago! (no coding experience)

-- add player  
local player = display.newImage('player\_testsprite.png')  
 player.x = \_W/2  
 player.y = \_H/1.255  
 physics.addBody(player, { bounce = 0, density = 100, friction = 100})  
 player.isFixedRotation = true  
  
-- Player wraps around the screen  
local function wrap (event)  
if player.x \< 0 then  
 player.x = \_W  
end  
  
if player.x \> \_W then  
player.x = 0  
end  
end  
  
Runtime:addEventListener("enterFrame", wrap)  
  
-- add grass floor  
local ground = display.newImage('Grass\_Floor.png')  
 ground.y = \_H - ground.contentHeight/3  
 ground.name = "ground"  
 physics.addBody(ground, "static", { bounce = 0, friction = 10})  
  
-- add d-pad  
local up = display.newImage ("upx.png")  
 up.x = 70  
 up.y = 750  
  
local left = display.newImage ("leftx.png")  
 left.x = 25  
 left.y = 785  
  
local right = display.newImage ("rightx.png")  
 right.x = 115  
 right.y = 785  
  
local motionx = 0  
local motiony = 0  
local speed = 10  
  
local function moveplayer (event)  
 player.x = player.x + motionx  
 player.y = player.y + motiony  
end  
  
Runtime:addEventListener("enterFrame", moveunicorn)  
function onGround(event)  
 if event.phase == "began" then  
 canJump = true  
 elseif event.phase == "ended" then  
 canJump = false  
 end  
end  
  
player:addEventListener("collision", onGround)  
  
-- Jumping function  
function jump(event)  
 if canJump then  
 motiony = -10  
 end  
end  
  
Runtime:addEventListener("enterFrame", jump)  
  
function stop (event)  
 if event.phase == "ended" then  
 motionx = 0  
 motiony = 0  
 end  
end  
  
Runtime:addEventListener("touch", stop)  
  
-- Left/Right function  
function left:touch()  
 motionx = -speed  
 motiony = 0  
end  
left:addEventListener("touch", left)  
  
function right:touch()  
 motionx = speed  
 motiony = 0  
end  
right:addEventListener("touch", right)  

What’s going wrong here?

p.s. Physics is being called at the beginning as per normal [import]uid: 45010 topic_id: 7911 reply_id: 307911[/import]

Also, can’t seem to edit - but the inconsistency between ‘moveplayer’ and ‘moveunicorn’ in the code is on the forum only. I was trying to generalise the code for here, and missed that piece. But now can’t edit the post because of a bug…

And for what it’s worth, I can’t just change the motiony = -15 to an applyLinearImpulse because if I do nothing happens.

Any ideas? Or suggestions for how to do it better? [import]uid: 45010 topic_id: 7911 reply_id: 28101[/import]

it looks like, you haven’t use the jump-Button.

[lua]Runtime:addEventListener(“enterFrame”, jump)[/lua]

shouldn’t it be
[lua]up:addEventListener(“enterFrame”, jump)[/lua]
?
Is the players start position on the ground? [import]uid: 42078 topic_id: 7911 reply_id: 28156[/import]

The player falls to the ground and is supposed to continue bouncing (in a similar manner to Doodle Jump).

I started originally with using a jump button (up) to control this, but then moved to an automatic system. So the ‘up’ part of the code is an artifact of that and I have since removed it but the problem persists.

The jumping works, but when it comes down it stops just short of the new collision - in hybrid mode you can see the gap between the player and the ground. When I press left or right again, release left or right (if held down), or touch the screen anywhere (and the touch event.phase == “ended” triggers) it closes this gap and jumps again.

If I jump, then continue to hold down left or right it will slide along this gap until I release - then fall to the collision and jump again. I’ve got a feeling it has something to do with the way I’ve set up motion (based on some d-pad tutorials), but I can’t figure out what…and even playing around with different things doesn’t seem to help.

Are there better ways to be doing this? [import]uid: 45010 topic_id: 7911 reply_id: 28157[/import]

I’ve never played doodle jump (shame on me…).

I would play with the physics settings.

In the documentations you can find a postCollision event under “Collision forces”.
http://developer.anscamobile.com/content/game-edition-collision-detection
Perhaps that could help!? [import]uid: 42078 topic_id: 7911 reply_id: 28162[/import]

@RetroQuest

How did make the player jump Doodle Jump style?

I’m trying to make my player just like that but I can’t get it to work, I want my player to be able to jump only if he is on a platform and not mid-air. I’ve tried several ways with linearImpulse, transitionTo etc.

if I use linearImpulse in my collision then the player jumps like crazy til the simulator crashes.

Could you help me out how to get it to work, share your solution?

David [import]uid: 34126 topic_id: 7911 reply_id: 38007[/import]