Newww2This
Newww2This

Reputation: 13

Can someone test my Corona project and identify the issue with my jump function?

I'm all very new to this and everything in this project is just placeholders and scrap work. Some of you may recognize some of the graphics used from other Corona tutorials, but it's to help me get better, so don't be too judgemental. Here's a link to the download for the Corona Simulator: http://www.mediafire.com/download/6a78bsewgwsiyp2/GooMan.rar

Anyways, here's my issue. The jump button seems fine at first. If I hold it down, the character constantly jumps. And if I let go, he stops. If I simultaneously hold down jump while pushing one of the arrow buttons, he'll jump in that direction. However, it seems as though if I jump once, then I hit the jump button again RIGHT as the character is making contact with the ground, that he won't jump. There's a sudden lack of responsiveness. I literally have to pause slightly and wait for the character to fully hit the ground before I can make another successful jump. Why?

And here's all the relevant code for you to look at:

I have this at the beginning to help define my goo character's position:

local spriteInAir = false
local yspeed = 0
local oldypos = 0
local holding = false

I then created the jump button.

local bJump = display.newImage("Images/jumpbutton.png")
bJump.x = 445
bJump.y = 265
bJump.xScale = 0.78
bJump.yScale = 0.78

Followed up by creating an enterFrame Runtime Event which will update my goo character's position in the Y. Running the game at 60fps if that's relevant.

function checkSpeed()
yspeed = sprite.y - oldypos
oldypos = sprite.y
if yspeed == 0 then
spriteInAir = false
else
spriteInAir = true
end
end
Runtime:addEventListener( "enterFrame", checkSpeed )

Then the meat of it all. Created a function called hold which tells the game to not only make my goo character jump, but to keep my goo character constantly jumping as long as the bJump button is held down. Works perfectly. The "jumping" function is a touch event that listens for the hold function, and all of it is executed by the bJump button listening to the jumping function.

local function hold()
    if holding and spriteInAir == false then
      sprite:applyForce( 0, -8, sprite.x, sprite.y )
      sprite:setLinearVelocity(0, -350)
      spriteInAir = true
      return true
    end
end
local function jumping( event )
    if event.phase == "began" then
        display.getCurrentStage():setFocus( event.target )
        event.target.isFocus = true
        event.target.alpha = 0.6
        Runtime:addEventListener( "enterFrame", hold )
        holding = true
    elseif event.target.isFocus then
        if event.phase == "moved" then
        elseif event.phase == "ended" then
            holding = false
            event.target.alpha = 1
            Runtime:removeEventListener( "enterFrame", hold )
            display.getCurrentStage():setFocus( nil )
            event.target.isFocus = false
            spriteInAir = false
            return true
        end
    end
    return true
end
bJump:addEventListener("touch",jumping)

Anyone who can help me identify this problem, I'd greatly appreciate it!

Upvotes: 0

Views: 103

Answers (1)

Caleb P
Caleb P

Reputation: 361

You're using a velocity check to detect if the character is on the ground. It can take a short while to set it back to zero after colliding with Box2D, so the better way to do it is to use a collision sensor:

sprite.grounded = 0 -- Use a number to detect if the sprite is on the ground; a Boolean fails if the sprite hits two ground tiles at once

function sprite:collision(event)
  if "began" == event.phase then
    -- If the other object is a ground object and the character is above it...
    if event.other.isGround and self.contentBounds.yMax < event.other.contentBounds.yMin + 5 then
      sprite.grounded = sprite.grounded + 1 -- ...register a ground collision
    end
  elseif "ended" == event.phase then
    -- If the other object is a ground object and the character is above it...
    if event.other.isGround and self.contentBounds.yMax < event.other.contentBounds.yMin + 5 then
      sprite.grounded = sprite.grounded - 1 -- ...unregister a ground collision
    end
  end
end

sprite:addEventListener("collision")

Then, in your jump function, just check to see if sprite.grounded > 0. If it is, the player is grounded.

Upvotes: 1

Related Questions