Images turn into white squares after time
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Images turn into white squares after time
for some odd reason, whenever I run my program (not willing to call it a game yet) the image I have set to spawn will start spawning, and then after a few seconds(30 or so) the images will blank out and be replaced with white squares, can someone take a look at the code and see if they can find out what the problem is, because I have no idea.
- Attachments
-
- Asteroid.love
- (24.49 KiB) Downloaded 198 times
Re: Images turn into white squares after time
Seems to be a problem with your system. Can you post your specs, especially graphics card/chip and drivers.
btw. the way you use classes seems to be pretty weird. You create a new object table inside your Asteroid class to handle them but the sense of a class is to be able to create an instance of it - so if you set up an asteroid object it shouldn't create it's own table objects and update/draw them. You didn't asked for it but I want to suggest you to look at this piece here.
btw. the way you use classes seems to be pretty weird. You create a new object table inside your Asteroid class to handle them but the sense of a class is to be able to create an instance of it - so if you set up an asteroid object it shouldn't create it's own table objects and update/draw them. You didn't asked for it but I want to suggest you to look at this piece here.
Last edited by MadByte on Thu Mar 24, 2016 12:38 pm, edited 3 times in total.
Re: Images turn into white squares after time
Works for me. I see you create a new image every time you add an asteroid. You should try to reuse the image as much as possible. Maybe you're running out of video memory?
On an unrelated note, I've noticed that this loop should go in reverse order:
That's because when you remove say element 5, what was element 6 becomes element 5, but you don't check element 5 again, so that element goes unchecked (i.e. it is skipped). If you go in reverse order, if you remove element 5, you don't care that 6 becomes 5, because 6 is already checked, and the next you will check is element 4.
In your case it probably doesn't matter because there will seldom be more than 1 asteroid removed per pass, but it's good to be trained in recognizing that kind of action and remedying it in general.
Here's an example of how to do that:
On an unrelated note, I've noticed that this loop should go in reverse order:
Code: Select all
for i, a in ipairs (asteroid) do
a.x = a.x - (200 * dt)
if a.x < -10 then
table.remove(asteroid, i)
end
end
In your case it probably doesn't matter because there will seldom be more than 1 asteroid removed per pass, but it's good to be trained in recognizing that kind of action and remedying it in general.
Here's an example of how to do that:
Code: Select all
for i = #asteroid, 1, -1 do
local a = asteroid[i]
a.x = a.x - (200 * dt)
if a.x < -10 then
table.remove(asteroid, i)
end
end
Re: Images turn into white squares after time
Yeah I figured it'd be a problem with my computer, but my specs are fine, or should beMadByte wrote:Seems to be a problem with your system. Can you post your specs, especially graphics card/chip and drivers.
btw. the way you use classes seems to be pretty weird. You create a new object table inside your Asteroid class to handle them but the sense of a class is to be able to create an instance of it - so if you set up an asteroid object it shouldn't create it's own table objects and update/draw them. You didn't asked for it but I want to suggest you to look at this piece here.
GPU : AMD Radeon R9 270
intel core i5-4460 3.20 Ghz
1 8 GB DDR3 RAM
the computers been acting weird lately so Its possible some parts are broken.
also, not sure what you mean by
im still new to programming hahaYou create a new object table inside your Asteroid class to handle them but the sense of a class is to be able to create an instance of it - so if you set up an asteroid object it shouldn't create it's own table objects and update/draw them.
Also about the other guys point
how do I reuse the image? Again, fairly newI see you create a new image every time you add an asteroid. You should try to reuse the image as much as possible.
Re: Images turn into white squares after time
Actually you can see it by looking at my example above.how do I reuse the image? Again, fairly new
Code: Select all
...
local Asteroid = Object:extend()
local image = love.graphics.newImage("assets/asteroid.png")
function Asteroid:new(x, y)
self.image = image
...
I mean this:also, not sure what you mean by...You create a new object table inside your Asteroid class to handle them but the sense of a class is to be able to create an instance of it - so if you set up an asteroid object it shouldn't create it's own table objects and update/draw them.
Code: Select all
function Asteroid:update(dt)
createEnemyTimer = createEnemyTimer - 1 * dt
if createEnemyTimer < 0 then
createEnemyTimer = createEnemyTimerMax
--random number
randomNumber = math.random(128, love.window.getHeight() - 200)
--end random number
newAsteroid = {x = asteroid.x, y = randomNumber, img = asteroid.img}
table.insert(asteroid, newAsteroid)
end
for i, a in ipairs (asteroid) do
a.x = a.x - (200 * dt)
if a.x < -10 then
table.remove(asteroid, i)
end
end
end
function Asteroid:draw(dt)
for i,asteroid in ipairs(asteroid) do
love.graphics.draw(asteroid.img, asteroid.x, asteroid.y)
end
end
your specs shouldn't be the problem at all.
Re: Images turn into white squares after time
MadByte wrote:Actually you can see it by looking at my example above.how do I reuse the image? Again, fairly new
Code: Select all
... local Asteroid = Object:extend() local image = love.graphics.newImage("assets/asteroid.png") function Asteroid:new(x, y) self.image = image ...
I mean this:also, not sure what you mean by...You create a new object table inside your Asteroid class to handle them but the sense of a class is to be able to create an instance of it - so if you set up an asteroid object it shouldn't create it's own table objects and update/draw them.
You use your Asteroid.lua as an "object handler" on it's own by letting it create new asteroids and then update and draw them inside the class. But this is the opposit to what a class is meant for. In my example the asteroid.lua creates a new asteroid and the update and draw process happens somewhere else. You need to make sure that your class just hold the object itself.Code: Select all
function Asteroid:update(dt) createEnemyTimer = createEnemyTimer - 1 * dt if createEnemyTimer < 0 then createEnemyTimer = createEnemyTimerMax --random number randomNumber = math.random(128, love.window.getHeight() - 200) --end random number newAsteroid = {x = asteroid.x, y = randomNumber, img = asteroid.img} table.insert(asteroid, newAsteroid) end for i, a in ipairs (asteroid) do a.x = a.x - (200 * dt) if a.x < -10 then table.remove(asteroid, i) end end end function Asteroid:draw(dt) for i,asteroid in ipairs(asteroid) do love.graphics.draw(asteroid.img, asteroid.x, asteroid.y) end end
your specs shouldn't be the problem at all.
mkay well ill worry about all that some other time as im sure it doesnt have anything to do with the images disappearing lol.
and yeah my specs shouldnt be a problem but I think part of my computer are actually broken, so that might help XD The thing is though if I run the program, then click off the window so it isn't focused, this doesnt happen. It only happens when I have the window focused, :/
- slime
- Solid Snayke
- Posts: 3160
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Images turn into white squares after time
If you create enough images that the graphics driver runs out of VRAM to use, it could cause instabilities and seemingly random graphics glitches (on top of the obvious performance issues). I'd worry about that now rather than later.
Re: Images turn into white squares after time
well yeah im gonna worry about the creating new images thing, but not the other stuff they were talking about.slime wrote:If you create enough images that the graphics driver runs out of VRAM to use, it could cause instabilities and seemingly random graphics glitches (on top of the obvious performance issues). I'd worry about that now rather than later.
Re: Images turn into white squares after time
MadByte wrote:Actually you can see it by looking at my example above.how do I reuse the image? Again, fairly new
Code: Select all
... local Asteroid = Object:extend() local image = love.graphics.newImage("assets/asteroid.png") function Asteroid:new(x, y) self.image = image ...
I mean this:also, not sure what you mean by...You create a new object table inside your Asteroid class to handle them but the sense of a class is to be able to create an instance of it - so if you set up an asteroid object it shouldn't create it's own table objects and update/draw them.
You use your Asteroid.lua as an "object handler" on it's own by letting it create new asteroids and then update and draw them inside the class. But this is the opposit to what a class is meant for. In my example the asteroid.lua creates a new asteroid and the update and draw process happens somewhere else. You need to make sure that your class just hold the object itself.Code: Select all
function Asteroid:update(dt) createEnemyTimer = createEnemyTimer - 1 * dt if createEnemyTimer < 0 then createEnemyTimer = createEnemyTimerMax --random number randomNumber = math.random(128, love.window.getHeight() - 200) --end random number newAsteroid = {x = asteroid.x, y = randomNumber, img = asteroid.img} table.insert(asteroid, newAsteroid) end for i, a in ipairs (asteroid) do a.x = a.x - (200 * dt) if a.x < -10 then table.remove(asteroid, i) end end end function Asteroid:draw(dt) for i,asteroid in ipairs(asteroid) do love.graphics.draw(asteroid.img, asteroid.x, asteroid.y) end end
your specs shouldn't be the problem at all.
Am I still making more images in this example? cuz the whitebox thing still happens
- Attachments
-
- thing.love
- (36.46 KiB) Downloaded 162 times
Re: Images turn into white squares after time
From what i see, you create a new image..
everytime you instantiate a "Player". So, if you create 300 players, you will also load the same file 300 times.
Try adding to your main love.load()
and when you draw the player, just use playerImg as you would.
Tl, dr: Make sure you load a texture just once (newImage) and then use the loaded texture everywhere you need it again.
Also, shouldnt this..
be self.speed = 500? So each player has it's own individual speed?
Code: Select all
playerImg = love.graphics.newImage("assets/player.png")
Try adding to your main love.load()
Code: Select all
function love.load()
player = Player(0,0)
playerImg = love.graphics.newImage("assets/player.png")
end
Tl, dr: Make sure you load a texture just once (newImage) and then use the loaded texture everywhere you need it again.
Also, shouldnt this..
Code: Select all
function Player:new(x, y)
speed = 500
end
https://github.com/Sulunia
Who is online
Users browsing this forum: No registered users and 4 guests