Ok, been messing with this a couple of hours and I can't get my brain into turbo mode :/
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
did you tried to do it like that?:
1
2
3
4
5
6
7
8
| 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 |
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
i managed to do this somehow, its not pretty, but it works)
plug and play
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| 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) |
Hmm, okay it looks better but not perfect , sorry ;)
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
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....
1
2
3
4
5
6
7
8
9
10
11
| 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 |
views:1486 update:2011/12/29 9:44:01