Platforms in Game Error
Posted: Sat Jan 13, 2018 3:04 am
I have been working on this for a while now but I received an error stating that the platform is nil.
Error:
Full Code:
I definitely defined platform.x, platform.y, platform.width, and platform.height. Im using Zerobrane for the editing.
If anyone could help me fix this error, that'd be great. Thanks!
Error:
Code: Select all
game.lua:17: bad argument #2 to 'rectangle' (number expected,
got nil)
Code: Select all
-- main tables
platform = {}
player = {}
enemy = {}
-- enemy controller tables
enemies = {}
enemies_controller = {}
enemies_controller.enemies = {}
-- enemy bullet tables
enemy.bullets = {}
enemy.cooldown = 30
enemy.fire = function()
if enemy.cooldown <= 0 then
enemy.cooldown = 30
bullet = {}
bullet.x = 35
bullet.y = 273
table.insert(enemy.bullets, bullet)
end
-- some assets/music
background_image = love.graphics.newImage('assets/backgrount.png')
-- ^ typo ¯\_(ツ)_/¯
enemy.fireSound = love.audio.newSource('sfxmusic/shoot.wav')
gameover = false
-- Collision
function checkCollisions(enemy, bullets)
for _,e in pairs(enemy) do
for _,b in pairs(bullets) do
if b.y <= e.y + e.height and b.x > e.x + e.width then
gameover = true
end
end
end
end
-- platform
platform.width = love.graphics.getWidth()
platform.height = love.graphics.getHeight()
platform.x = 0
platform.y = platform.height / 2
-- player tables
player.x = love.graphics.getWidth() / 2.05
player.y = love.graphics.getHeight() / 2.05
player.speed = 300
player.img = love.graphics.newImage('assets/sprite.png')
player.ground = player.y -- This makes the character land on the plaform.
player.y_velocity = 0 -- Whenever the character hasn't jumped yet, the Y-Axis velocity is always at 0.
player.jump_height = -300 -- Whenever the character jumps, he can reach this height.
player.gravity = -500
--Enemy
enemy.x = love.graphics.getWidth() / 2.05
enemy.y = love.graphics.getHeight() / 2.05
enemy.img = love.graphics.newImage('assets/sprite2.png')
enemy.ground = player.y
enemy.y_velocity = 0
-- enemy image/sprite
enemy.img = love.graphics.newImage('assets/sprite2.png')
function love.load()
background_image = love.graphics.newImage('assets/backgrount.png')
function enemies_controller:spawnEnemy()
end
end
function love.update(dt)
enemy.cooldown = enemy.cooldown - 1
for i=10,1,-1 do
enemy:fire()
end
-- Bullet creator/cleaner (removes bullets off screen for memory saving)
for i,b in ipairs(enemy.bullets) do
if b.x < 10 then
table.remove(enemy.bullets, i)
end
b.x = b.x + 4
end
-- Player controls
if love.keyboard.isDown('d') or love.keyboard.isDown('right') then
if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
player.x = player.x + (player.speed * dt)
end
elseif love.keyboard.isDown('a') or love.keyboard.isDown('left')then
if player.x > 0 then
player.x = player.x - (player.speed * dt)
end
end
if love.keyboard.isDown('space') or love.keyboard.isDown('up') then
if player.y_velocity == 0 then
player.y_velocity = player.jump_height
end
end
if player.y_velocity ~= 0 then
player.y = player.y + player.y_velocity * dt
player.y_velocity = player.y_velocity - player.gravity * dt
end
if player.y > player.ground then
player.y_velocity = 0
player.y = player.ground
end
end
checkCollisions(enemies_controller.enemies, enemy.bullets)
end
function love.draw()
love.graphics.setBackgroundColor(100, 100, 100)
if gameover then
love.graphics.setNewFont(20)
love.graphics.print("Game Over")
return
end
love.graphics.setColor(0, 255, 50)
love.graphics.rectangle('fill', platform.x, platform.y, platform.width, platform.height)
love.graphics.setColor(255, 255, 255)
love.graphics.draw(player.img, player.x, player.y, 0, 1, 1.5, 2.2, 32)
love.graphics.draw(enemy.img, 1, enemy.y, 0, 1, 1.5, 1.6, 32)
-- draw bullets
love.graphics.setColor(255, 255, 0)
for _,b in pairs(enemy.bullets) do
love.graphics.rectangle("fill", b.x, b.y, 10, 5)
end
end
If anyone could help me fix this error, that'd be great. Thanks!