Page 1 of 1

Randomly Generated Images Not Drawing

Posted: Sat May 04, 2013 7:54 am
by kylewatson98
I am working on a project and I have came across a bug but I don't know how to fix it :/ Take a look:

Code: Select all

clouds = {}
clouds.__index = clouds

function clouds.init()
	local cld = {}
	setmetatable(cld, clouds)
	cld.batch = {}
	for i=1, 9 do
		table.insert(cld.batch, {love.graphics.newImage("graphics/cloud"..math.random(3)..".png"), math.random(width - 32), (i - 1)*1.92})
	end
	cld.v = 1
	return cld
end

function clouds:draw()
	love.graphics.setColor(255,255,255,255)
	for i=1, #self.batch do
		love.graphics.draw(self.batch[i][1], self.batch[i][2], self.batch[i][3])
	end
end

function clouds:update(dt)
	for i=1, #self.batch do
		self.batch[i][3] = self.batch[i][3] + self.v * 20
		if self.batch[i][3] < -16 then
			self.batch[i][3] = height
			self.batch[i][2] = math.random(width - 32)
			self.batch[i][1] = love.graphics.newImage("graphics/cloud"..math.random(3)..".png")
		end
	end
	
	if self.v < 10 then
		self.v = self.v + dt
	end
end
My canvas is scaled 8x8 and what I am trying to happen is the image to load up randomly spread down the height of the screen, they travel upwards until off the screen to then change image and start from the bottom again. I have passed all the right functions into the callback functions but I still don't know whats wrong.
Please help me. Thanks in advance, Kyle.

Re: Randomly Generated Images Not Drawing

Posted: Sat May 04, 2013 8:11 am
by Robin
Could you explain what does happen?

Also, in the future upload a .love.

Re: Randomly Generated Images Not Drawing

Posted: Sat May 04, 2013 8:25 am
by kylewatson98
Like it clearly states in the title, the randomly generated images do not show up...
I will just upload my give my main.lua code because that is the only thing that uses this class.

Code: Select all

function love.load()
	require "class/whale"
	require "class/clouds"
	require "class/cut1"
	require "class/speechB"
	height = 75
	width = 100
	love.graphics.setLine(1, "rough")
	love.graphics.setBackgroundColor(130, 220, 255)
	love.graphics.setDefaultImageFilter("nearest", "nearest")
	love.graphics.setFont(love.graphics.newImageFont("graphics/font.png", " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\""))
	whale = whale:init()
	clouds = clouds:init()
	cut1 = cut1:init()
	speechB = speechB:init()
end

function love.draw()
	love.graphics.scale(8, 8)
	whale:draw()
	clouds:draw()
	cut1:draw()
	speechB:draw()
	love.graphics.print(love.timer.getFPS(), 1, 1, 0, 0.125, 0.125)
end

function love.update(dt)
	whale:update(dt)
	clouds:update(dt)
	--cut1:update(dt)
end

function love.keyreleased(k)
	whale:keypressed(k)
end

Re: Randomly Generated Images Not Drawing

Posted: Sat May 04, 2013 9:00 am
by Robin
If you upload a .love, both the probability someone helps you is greatly increased and the average time before someone helps you is greatly reduced.

I've tried to find the solution, but without a .love I cannot do more.

Re: Randomly Generated Images Not Drawing

Posted: Sun May 05, 2013 10:33 am
by kylewatson98
https://www.dropbox.com/s/ohwayhc7jn7hb ... round.love
Sorry, didn't realise, kinda tired last night.

Re: Randomly Generated Images Not Drawing

Posted: Sun May 05, 2013 12:32 pm
by Zeliarden
Yo!
You are drawning the clouds outside of the screen
change the code

Code: Select all

self.batch[i][3] = self.batch[i][3] + self.v * 20
to

Code: Select all

self.batch[i][2] = self.batch[i][2] + self.v
for moving clouds

I recommended that you only use love.graphics.newImage once per image
something like:

Code: Select all

cloud[1] = love.graphics.newImage("graphics/cloud1.png")
cloud[2] = love.graphics.newImage("graphics/cloud2.png")
cloud[3] = love.graphics.newImage("graphics/cloud3.png")
...
self.batch[i][1] = cloud[math.random(3)]

Re: Randomly Generated Images Not Drawing

Posted: Sun May 05, 2013 2:16 pm
by kylewatson98
Thanks a bunch :3 Also great idea, just a little more efficient :ultraglee: