First post, and can I say thanks to the many people who have provided tutorials and other advice - it’s been a great help getting started!
I’ve come across an issue with what seems like such an easy thing for other people to do: player jumping.
Basically, I’m trying to create a Doodle Jump style continuous jump upon collision with a static object. This is working fine in principle, however when coming down the player gets stuck just above the ground - just kind of hangs there in the air. This can be overcome by moving left or right, or by touching the screen anywhere (there’s that d-pad stop function in there).
Any help you all can give on overcoming this would be great - no doubt it’s something very simple, but I only started two days ago! (no coding experience)
-- add player
local player = display.newImage('player\_testsprite.png')
player.x = \_W/2
player.y = \_H/1.255
physics.addBody(player, { bounce = 0, density = 100, friction = 100})
player.isFixedRotation = true
-- Player wraps around the screen
local function wrap (event)
if player.x \< 0 then
player.x = \_W
end
if player.x \> \_W then
player.x = 0
end
end
Runtime:addEventListener("enterFrame", wrap)
-- add grass floor
local ground = display.newImage('Grass\_Floor.png')
ground.y = \_H - ground.contentHeight/3
ground.name = "ground"
physics.addBody(ground, "static", { bounce = 0, friction = 10})
-- add d-pad
local up = display.newImage ("upx.png")
up.x = 70
up.y = 750
local left = display.newImage ("leftx.png")
left.x = 25
left.y = 785
local right = display.newImage ("rightx.png")
right.x = 115
right.y = 785
local motionx = 0
local motiony = 0
local speed = 10
local function moveplayer (event)
player.x = player.x + motionx
player.y = player.y + motiony
end
Runtime:addEventListener("enterFrame", moveunicorn)
function onGround(event)
if event.phase == "began" then
canJump = true
elseif event.phase == "ended" then
canJump = false
end
end
player:addEventListener("collision", onGround)
-- Jumping function
function jump(event)
if canJump then
motiony = -10
end
end
Runtime:addEventListener("enterFrame", jump)
function stop (event)
if event.phase == "ended" then
motionx = 0
motiony = 0
end
end
Runtime:addEventListener("touch", stop)
-- Left/Right function
function left:touch()
motionx = -speed
motiony = 0
end
left:addEventListener("touch", left)
function right:touch()
motionx = speed
motiony = 0
end
right:addEventListener("touch", right)
What’s going wrong here?
p.s. Physics is being called at the beginning as per normal [import]uid: 45010 topic_id: 7911 reply_id: 307911[/import]