The problem that kept scratching my head is that whenever I press and hold the spacebar to jump, it will jump, but since my code does double jumping, pressing “a” or “d” would also cause it to jump.
Here’s the code:
aye = display.newGroup() physics = require("physics") physics.start() --physics.setDrawMode("hybrid") offset = 520 rect = display.newRect(display.contentCenterX,display.contentCenterY,100,100) rect2 = display.newRect(display.contentCenterX+offset,display.contentCenterY+200,1000,20) physics.addBody(rect,"dynamic", {density = 1,bounce = 0}, {box = {halfWidth = 50,halfHeight = 3,x = 0,y = 50},isSensor = true} ) ground1 = display.newRect(display.contentCenterX+offset+1500,display.contentCenterY+100,1000,20) physics.addBody(rect,"dynamic", {density = 1,bounce = 0}, {box = {halfWidth = 50,halfHeight = 6,x = 0,y = 50},isSensor = true} ) ground1.objType = 'ground' rect2.objType = "ground" physics.addBody(rect2,"static",{bounce = 0,friction = 0.4}) physics.addBody(ground1,"static",{bounce = 0,friction = 0.4}) rect.isFixedRotation = true rect.sensorOverlaps = 0 function lerp(a,b,t) return a \* (1-t) + b \* t end -- OUTSOURCING TO THE MAX! physicsGroup = display.newGroup() camera = display.newGroup() physicsGroup:insert(rect) physicsGroup:insert(rect2) physicsGroup:insert(ground1) camera:insert(physicsGroup) function ayeet() distanceX,distanceY = (0+rect.x),(0+rect.y) transition.to(camera,{time = 20,x = -distanceX+display.actualContentWidth/64,y = -distanceY+display.actualContentHeight/64,onComplete = ayeet}) end --- CAMERA AND CHARACTER physics.setGravity(0,55.6) -- CAMERA function lerpTo() transition.to(camera,{time = 65,x = lerp(camera.x,-rect.x,0.32)+display.actualContentWidth/6.5,y = lerp(camera.y,-rect.y,0.32)+display.actualContentHeight/6.5,onComplete = lerpTo}) end --- CHARACTER ---- CHARACTER ANIMATIONS ---- CHARACTER MOVEMENT double = 2 local X,Y = rect:getLinearVelocity() isColliding = false keyed = false allow = false currentlyDown = {a = false,d = false,space = false} local function move(press) local X,Y = rect:getLinearVelocity() local kp = press.phase local k = press.keyName if kp == "down" then if k == "a" then currentlyDown.a = true elseif k == "d" then currentlyDown.d = true end else if k == "a" then currentlyDown.a = false elseif k == "d" then currentlyDown.d = false end end print("Moving left = " .. tostring(currentlyDown.a) .. " | Moving right = " .. tostring(currentlyDown.d)) if kp == "down" and k == "space" then currentlyDown.space = true print("spaceDown") end if kp == "up" and k == "space" then currentlyDown.space = false print("spaceUp") end if currentlyDown.a == true then rect:setLinearVelocity(-500,Y) allow = false rect.linearDamping = 0 elseif currentlyDown.d == true then rect:setLinearVelocity(500,Y) allow = false rect.linearDamping = 0 end -------------------------------------------------------------------------------------------------------------- JUMP -------------------------------------------------------------------------------------------------------------- if currentlyDown.space == true and double \> 0 then print("Moving left = " .. tostring(currentlyDown.a) .. " | Moving right = " .. tostring(currentlyDown.d) .. " JUMP") if (currentlyDown.a == false or currentlyDown.d == false) then allow = false rect:setLinearVelocity(X,-600) double = double - 1 rect.linearDamping = 0 end end -------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------- end --[[NOTHING SPECIAL a rect:setLinearVelocity(-500,Y) allow = false rect.linearDamping = 0 d rect:setLinearVelocity(500,Y) allow = false rect.linearDamping = 0 space allow = false rect:setLinearVelocity(X,-600) double = double - 1]]-- local function jumper(self,press) if press.selfElement == 2 and press.other.objType == "ground" then if press.phase == "began" then isColliding = true double = 2 elseif press.phase == "ended" then isColliding = false end end end function rect.key(event) active = 0 for \_,v in pairs(currentlyDown) do if v == true then active = 1 allow = false end end if active == 1 then keyed = true allow = false else keyed = false allow = true end end function rect.enterFrame() if allow and not keyed then if isColliding then rect.linearDamping = 8 else rect.linearDamping = 0 end end end lerpTo() rect.collision = jumper rect:addEventListener("collision") Runtime:addEventListener("key",move) Runtime:addEventListener("key",rect) Runtime:addEventListener("enterFrame",rect)
Really frustates me, I’ve been working on this issue for almost 3 days, probably? But I need answers.
Thanks in advance.
(I’m new to posting topics, so I’m not really sure if I did the topic format right.)