Character-Jump tutorial Question

Hello,

I tried using the jump tutorial from

https://coronalabs.com/blog/2013/02/19/more-physics-tricks-explained/

First, everything works fine. But when I replace the character Rect with my own ImageRect(png, same size), its getting kinda slow. I need to press like 5-20 times until the character finaly moves up. 

What should I do?

Thanks

Looking through the tutorial, it didn’t really provide any “jumping” code. Just detecting when the player is on the group. I think we need to see your jumping code (touch event handler) and your image creation code to get an idea of what’s happening.

Rob

--character creation character = display.newImageRect("Data/Level\_1-3/level1\_textures/char01.png",85,150) character.x = CenterW-120; character.y = CenterH+30;

physics.addBody( character, "dynamic",  { density=1.0, friction=0.0, bounce=0.0 }   { shape={40,0,  40,85,   -40,85,    -40,0}, isSensor=true }  ) character.isFixedRotation = true character.canJump = 0

-- jump function function onScreenTouch( event ) print(character.canJump) if ( event.phase == "began" and character.canJump \> 0 ) then character:setLinearVelocity(0,-200) end return true end

If I use

character = display.newRect( CenterH, CenterH, 120,120 ) 

and a different shape everything works perfect.

Well, only my character creation is different from the tutorial.

Hi @volkenandt.mo,

The main premise of that tutorial is that it uses a 2-element physics body with the “foot” section that helps determine if the character is touching the ground. Did you implement that? If not, then it’s less accurate to know when the character is really, fully, totally touching the ground, and your “canJump” variable may not be resetting properly because of that. As a result, it may seem like you need to tap a ton of times to make the character jump, but that’s not anything to do with the physical behavior, but rather that the code thinks the character isn’t on the ground when it, by your judgment, it appears that it is.

Hope this helps,

Brent

function charCollide( self,event ) if ( event.selfElement == 2 and event.other.objType == "ground" ) then if ( event.phase == "began" ) then self.canJump = self.canJump+1 elseif ( event.phase == "ended" ) then self.canJump = self.canJump-1 end end end

ground = display.newImageRect("Data/ground.png",667,70) ground.x = CenterW ground.y = 288 ground.objType = "ground" physics.addBody( ground, "static", { friction=0.3, density=1.0, bounce=0} )

Hello, 

Thanks for your answer.

I used these lines from the tutorial to check if the “foot” section is on the ground.

Edit:

Ah, I found the problem.

if ( event.phase == "began" and character.canJump \> 0 ) then should be if ( event.phase == "began" and character.canJump = 0 ) then

:rolleyes:

But I dont understand why it worked at all before.  :huh:

M

Looking through the tutorial, it didn’t really provide any “jumping” code. Just detecting when the player is on the group. I think we need to see your jumping code (touch event handler) and your image creation code to get an idea of what’s happening.

Rob

--character creation character = display.newImageRect("Data/Level\_1-3/level1\_textures/char01.png",85,150) character.x = CenterW-120; character.y = CenterH+30;

physics.addBody( character, "dynamic",  { density=1.0, friction=0.0, bounce=0.0 }   { shape={40,0,  40,85,   -40,85,    -40,0}, isSensor=true }  ) character.isFixedRotation = true character.canJump = 0

-- jump function function onScreenTouch( event ) print(character.canJump) if ( event.phase == "began" and character.canJump \> 0 ) then character:setLinearVelocity(0,-200) end return true end

If I use

character = display.newRect( CenterH, CenterH, 120,120 ) 

and a different shape everything works perfect.

Well, only my character creation is different from the tutorial.

Hi @volkenandt.mo,

The main premise of that tutorial is that it uses a 2-element physics body with the “foot” section that helps determine if the character is touching the ground. Did you implement that? If not, then it’s less accurate to know when the character is really, fully, totally touching the ground, and your “canJump” variable may not be resetting properly because of that. As a result, it may seem like you need to tap a ton of times to make the character jump, but that’s not anything to do with the physical behavior, but rather that the code thinks the character isn’t on the ground when it, by your judgment, it appears that it is.

Hope this helps,

Brent

function charCollide( self,event ) if ( event.selfElement == 2 and event.other.objType == "ground" ) then if ( event.phase == "began" ) then self.canJump = self.canJump+1 elseif ( event.phase == "ended" ) then self.canJump = self.canJump-1 end end end

ground = display.newImageRect("Data/ground.png",667,70) ground.x = CenterW ground.y = 288 ground.objType = "ground" physics.addBody( ground, "static", { friction=0.3, density=1.0, bounce=0} )

Hello, 

Thanks for your answer.

I used these lines from the tutorial to check if the “foot” section is on the ground.

Edit:

Ah, I found the problem.

if ( event.phase == "began" and character.canJump \> 0 ) then should be if ( event.phase == "began" and character.canJump = 0 ) then

:rolleyes:

But I dont understand why it worked at all before.  :huh:

M