How should I get my brain to startup?

Ok, been messing with this a couple of hours and I can’t get my brain into turbo mode :confused:

I am having a physics world with static ground, and platforms.

I am having a ball as my character, and I can make him jump by applyLinearImpulse.

Problem is that I only want to make him jump when he is on the ground or on a platform. Not to apply more force when he is in midair.

My latest approach was to check for post collisions with ground and platform, and set a boolean variable to true. And when he jump, set the value to false. But that didn’t work either. Anyone, how has solved this before, or anyone have an idea on how to get this to work?

Best regards, Joakim [import]uid: 81188 topic_id: 19282 reply_id: 319282[/import]

did you tried to do it like that?:
[lua]local function onCollision(event)
if event.phase == “began” then
if event.other.name == “ground” or “platform” then
toJump = true
else toJump = false
end
end
end[/lua] [import]uid: 16142 topic_id: 19282 reply_id: 74372[/import]

Yes I have tried that, and I narrowed down the problem one more step…

As in a physics world, the ball bounces. Everytime the ball bounce to the ground, it sets the flag to true. That means that I can apply more force to the ball while it still is bouncing, and it is getting crazy.

Any more ideas? I just want to be able to make the ball jump, when it is still on the ground…

Thanks, Joakim
[import]uid: 81188 topic_id: 19282 reply_id: 74373[/import]

i managed to do this somehow, its not pretty, but it works)
plug and play
[lua]local physics = require(“physics”)
physics.start()

local ball = display.newCircle(0,0,30)
ball.x = 150
ball.y = 300
physics.addBody(ball, {radius = 30})
ball.name = “ball”
ball.jumping = false

local flor = display.newRect(0,0,display.contentWidth, 20)
flor.x = display.contentWidth/2; flor.y = display.contentHeight - 20
physics.addBody(flor, “static”)
flor.name = “ground”

local function ballCollision(event)
if event.phase == “began” then

if event.other.name == “ground” then

ball.jumping = true
print(ball.jumping)

end
end
end
ball:addEventListener(“collision”, ballCollision)

local function onTouch(event)
if event.phase == “ended” then
if ball.jumping == true then
print(jumping)

ball:applyForce(0,-10,ball.x,ball.y)

ball.jumping = not ball.jumping
end
end
end

ball:addEventListener(“touch”, onTouch)[/lua] [import]uid: 16142 topic_id: 19282 reply_id: 74380[/import]

Hmm, okay it looks better but not perfect , sorry :wink:

You can add force to the ball, exactly when it starts to bounce again. If you repeat that several of times, the ball is almost reaching the top.

So, I guess that there has to be another way. Maybe to see if the ball is “not” bouncing - and then apply the force again?

Joakim [import]uid: 81188 topic_id: 19282 reply_id: 74385[/import]

Ok, at last - with my friend google and all other inputs.

Within the function for the jump action I had to control the speed of the player…

if(onFloor) then  
 onFloor = false  
  
 local speedX, speedY = mySprite:getLinearVelocity()  
  
 if speedY \>= -16 then -- I allow multiple jumps, but only when descending or about to descend  
 local speedY = 52   
 mySprite:applyLinearImpulse(0, speedY, mySprite.x, mySprite.y)  
 end  
  
 end  

Thanks for all input!

Joakim [import]uid: 81188 topic_id: 19282 reply_id: 74421[/import]