What this attempts to do is use the example and add the simple functionality of clicking somewhere on the screen and adding a platform.
Code: Select all
function fCon(a,b,coll)
end
function con(a,b,coll)
player.onGround = true
end
function lCon(a,b,coll)
end
function result(a,b,coll)
end
function love.load()
world = love.physics.newWorld(0,0,650,650)
world:setCallbacks(fCon,con,lCon,result)
world:setGravity(0,700)
world:setMeter(64)
bodies = {}
shapes = {}
bodies[0] = love.physics.newBody(world,650/2,625,0,0)
shapes[0] = love.physics.newRectangleShape(bodies[0],0,0,650,50,0)
bodies[1] = love.physics.newBody(world,650/2,650/2,15,0)
shapes[1] = love.physics.newCircleShape(bodies[1],0,0,20)
love.graphics.setBackgroundColor(104, 136, 248)
love.graphics.setMode(650, 650, false, true, 0)
player = {bodies[1],onGround=false}
end
function love.update(dt)
world:update(dt)
if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right
bodies[1]:applyForce(400, 0)
elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
bodies[1]:applyForce(-400, 0)
end
if love.keyboard.isDown("up") and player.onGround == true then
bodies[1]:applyImpulse(0,-50,0,0)
player.onGround = false
end
if love.mouse.isDown("l") then
local x=#bodies+1
bodies[x] = love.physics.newBody(world,love.mouse.getX(),love.mouse.getY(),0,0)
shapes[x]= love.physics.newRectangleShape(bodies[x],0,0,3,1,0)
end
end
function love.draw()
local x1, y1, x2, y2, x3, y3, x4, y4 = shapes[0]:getBoundingBox()
local boxwidth = x3 - x2
local boxheight = y2 - y1
love.graphics.setColor(72, 160, 14)
love.graphics.rectangle("fill", bodies[0]:getX() - boxwidth/2, bodies[0]:getY() - boxheight/2, boxwidth, boxheight)
love.graphics.setColor(193, 47, 14)
love.graphics.circle("fill", bodies[1]:getX(), bodies[1]:getY(), shapes[1]:getRadius(), 20)
end