Thanks so much! I managed to fix it thanks to you.
Randomly off-topic, but any idea why I’m not able to jump while holding the move button(s) for my character? Here’s the full code.
[code]
local physics = require( “physics” )
physics.start()
–physics.setDrawMode( “hybrid” )
– Shapes
local dirtPlatformShape = { -97,-99, -98,-124, 98,-123, 98,-100 }
dirtPlatformShape.density = 1; dirtPlatformShape.friction = 0.3; dirtPlatformShape.bounce = 0.0;
local smalldirtPlayformShape = { -98.5,-32.5, 99.5,-31, 99,-50, -98.5,-50 }
smalldirtPlayformShape.density = 1; smalldirtPlayformShape.friction = 0.3; smalldirtPlayformShape.bounce = 0.0;
local dirtPlatform_3 = display.newImageRect( “dirtPlatform.png”, 199, 250 )
dirtPlatform_3.x = 211
dirtPlatform_3.y = 475
physics.addBody( dirtPlatform_3, “static”,
{density=dirtPlatformShape.density, friction=dirtPlatformShape.friction, bounce=dirtPlatformShape.bounce, shape=dirtPlatformShape}
)
local dirtPlatform_2 = display.newImageRect( “dirtPlatform.png”, 199, 250 )
dirtPlatform_2.x = 529
dirtPlatform_2.y = 475
physics.addBody( dirtPlatform_2, “static”,
{density=dirtPlatformShape.density, friction=dirtPlatformShape.friction, bounce=dirtPlatformShape.bounce, shape=dirtPlatformShape}
)
local dirtPlatform = display.newImageRect( “dirtPlatform.png”, 199, 250 )
dirtPlatform.x = 794
dirtPlatform.y = 475
physics.addBody( dirtPlatform, “static”,
{density=dirtPlatformShape.density, friction=dirtPlatformShape.friction, bounce=dirtPlatformShape.bounce, shape=dirtPlatformShape}
)
local ball = display.newImageRect( “ball.png”, 32, 32 )
ball.x = 521
ball.y = 304
physics.addBody( ball, { density=1, friction=1, bounce=0.,} )
ball.isFixedRotation = true
local ground = display.newImageRect( “ground.png”, 1024, 50 )
ground.x = 512
ground.y = 600
physics.addBody( ground, “static”, { density=1, friction=1, bounce=0 } )
–Draw the Walls
local leftWall = display.newRect(-8,0, 16, 793)
leftWall:setFillColor(255,255,255)
leftWall.isVisible = false
physics.addBody( leftWall, “static”, { density=1, friction=1, bounce=0 } )
local rightWall = display.newRect(1016,0, 16, 793)
rightWall:setFillColor(255,255,255)
rightWall.isVisible = false
physics.addBody( rightWall, “static”, { density=1, friction=1, bounce=0 } )
–Draw the movement buttons
local leftButton = display.newImage(“leftButton.png”)
leftButton.x = display.contentWidth / 10
leftButton.y = display.contentHeight - 85
local rightButton = display.newImage(“rightButton.png”)
rightButton.x = display.contentWidth / 4
rightButton.y = display.contentHeight - 85
local jumpButton = display.newImage(“jumpButton.png”)
jumpButton.x = display.contentWidth - 75
jumpButton.y = display.contentHeight - 85
– Movement Functions
function moveLeft(event)
ball.x = ball.x - 3
end
function moveRight(event)
ball.x = ball.x + 3
end
–Movement
function leftButton:touch(event)
if (event.phase == “began”) then
Runtime:addEventListener(“enterFrame”, moveLeft)
display.getCurrentStage():setFocus(event.target, event.id);
elseif (event.phase == “ended”) then
Runtime:removeEventListener(“enterFrame”, moveLeft)
display.getCurrentStage():setFocus(event.target, nil);
end
end
function rightButton:touch(event)
if (event.phase == “began”) then
Runtime:addEventListener(“enterFrame”, moveRight)
display.getCurrentStage():setFocus(event.target, event.id);
elseif (event.phase == “ended”) then
Runtime:removeEventListener(“enterFrame”, moveRight)
display.getCurrentStage():setFocus(event.target, nil);
end
end
function jumpButton:tap(event)
if ball.canJump then
ball:applyForce(0, -600, ball.x, ball.y)
end
end
–Limiting the jump of the player
local function onCollision(self, event )
if ( event.phase == “began” ) then
ball.canJump = true
elseif ( event.phase == “ended” ) then
ball.canJump = false
end
end
–Setting the ball collision to onCollision
ball.collision = onCollision
–Event Listeners
leftButton:addEventListener(“touch”, leftButton)
rightButton:addEventListener(“touch”, rightButton)
jumpButton:addEventListener(“tap”, jumpButton)
ball:addEventListener( “collision”, ball)
[/code] [import]uid: 131400 topic_id: 23081 reply_id: 92295[/import]