Platforms in Game Error

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Sonic
Prole
Posts: 11
Joined: Tue Jan 02, 2018 6:25 pm
Contact:

Platforms in Game Error

Post by Sonic »

I have been working on this for a while now but I received an error stating that the platform is nil.

Error:

Code: Select all

game.lua:17: bad argument #2 to 'rectangle' (number expected,
 got nil)
Full Code:

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
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!
User avatar
zorg
Party member
Posts: 3441
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Platforms in Game Error

Post by zorg »

I'm assuming you just dumped your separate source files together above, since there's no separate game.lua you have showed us, that would have love.graphics.rectangle call on the 17th line as the error message says...
Either post a löve file, or separate the code as how it originally was, or we can't help... or at least you're making all our lives that much harder.

Also, that bullet cleaner code looks bad, since it has the chance of trying to modify enemy.bullets after you have already removed it.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 31 guests