Character Jump

Hi, please help me :frowning:

I try make character jump by CHARACTER JUMP - EXAMPLE ,but this example has a few “bugs”.

Some “bugs”:

  • If I touch more times character jump still up and up.
  • If I hold finger-mouse on display character jump only one. ( I need that character jump one and if character collision with grass-ground then next jump etc… )

I tried this modification ,but It didn’t help me :frowning:

[lua]-----------------------------------------------------------------------------------------

– main.lua


display.setStatusBar( display.HiddenStatusBar )
local physics = require “physics”
local onAir = false
physics.start()

local background = display.newImage( “background.jpg” )

local ground = display.newImage( “ground.png” )
ground:setReferencePoint( display.BottomLeftReferencePoint )
ground.x, ground.y = 0, 320
ground.name = “ground”

local groundShape = { -240,-20, 240,-20, 240,20, -240,20 }
physics.addBody( ground, “static”, { friction=1.0, density=1.0, bounce=0, shape=groundShape } )

local character = display.newImage( “character.png” )
character.x = 70
character.y = 234

physics.addBody( character, { friction=1.0, density=2.0, bounce=0, radius=35 } )
character.isFixedRotation = true

local function onScreenTouch( event )
if (event.phase == “began”) then
tmr = timer.performWithDelay( 1, function()

if(onAir == false) then
onAir = true
character:applyForce( 0, -2500, character.x, character.y )
end

end, 0)
end

if (event.phase == “ended”) then timer.pause(tmr) end
return true
end
Runtime:addEventListener( “touch”, onScreenTouch )
local function collis( event )
if (event.phase == “began” and event.other.name == “ground”) then
onAir = false
end
end
character.collision = collis
character :addEventListener( “collision”, machio )[/lua] [import]uid: 163161 topic_id: 29721 reply_id: 329721[/import]

Hi there,

Well looking at the code I couldn’t figure out why is wasn’t working.

After nabbing it and trying it myself. I managed to fix it for you.

-----------------------------------------------------------------------------------------  
--  
-- main.lua  
--  
-----------------------------------------------------------------------------------------  
   
display.setStatusBar( display.HiddenStatusBar )  
local physics = require "physics"  
local onAir = false  
physics.start()  
physics.setDrawMode("hybrid"); --This is so I could see the physics bodies, comment & uncomment.  
   
local background = display.newImage( "background.jpg" )  
   
local ground = display.newImage( "ground.png" )  
ground:setReferencePoint( display.BottomLeftReferencePoint )  
ground.x, ground.y = 0, 320  
ground.name = "ground"  
   
local groundShape = { -240,-20, 240,-20, 240,20, -240,20 }  
physics.addBody( ground, "static", { friction=1.0, density=1.0, bounce=0, shape=groundShape } )  
   
local character = display.newImage( "character.png" )  
character.x = 70  
character.y = 234  
   
physics.addBody( character, { friction=1.0, density=2.0, bounce=0, radius=35 } )  
character.isFixedRotation = true  
   
local function onScreenTouch( event )  
 print(onAir)  
 if (event.phase == "began") then  
 -- tmr = timer.performWithDelay( 1, function() -- Not sure what these are doing.  
   
 if(onAir == false) then  
 onAir = true  
 character:applyForce( 0, -2500, character.x, character.y )  
 end  
   
 --end, 0) -- end of timer (Still unsure)  
 end  
   
 -- if (event.phase == "ended") then timer.pause(tmr) end -- Timer seems unnecessary.   
 -- return true  
end  
Runtime:addEventListener( "touch", onScreenTouch )  
   
   
local function collis( event )  
 if (event.phase == "began" and event.other.name == "ground") then  
 print("Collision with Ground")  
 onAir = false  
 end  
 end  
 character.collision = collis  
 character :addEventListener( "collision", collis ) -- Changed 'machio' to 'collis' no Idea what machio is.  

I commented sections that were causing issue.
The timer didn’t seem to do much except crash the game when you held the screen if the ‘ended’ phase came up. So I took it out.

And at the end you were guiding the eventlistener for your character to something called ‘machio’.
Whatever that is!? I changed it to collis, because that is the collision function to set ‘onAir’ back to false.

I also added in a couple of debugging prints and a physics debug mode, feel free to comment this out and back in when you want to debug.
And you can remove the prints i added.

Good Luck.
Matt [import]uid: 91798 topic_id: 29721 reply_id: 119298[/import]

Thanks, for answer.

  • “machio” is my mistake, I use this object in previous project…

First problem is solved ,but what with second problem ?

“If I hold finger-mouse on display character jump only one ( UP and DOWN and then STOP jumping ). I need that character jump one and if character collision with grass-ground then next jump etc - automatically during hold finger or mouse on display.”

Because of this, there was a timer. If you don¨t know what I mean, try this flash game HERE. Start game and press-hold UP button-arrow :slight_smile: [import]uid: 163161 topic_id: 29721 reply_id: 119299[/import]

I think it is because you have the event.phase == began so when the player lands nothing else happens.

Apologize I don’t have time right now to throw some code up for you, but this should get you started. [import]uid: 105129 topic_id: 29721 reply_id: 119300[/import]

Why do you think there is var “onAir” and timer ?

// Why with this src character jump nonstop

[lua]local function collis( event )
if (event.phase == “began” and event.other.name == “ground”) then
print(“Collision with Ground”)
timer.performWithDelay(1, function()
character:applyForce( 0, -2500, character.x, character.y )
end)
onAir = false
end
end[/lua]

,but with this src character jump only one

[lua]local function collis( event )
if (event.phase == “began” and event.other.name == “ground”) then
print(“Collision with Ground”)
character:applyForce( 0, -2500, character.x, character.y )
onAir = false
end
end[/lua] [import]uid: 163161 topic_id: 29721 reply_id: 119333[/import]

Hi,

You need to position your actual jump code in an event that is sure to fire when both the user is touching the screen and the player contacts the ground. You can’t use the touch listener because you only get events when the user first touches, when they move their finger and when they lift it up. So if they just hold down their finger unmoving(which is what they will do) then you will only jump once if your jump action is in your touch event.

I’ve put the logic in my enterFrame loop before just where ‘touch == true’ and ‘onGround == true’ but really you could just stick it in your collision event handler as well. Below is an example. I just typed it up on the forum so you may potentially need to fix typos!

[lua]-----------------------------------------------------------------------------------------

– main.lua


display.setStatusBar( display.HiddenStatusBar )
local physics = require “physics”
local onAir = false
physics.start()
physics.setDrawMode(“hybrid”); --This is so I could see the physics bodies, comment & uncomment.

local background = display.newImage( “background.jpg” )

local ground = display.newImage( “ground.png” )
ground:setReferencePoint( display.BottomLeftReferencePoint )
ground.x, ground.y = 0, 320
ground.name = “ground”

local groundShape = { -240,-20, 240,-20, 240,20, -240,20 }
physics.addBody( ground, “static”, { friction=1.0, density=1.0, bounce=0, shape=groundShape } )

local character = display.newImage( “character.png” )
character.x = 70
character.y = 234

physics.addBody( character, { friction=1.0, density=2.0, bounce=0, radius=35 } )
character.isFixedRotation = true

local function onScreenTouch( event )

if (event.phase == “began”) then
doJump = true
elseif event.phase == “ended” then
doJump = false
end

end
Runtime:addEventListener( “touch”, onScreenTouch )

local function collis( event )
if (event.phase == “began” and event.other.name == “ground”) then
if doJump == true then
character:applyForce( 0, -2500, character.x, character.y )
end
end

end[/lua] [import]uid: 147305 topic_id: 29721 reply_id: 119342[/import]

Thanks ,but I need “automatically” jumping during hold finger on display. So again because I don’t know whether we understand.

If I touch-tap on display character jump once.
If I hold finger on display character jump automatically, it means: Character jump UP -> falls DOWN ( Collision with ground ) and again jump UP and…until I hold finger on display. If I away finger from display character stop jumping. This I need, please how do I make ?

[lua]-----------------------------------------------------------------------------------------

– main.lua


display.setStatusBar( display.HiddenStatusBar )
local physics = require “physics”
local onAir = false
physics.start()
physics.setDrawMode(“hybrid”); --This is so I could see the physics bodies, comment & uncomment.

local background = display.newImage( “background.jpg” )

local ground = display.newImage( “ground.png” )
ground:setReferencePoint( display.BottomLeftReferencePoint )
ground.x, ground.y = 0, 320
ground.name = “ground”

local groundShape = { -240,-20, 240,-20, 240,20, -240,20 }
physics.addBody( ground, “static”, { friction=1.0, density=1.0, bounce=0, shape=groundShape } )

local character = display.newImage( “character.png” )
character.x = 70
character.y = 234

physics.addBody( character, { friction=1.0, density=2.0, bounce=0, radius=35 } )
character.isFixedRotation = true
local onAir = false
local doJump = false

local function onScreenTouch( event )
if (event.phase == “began”) then
doJump = true
elseif event.phase == “ended” then
doJump = false
end
end
Runtime:addEventListener( “touch”, onScreenTouch )

local function jump( event )
if (doJump == true and onAir == false) then
character:applyForce( 0, -2500, character.x, character.y )
onAir = true
end
end
Runtime:addEventListener( “enterFrame”, jump );

local function collision( event )
if (event.phase == “began” and event.other.name == “ground”) then
onAir = false
end
end
character.collision = collision
character:addEventListener( “collision”, collision )[/lua] [import]uid: 163161 topic_id: 29721 reply_id: 119351[/import]

Well, that was the aim of the code i posted but I did see some issues with it. If you paste the below code into the main.lua if the game template it should do what you want(it did for me). I changed onAir to onGround as it was more intuitive as to what was going on. To make it work with your project you should be able to just replace the image names and uncomment out the background line.

[lua]-----------------------------------------------------------------------------------------

– main.lua


display.setStatusBar( display.HiddenStatusBar )
local physics = require “physics”
local onGround = true
physics.start()
physics.setDrawMode(“hybrid”); --This is so I could see the physics bodies, comment & uncomment.

–local background = display.newImage( “background.jpg” )

local ground = display.newImage( “grass.png” )
ground:setReferencePoint( display.BottomLeftReferencePoint )
ground.x, ground.y = 0, 320
ground.name = “ground”

local groundShape = { -240,-20, 240,-20, 240,20, -240,20 }
physics.addBody( ground, “static”, { friction=1.0, density=1.0, bounce=0, shape=groundShape } )

local character = display.newImage( “crate.png” )
character.x = 70
character.y = 234

physics.addBody( character, { friction=1.0, density=2.0, bounce=0, radius=35 } )
character.isFixedRotation = true

local function onScreenTouch( event )

if (event.phase == “began”) then
doJump = true
elseif event.phase == “ended” then
doJump = false
end

end
Runtime:addEventListener( “touch”, onScreenTouch )

local function collis( event )
if (event.phase == “began” and event.other.name == “ground”) then
onGround = true
elseif (event.phase == “ended” and event.other.name == “ground”) then
onGround = false
end

end

character.collision = collis
character:addEventListener( “collision”, collis )

function enterFrame()

if doJump == true and onGround == true then
character:applyForce( 0, -1200, character.x, character.y )
end

end

Runtime:addEventListener(“enterFrame”, enterFrame)[/lua] [import]uid: 147305 topic_id: 29721 reply_id: 119362[/import]

It works ! Very, very thank, you are saved me :slight_smile: [import]uid: 163161 topic_id: 29721 reply_id: 119364[/import]