Character jump question

How can I make my physics object in my game jump, without it coming straight down after moved?

Also this does not work from some reason, what I mean by that it is so that that function down there does not limit jumps. You can jump unlimited times. I would like to fix that.

local function movePlayer() motionx = 0 -- Variable used to move character along x axis speed = 5 -- Set Walking Speed local function moveLeft(event) if "began" == event.phase then motionx = - speed animation.xScale = -1 end if "ended" == event.phase then motionx = 0 end end local function moveRight(event) if "began" == event.phase then motionx = speed animation.xScale = 1 end if "ended" == event.phase then motionx = 0 end end local function moveUp(event) if "began" == event.phase and animation.canJump == 0 then transition.to(animation, {time = 750, y = animation.y - 100}) end end 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 animation.collision = charCollide animation:addEventListener("collision", animation) local function moveguy(event) --map.updateView() animation:setLinearVelocity(motionx \* 40, 0) map.positionCamera(animation.x - \_W / 15, animation.y) animation.rotation = 0 end Runtime:addEventListener("enterFrame", moveguy) leftButton:addEventListener("touch", moveLeft) rightButton:addEventListener("touch", moveRight) jumpButton:addEventListener("touch", moveUp) end movePlayer()

Thanks for your time :slight_smile:

The code below is how I use jump.  This code allows your object to double jump.  If you want to only single jump then remove the elseif singleJump == 1 portion.  You can adjust how high the object can jump by adjusting jumpPower.

Apply this event listener to your jump button:

[lua]local singleJump = 0

local jumpPower = -2

local function jump:touch( event )

   if event.phase == “began” then

      display.getCurrentStage():setFocus(self, event.id)

      self.numTouches = self.numTouches + 1

      if singleJump == 0 then --single jump

         singleJump = 1

         object:applyLinearImpulse(0, jumpPower, object.x, object.y)

      elseif singleJump == 1 then --double jump

         singleJump = 2

         object:applyLinearImpulse(0, jumpPower, object.x, object.y)

      end

   elseif event.phase == “ended” or event.phase == “cancelled” then

      display.getCurrentStage():setFocus(nil)

      self.numTouches = self.numTouches - 1

   end

   return true

end

jump:addEventListener( “touch”, jump )[/lua]

If your ground has an objectType of “ground”, then this event listener will reset your jump (so that you can jump again) when your object touches the ground:

[lua]local function object(self, event)

   if ( event.phase == “began” ) then

      if event.selfElement == 1 and event.other.objType == “ground” then                   

         singleJump = 0

      end

   end

   return true

end

object.collision = object

object:addEventListener( “collision”, object )[/lua]

The code below is how I use jump.  This code allows your object to double jump.  If you want to only single jump then remove the elseif singleJump == 1 portion.  You can adjust how high the object can jump by adjusting jumpPower.

Apply this event listener to your jump button:

[lua]local singleJump = 0

local jumpPower = -2

local function jump:touch( event )

   if event.phase == “began” then

      display.getCurrentStage():setFocus(self, event.id)

      self.numTouches = self.numTouches + 1

      if singleJump == 0 then --single jump

         singleJump = 1

         object:applyLinearImpulse(0, jumpPower, object.x, object.y)

      elseif singleJump == 1 then --double jump

         singleJump = 2

         object:applyLinearImpulse(0, jumpPower, object.x, object.y)

      end

   elseif event.phase == “ended” or event.phase == “cancelled” then

      display.getCurrentStage():setFocus(nil)

      self.numTouches = self.numTouches - 1

   end

   return true

end

jump:addEventListener( “touch”, jump )[/lua]

If your ground has an objectType of “ground”, then this event listener will reset your jump (so that you can jump again) when your object touches the ground:

[lua]local function object(self, event)

   if ( event.phase == “began” ) then

      if event.selfElement == 1 and event.other.objType == “ground” then                   

         singleJump = 0

      end

   end

   return true

end

object.collision = object

object:addEventListener( “collision”, object )[/lua]