I am making a game and i have a problem that the player can’t walk and jump at the same time how can i fix it?
Player.lua:
local physics = require "physics" physics.start() local Player = {} local life = 100 local Player\_mt = {\_\_index=Player} -- prep for metatable-based oop function Player:new(x,y) local instance = { x = x or 0, y = y or 0, life = 100, } setmetatable(instance,Player\_mt) -- now instance isa Player -- x=10 -- y=10 local sheetOptions = { width = 50, height = 100, numFrames = 6 } local sheet\_runningCat = graphics.newImageSheet( "hero\_sheet1.png", sheetOptions ) local sequences\_runningCat = { -- consecutive frames sequence { name = "normalRun", start = 1, count = 6, time = 800, loopCount = 0, loopDirection = "forward" } } instance.view = display.newSprite( sheet\_runningCat, sequences\_runningCat ) --instance.view = display.newImage("player.gif") -- and "owns" a display object instance.view.x = x instance.view.y = y physics.addBody( instance.view, { friction=1.0, density=1.0, bounce=0, radius=35 } ) instance.view.isFixedRotation = true instance.view:play() return instance end ------------------------------------------------- function Player:getX() return self.view.x end ------------------------------------------------- function Player:getY() return self.view.y end ------------------------------------------------- function Player:setX(x) self.view.x = x end function Player:setY(y) self.view.y = y end function Player:moveRight() --if self.view.x \<= display.contentWidth\*2-self.view.width then self.view.x = self.view.x+3 --end --img.x = self.x end function Player:moveLeft() if self.view.x \>= self.view.width/2 then self.view.x = self.view.x-3 end --img.x = self.x end function Player:Jump() if self.view.y \>=display.contentHeight/2 then self.view:applyForce( 0, -800, self.view.x, self.view.y ) end end function Player:isJumping(ground) if img.y\>ground then self.x = img.x self.y = img.y return true else self.x = img.x self.y = img.y return false end end function Player:setLife(subLife) self.life = self.life-subLife end function Player:getLife() return self.life end ------------------------------------------------- return Player
The code that make the walk and jump on main
local function gameLoop() if isJumping == true then player:Jump() isJumping = false end if where == "left" then player:moveLeft() elseif where == "right" then player:moveRight() end iEnemy:moveLeft() for i=1,5 do if player:getX() \< enemys[i]:getX() then enemys[i]:moveLeft() elseif player:getX()\> enemys[i]:getX() then enemys[i]:moveRight() end end end local function stopMoving(event) local phase = event.phase if phase == "ended" then where = "none" end end local function Jump(event) local phase = event.phase if phase == "ended" then isJumping = true end end local function moveLeft(event) local phase = event.phase where = "left" --display.currentStage:setFocus(moveLeft) if phase == "ended" then where = "none" --display.currentStage:setFocus(nil). end end local function moveRight(event) local phase = event.phase where = "right" --display.currentStage:setFocus(moveRight) if phase == "ended" then transition.cancel(trans) where = "none" --display.currentStage:setFocus(nil). end end