Code: Select all
Error
main.lua:43: Incorrect parameter type: expected userdata.
Traceback
[C]: in function 'getX'
main.lua:43: in function 'player_update'
main.lua:21: in function 'update'
[C]: in function 'xpcall'
Code: Select all
function love.load()
love.physics.setMeter(100)
world = love.physics.newWorld(0,0,true)
ship = love.graphics.newImage("ship.png")
bullet = love.graphics.newImage("bullet.png")
player = {}
player.body = love.physics.newBody(world, love.graphics.getWidth()/2, love.graphics.getHeight()/2, "dynamic")
player.shape = love.physics.newRectangleShape(34, 44)
player.fixture = love.physics.newFixture(player.body, player.shape)
player.accel = 75
player.turnSpeed = 15
player.direction = 0.5 * math.pi
bullets = { }
end
function love.update(dt)
world:update(dt)
player_update(dt)
bullets_update(dt)
end
function player_update(dt)
--turning
if love.keyboard.isDown("right") then
player.direction = player.direction + (dt * player.turnSpeed)
elseif love.keyboard.isDown("left") then
player.direction = player.direction - (dt * player.turnSpeed)
end
--forward and back
if love.keyboard.isDown("up") then
player.body:applyForce(player.accel * -math.cos(player.direction), player.accel * -math.sin(player.direction))
elseif love.keyboard.isDown("down") then
player.body:applyForce(player.accel * math.cos(player.direction), player.accel * math.sin(player.direction))
end
--shoot bullets
if love.keyboard.isDown(" ") then
table.insert(bullets, {})
bullets[#bullets].body = love.physics.newBody(world, player.body.getX(), player.body.getY(), "dynamic")
bullets[#bullets].shape = love.physics.newRectangleShape(5, 20)
bullets[#bullets].fixture = love.physics.newFixture(bullets[#bullets].body, bullets[#bullets].shape)
bullets[#bullets].speed = player.body:getLinearVelocity() + 100
bullets[#bullets].direction = player.direction
end
--screen looping
if player.body:getX() <0 then
player.body:setPosition(love.graphics.getWidth(), player.body:getY())
elseif player.body:getX() > love.graphics.getWidth() then
player.body:setPosition(0, player.body:getY())
end
if player.body:getY() <0 then
player.body:setPosition(love.graphics.getHeight(), player.body:getX())
elseif player.body:getY() > love.graphics.getHeight() then
player.body:setPosition(0, player.body:getX())
end
end
--update bullets
function bullets_update(dt)
for i, o in ipairs(bullets) do
o.body:applyForce(o.speed + math.cos(o.direction), o.speed + math.sin(o.direction))
--check for out of bounds
if (o.x < -10) or (o.x > love.graphics.getWidth() + 10) or (o.y < -10) or (o.y > love.graphics.getHeight() + 10) then
table.remove(bullets, i)
end
end
end
--draw everything on the screen;
function love.draw()
love.graphics.draw(ship, player.body:getX(), player.body:getY(), player.direction - 0.5 * math.pi, 1, 1, 17, 22)
for i, o in ipairs(bullets) do
love.graphics.draw(bullet, o.body.getX(), o.body.getY(), o.direction - 0.5 * math.pi, 1, 1, 3, 3)
end
end