bad argument #2 to ' rectangle' (number expected, got nil)
Posted: Wed Mar 04, 2015 3:36 pm
So I'm trying to build off of the "Grid-Locked Player" Tutorial. And I've added a turret that should shoot at the player when within a certain distance of it.
Here's my code:
I get the error main.lua:88: bad argument #2 to ' rectangle' (number expected, got nil)
ANY HELP IS GREAT
Here's my code:
Code: Select all
function love.load()
enemyImage = love.graphics.newImage("turret.png")
enemyBullets = {}
bulletSpeed = 250
enemyDirection = 0
enemyX = 200
enemyY = 300
enemyOriginX = enemyImage:getWidth()/2
enemyOriginY = enemyImage:getHeight()/2
enemyFOV = 100
player = {
grid_x = 256,
grid_y = 256,
act_x = 200,
act_y = 200,
speed = 10
}
map = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 3, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 3, 1 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1 },
{ 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1 },
{ 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 3, 3, 0, 1, 0, 1, 0, 1, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 3, 3, 0, 1, 0, 1, 0, 1, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
}
end
function love.update(dt)
enemyDirection = findRotation(enemyX,enemyY,player.grid_x,player.grid_y)
player.act_y = player.act_y - ((player.act_y - player.grid_y) * player.speed * dt)
player.act_x = player.act_x - ((player.act_x - player.grid_x) * player.speed * dt)
for i,v in ipairs(enemyBullets) do
v.x = v.x + (v.dx * dt)
v.y = v.y + (v.dy * dt)
end
if player.grid_x and player.grid_y >= enemyFOV then
local startX = enemyOriginX
local startY = enemyOriginY
local angle = math.atan2((player.act_y - startY), (player.act_x) - startX)
local bulletDx = bulletSpeed * math.cos(angle)
local bulletDy = bulletSpeed * math.sin(angle)
table.insert(enemyBullets, {enemyX = startX, enemyY = startY, dx = bulletDx, dy = bulletDy})
end
end
function love.keypressed(key)
if key == "up" then
if testMap(0, -1) then
player.grid_y = player.grid_y - 32
end
elseif key == "down" then
if testMap(0, 1) then
player.grid_y = player.grid_y + 32
end
elseif key == "left" then
if testMap(-1, 0) then
player.grid_x = player.grid_x - 32
end
elseif key == "right" then
if testMap(1, 0) then
player.grid_x = player.grid_x + 32
end
end
end
function love.draw()
love.graphics.draw(enemyImage,enemyX,enemyY,enemyDirection,0.05,0.05,enemyOriginX,enemyOriginY)
love.graphics.setColor( 0, 255, 0, 200)
love.graphics.rectangle("fill", player.act_x, player.act_y, 32, 32)
love.graphics.setColor(128, 128, 128)
for i,v in ipairs(enemyBullets) do
love.graphics.rectangle("fill", v.x, v.y, 10, 10)
end
for y=1, #map do
for x=1, #map[y] do
if map[y][x] == 1 then
love.graphics.setColor( 255, 255, 255, 200)
love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
elseif map[y][x] == 3 then
love.graphics.setColor( 255, 0, 0, 200)
love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
end
end
end
end
function findRotation(x1,y1,x2,y2)
return math.atan2(y2 - y1, x2 - x1)
end
function testMap(x, y)
if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 1 then
return false
elseif map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 3 then
player.grid_y = 256
player.grid_x = 256
end
return true
end
ANY HELP IS GREAT