My initial thought was that a physics.body would have to be applied to the player{} but this threw back errors (probably down to poor syntax or something)
So would this be the case? That a body needs to be applied to the player? Or am I way off?
Any help or suggestions would be greatly appreciated.
UPDATE Being playing with the code and think I'm closer now than I was before, but still a little confused as to whats going on and why the physics aren't working quite right.
Code: Select all
-- love 2d 0.8.0
require("AnAL")
function love.load()
--love.graphics.setMode(650, 650, false, true, 0) --set the window dimensions to 650 by 650
door=love.graphics.newImage("door.png")
love.physics.setMeter(64)
world = love.physics.newWorld(0, 29.81*64, true)
player = {}
player.x=400
player.y=450
player.speed = 300
player.y_velocity = 450
player.tim=love.graphics.newImage("tim.png")
player.body= love.physics.newBody(world, player.x,player.y, "dynamic")
player.shape = love.physics.newRectangleShape(0,0,100,50)
player.fixture=love.physics.newFixture(player.body, player.shape);
gravity = 400
jump_height = -300
local imgwalk = love.graphics.newImage("timwalking.png")
local imgwalk1 = love.graphics.newImage("timwalking1.png")
local imgjump = love.graphics.newImage("timjumping.png")
anim = newAnimation(imgwalk, 128.5, 150, 0.08, 0)
anim:setMode("loop")
anim1 = newAnimation(imgwalk1, 128.5, 150, 0.08, 0)
anim1:setMode("loop")
anim2 = newAnimation(imgjump, 128.5, 150, 0.08, 0)
anim2:setMode("bounce")
activeImage = "standing" -- character states
objects = {}
objects.door = {}
objects.door.body = love.physics.newBody(world,50,550) --remember, the shape (the rectangle we create next) anchors to the body from its center, so we have to move it to 300/2, 300-50/2)
objects.door.shape = love.physics.newRectangleShape(0,0,100,100) --make a rectangle with a width of 300 and a height of 50
objects.door.fixture = love.physics.newFixture(objects.door.body, objects.door.shape);
end
function love.update(dt)
world:update(dt)
if player.y_velocity ~= 450 then -- we're probably jumping
player.y = player.y + player.y_velocity * dt -- dt means we wont move at
-- different speeds if the game lags
player.y_velocity = player.y_velocity + gravity * dt
activeImage = "jumping"
anim2:update(dt)
if player.y > 450 then -- we hit the ground again
player.y_velocity = 450
player.y = 450
end
end
if love.keyboard.isDown("right") then
player.x = player.x + (player.speed * dt)
activeImage = "walkingright"
anim:update(dt)
else
activeImage = "standing"
end
if love.keyboard.isDown("left") then
player.x = player.x - (player.speed * dt)
activeImage = "walkingleft"
anim1:update(dt)
end
end
function love.keypressed(key)
if key == " " then
if player.y_velocity == 450 then -- we're probably on the ground, let's jump
player.y_velocity = jump_height
end
end
end
function love.draw()
x, y = objects.door.body:getPosition( )
love.graphics.draw(door,x-50,y-50)
if activeImage == "walkingright" then
love.graphics.print('Walking right', 400, 300)
anim:draw(player.x,player.y)
end
if activeImage == "walkingleft" then
love.graphics.print('Walking left', 400, 300)
anim1:draw(player.x,player.y)
end
if activeImage == "jumping" then
love.graphics.print('Jumping', 400, 300)
anim2:draw(player.x,player.y)
end
if activeImage == "standing" then
love.graphics.print('Standing', 400, 300)
love.graphics.draw(player.tim, player.x, player.y)
end
end