Page 1 of 1

(Solved) No objects are being drawn

Posted: Mon May 28, 2018 10:06 am
by Torbaz

Code: Select all

	enemy = {}
	enemies_controller = {}
	enemies_controller.enemies = {}
	
function love.load()
	player = {}
	player.x = 0
	player.y = 575
	player.bullets = {}
	player.cooldown = 40
	player.speed = 5
	player.fire = function()
		if player.cooldown <= 0 then
			player.cooldown = 40
			bullet = {}
			bullet.x = player.x + 35
			bullet.y = player.y - 5
			table.insert(player.bullets, bullet)
		end
	end
	enemies_controller:spawnEnemy()
end

function enemies_controller:spawnEnemy()
	enemy = {}
	enemy.x = 0
	enemy.y = 0
	enemy.bullets = {}
	enemy.cooldown = 40
	enemy.speed = 5
	table.insert(self.enemies, enemy)
end

function enemy:fire()
	if self.cooldown <= 0 then
		self.cooldown = 40
		bullet = {}
		bullet.x = self.x + 35
		bullet.y = self.y - 5
		table.insert(player.bullets, bullet)
end

function love.draw()
	-- Draw the player
	love.graphics.setColor(0, 0, 255)
	love.graphics.rectangle("fill", player.x, player.y, 80, 20)
	-- Draw Bullets
	love.graphics.setColor(255, 255, 255)
	for _, b in pairs(player.bullets) do
		love.graphics.rectangle("fill", b.x, b.y, 10, 10)
	end
end

function love.update(dt)
	player.cooldown = player.cooldown - 1
	if love.keyboard.isDown("right") then
		player.x = player.x + player.speed
	end

	if love.keyboard.isDown("left") then
		player.x = player.x - player.speed
	end

	if love.keyboard.isDown("space") then
		player.fire()
	end

	for i, b in ipairs(player.bullets) do
		if b.y < -10 then
			table.remove(player.bullets, i)
		end
		b.y = b.y - 5
	end

end
end
Nothing is being drawn when I run the program

Re: No objects are being drawn

Posted: Mon May 28, 2018 10:40 am
by KayleMaster
If this is 11+ version, you'd need the color values to be between 0-1, not 0-255.

edit: check bobbyjones answer

Re: No objects are being drawn

Posted: Mon May 28, 2018 10:43 am
by bobbyjones
So maybe I'm just not pairing them up properly but you have a stray end at the end of the file. Is all of the code you shown wrapped in another function?

Edit: found it. Remove the end at the end of the file and add after the end for enemy:fire(). You ended the if statement but did not end the function. I'm assuming you ended up with an error and just threw an extra end at the end. I've done that when I first started. I recommend indenting the code properly. It will allow you see things that are missing really easily.

Re: No objects are being drawn

Posted: Mon May 28, 2018 11:30 pm
by Torbaz
bobbyjones wrote: Mon May 28, 2018 10:43 am So maybe I'm just not pairing them up properly but you have a stray end at the end of the file. Is all of the code you shown wrapped in another function?

Edit: found it. Remove the end at the end of the file and add after the end for enemy:fire(). You ended the if statement but did not end the function. I'm assuming you ended up with an error and just threw an extra end at the end. I've done that when I first started. I recommend indenting the code properly. It will allow you see things that are missing really easily.
Thanks :)