Jumping?

Hello,

So if you haven’t seen, I’m working on a platform game. “Bricks/platforms” scroll across the screen, and the player jumps from brick to brick.

I have the scrolling background, and everything pretty much done. But for jumping, I have this:

physics.setGravity(0, 9.8)  
  
local image = display.newImage("monkeyStand.png")  
image.y = display.contentHeight/2  
image.x = display.contentWidth/2  
physics.addBody(image, {density=0.01, bounce = 0})  
  
local function onRelease(e)   
image:applyLinearImpulse(0, -0.5, image.x, image.y)  
end  
  
image:addEventListener("touch", onRelease)  

(Let me remind you I’m the rookie of beginners here, so bear with me)

Im sure you can see what it does. The player jumps from plat to plat, but heres my issue. How do I make it so they can only jump from a platform? Not sure how to implement this…

Also, I thought it would be nice to have the picture change when the player is jumping (to appear as he is jumping) and one when he lands. How would I attempt this? I tried setting a variable to nil, but that didn’t work.

Any help would be greatly appreciated :slight_smile:
Ryley [import]uid: 28237 topic_id: 31868 reply_id: 331868[/import]

By jump from a platform, I am assuming you mean jump only when touching a platform. Create an “onGround” variable that checks the character’s y position.

[lua]local function check()
if player.y < 200 then
onGround = true
else
onGround = false
end
end
Runtime:addEventListener(“enterFrame”, check)[/lua]
then add an if statement around your jumping code, like so:
[lua]if onGround then
–jump code
else
end[/lua]

For the picture change, look into sprites.
http://developer.coronalabs.com/reference/sprite-sheets [import]uid: 59735 topic_id: 31868 reply_id: 127158[/import]

Could also use a collision to set flag onGround = true (or false) when the player lands on/jumps off the platform. Few threads on this. You’d likely find them Googling for “coronasdk” “onGround”.

Peach :slight_smile: [import]uid: 52491 topic_id: 31868 reply_id: 127165[/import]

Hmm.

BoltVisual, that might not work, because the platforms are at different y values.

Peach, I couldn’t find anything of use in a search, will keep going though.

Here is what I did.

local function jump()  
 if(onPlat == true)then  
 image:applyLinearImpulse(0, -0.5, image.x, image.y)  
  
 end  
end  
  
local function collided()  
onPlat = true  
end  
  
image:addEventListener("touch", jump)  
image2:addEventListener("collision", collided)  

And what it is doing, is just allowing me to tap the player image and it “jumps” regardless of the platform or not. Would it be easier if I were to implement a button into this? Like press a button, and it would check if the collision is true between player and platform, if so, then apply a linear impulse?

Thank you once again! [import]uid: 28237 topic_id: 31868 reply_id: 127166[/import]

That’s the exact same as “onGround” or the like - “onPlat”. Yes, when player collides with platform set onPlat or onGround or whatnot to true, and in jump function only fire if it is true, set it to false.

Peach :slight_smile: [import]uid: 52491 topic_id: 31868 reply_id: 127227[/import]

I usually weld a sensor to my player something like this:

[lua]local physics = require “physics”
physics.start()
physics.setGravity( 0, 40 )
physics.setDrawMode( “hybrid” )

local ground = display.newRect ( 0,300,1000,30 )
ground.id = “ground”
physics.addBody ( ground, “static” )

local player = display.newCircle ( 210,250,30 )
physics.addBody ( player, { bounce=0, radius = 30 } )
player.isFixedRotation = true

local onGroundSensor = display.newRect ( 190,265, 40, 30 )
physics.addBody ( onGroundSensor, { isSensor = true } )
onGroundSensor.isVisible = false

local sensorJoint = physics.newJoint ( “weld”, player, onGroundSensor, onGroundSensor.x, onGroundSensor.y )

– Collision handler
local onGroundCollision = function ( self, event )
if event.other.id == “ground” then
if ( event.phase == “began” ) then onGround = true
elseif ( event.phase == “ended” ) then onGround = false end
print ("on ground: " … tostring (onGround) )
end
return true
end

onGroundSensor.collision = onGroundCollision
onGroundSensor:addEventListener ( “collision”, onGroundSensor )

– jump
local function onTouch ( e )
if e.phase == “began” then
if onGround then
player:applyLinearImpulse(0, -1, player.x, player.y)
end
end
return true
end

Runtime:addEventListener ( “touch”, onTouch )
[import]uid: 13632 topic_id: 31868 reply_id: 127255[/import]

Thank you all very much for your responses. Peach, I used the onPlat and it worked. Thank you for your idea with onGround :slight_smile:

Ojnab, I see what you did there, and although too complicated for me to fully understand I believe I did something similar.

What I have now, is a working menu, and level system, as well as the gameplay.

One question, I have my apply linear impulse to my sprite, but I only want it to be able to move in the y direction. How could I manage this? Set x to nil?! I don’t think that would work… Or even have boundaries, so say the x can only change on the left to half of the screen.

Thanks guys, I’m trying to have a hobby through high school… A stressful one at that, but y’all make it that much easier! [import]uid: 28237 topic_id: 31868 reply_id: 127285[/import]

By jump from a platform, I am assuming you mean jump only when touching a platform. Create an “onGround” variable that checks the character’s y position.

[lua]local function check()
if player.y < 200 then
onGround = true
else
onGround = false
end
end
Runtime:addEventListener(“enterFrame”, check)[/lua]
then add an if statement around your jumping code, like so:
[lua]if onGround then
–jump code
else
end[/lua]

For the picture change, look into sprites.
http://developer.coronalabs.com/reference/sprite-sheets [import]uid: 59735 topic_id: 31868 reply_id: 127158[/import]

Could also use a collision to set flag onGround = true (or false) when the player lands on/jumps off the platform. Few threads on this. You’d likely find them Googling for “coronasdk” “onGround”.

Peach :slight_smile: [import]uid: 52491 topic_id: 31868 reply_id: 127165[/import]

Hmm.

BoltVisual, that might not work, because the platforms are at different y values.

Peach, I couldn’t find anything of use in a search, will keep going though.

Here is what I did.

local function jump()  
 if(onPlat == true)then  
 image:applyLinearImpulse(0, -0.5, image.x, image.y)  
  
 end  
end  
  
local function collided()  
onPlat = true  
end  
  
image:addEventListener("touch", jump)  
image2:addEventListener("collision", collided)  

And what it is doing, is just allowing me to tap the player image and it “jumps” regardless of the platform or not. Would it be easier if I were to implement a button into this? Like press a button, and it would check if the collision is true between player and platform, if so, then apply a linear impulse?

Thank you once again! [import]uid: 28237 topic_id: 31868 reply_id: 127166[/import]

That’s the exact same as “onGround” or the like - “onPlat”. Yes, when player collides with platform set onPlat or onGround or whatnot to true, and in jump function only fire if it is true, set it to false.

Peach :slight_smile: [import]uid: 52491 topic_id: 31868 reply_id: 127227[/import]

I usually weld a sensor to my player something like this:

[lua]local physics = require “physics”
physics.start()
physics.setGravity( 0, 40 )
physics.setDrawMode( “hybrid” )

local ground = display.newRect ( 0,300,1000,30 )
ground.id = “ground”
physics.addBody ( ground, “static” )

local player = display.newCircle ( 210,250,30 )
physics.addBody ( player, { bounce=0, radius = 30 } )
player.isFixedRotation = true

local onGroundSensor = display.newRect ( 190,265, 40, 30 )
physics.addBody ( onGroundSensor, { isSensor = true } )
onGroundSensor.isVisible = false

local sensorJoint = physics.newJoint ( “weld”, player, onGroundSensor, onGroundSensor.x, onGroundSensor.y )

– Collision handler
local onGroundCollision = function ( self, event )
if event.other.id == “ground” then
if ( event.phase == “began” ) then onGround = true
elseif ( event.phase == “ended” ) then onGround = false end
print ("on ground: " … tostring (onGround) )
end
return true
end

onGroundSensor.collision = onGroundCollision
onGroundSensor:addEventListener ( “collision”, onGroundSensor )

– jump
local function onTouch ( e )
if e.phase == “began” then
if onGround then
player:applyLinearImpulse(0, -1, player.x, player.y)
end
end
return true
end

Runtime:addEventListener ( “touch”, onTouch )
[import]uid: 13632 topic_id: 31868 reply_id: 127255[/import]

Thank you all very much for your responses. Peach, I used the onPlat and it worked. Thank you for your idea with onGround :slight_smile:

Ojnab, I see what you did there, and although too complicated for me to fully understand I believe I did something similar.

What I have now, is a working menu, and level system, as well as the gameplay.

One question, I have my apply linear impulse to my sprite, but I only want it to be able to move in the y direction. How could I manage this? Set x to nil?! I don’t think that would work… Or even have boundaries, so say the x can only change on the left to half of the screen.

Thanks guys, I’m trying to have a hobby through high school… A stressful one at that, but y’all make it that much easier! [import]uid: 28237 topic_id: 31868 reply_id: 127285[/import]

“One question, I have my apply linear impulse to my sprite, but I only want it to be able to move in the y direction. How could I manage this? Set x to nil?!”

Just put 0 for x value, not nil - is this what you mean? (Not sure if I am getting full picture here or not.) [import]uid: 52491 topic_id: 31868 reply_id: 127629[/import]

When I impulse it, it will sometimes move side to side. I want to lock it, so it only can move vertical, and not along the direction of the screen. I suppose there could be an issue when it hits the end of a platform, it would most likely create a massive glitch… Correct? [import]uid: 28237 topic_id: 31868 reply_id: 127636[/import]

“One question, I have my apply linear impulse to my sprite, but I only want it to be able to move in the y direction. How could I manage this? Set x to nil?!”

Just put 0 for x value, not nil - is this what you mean? (Not sure if I am getting full picture here or not.) [import]uid: 52491 topic_id: 31868 reply_id: 127629[/import]

When I impulse it, it will sometimes move side to side. I want to lock it, so it only can move vertical, and not along the direction of the screen. I suppose there could be an issue when it hits the end of a platform, it would most likely create a massive glitch… Correct? [import]uid: 28237 topic_id: 31868 reply_id: 127636[/import]

obj.isFixedRotation = true then apply force, let me know if that does the trick :slight_smile: [import]uid: 52491 topic_id: 31868 reply_id: 127848[/import]

obj.isFixedRotation = true then apply force, let me know if that does the trick :slight_smile: [import]uid: 52491 topic_id: 31868 reply_id: 127848[/import]

Great! Thanks peach :slight_smile: [import]uid: 28237 topic_id: 31868 reply_id: 128410[/import]