Watch the stars fly!
Posted: Sun Nov 21, 2010 9:48 pm
A little somethin' I decided to whip up in a couple hours.
Hi! That remember more snow fall than stars but is always a nice effect. Look, shouldn't be a Mac issue but it seems that from time to time things stop a bit. Let's wait for other reports to see if it's only happening here. Also stars animation/trail isn't quite smooth. Probably something could be improved. Keep the good work in this demo!Kingdaro wrote:A little somethin' I decided to whip up in a couple hours.
Thanks for the feedback.arquivista wrote:Hi! That remember more snow fall than stars but is always a nice effect. Look, shouldn't be a Mac issue but it seems that from time to time things stop a bit. Let's wait for other reports to see if it's only happening here. Also stars animation/trail isn't quite smooth. Probably something could be improved. Keep the good work in this demo!Kingdaro wrote:A little somethin' I decided to whip up in a couple hours.
Code: Select all
stars = {}
time = 1
function love.update(dt)
if math.random(1,2) == 2 then
local star = {}
star.x = math.random(0,640)
star.y = -100
star.size = math.random(0.4,2.5)
table.insert(stars,star)
end
for i=1, #stars do
if stars[i] ~= nil then
stars[i].y = stars[i].y+(150*dt*stars[i].size)
end
end
--[[
for i=1, #stars do
if stars[i].y >= 480 then --#main.lua.24 attempt to index field ? (a nil value)
table.remove(stars,i)
end
end
--]]
if time >= 1 then
love.graphics.setCaption("Watch the stars fly! ("..love.timer.getFPS().." FPS)")
time = 0
else
time = time+dt
end
end
Code: Select all
local stars, center
function love.load()
center = {
x = love.graphics.getWidth()/2,
y = love.graphics.getHeight()/2,
}
stars = love.graphics.newParticleSystem( love.graphics.newImage('fire.png'), 300 )
stars:setPosition(center.x, center.y)
stars:setBufferSize( 2000 )
stars:setEmissionRate( 300 )
stars:setLifetime( -1 )
stars:setParticleLife( 6 )
stars:setColor( 55, 55, 55, 0, 255, 255, 255, 255 )
stars:setSize( 0.16, 0.1, 1 )
stars:setSpeed( 30, 80 )
stars:setSpread( math.rad(360) )
stars:setRadialAcceleration( 20 )
end
function love.keypressed(key)
if key == 'escape' then os.exit() end
end
function love.update(dt)
local x, y = love.mouse.getPosition()
stars:setOffset((x-center.x) * -5, (y-center.y) * -5)
stars:update(dt)
end
function love.draw()
love.graphics.draw(stars, 0, 0)
end
Code: Select all
love.graphics.draw(love.graphics.newImage("star.png"),stars[i].x,stars[i].y,0,stars[i].size/4)
Code: Select all
math.randomseed( os.time() )
function love.load()
win = love.graphics.setMode(640,480)
stars = {}
time = 1
stars.image = love.graphics.newImage("star.png")
f = love.graphics.newFont(12)
love.graphics.setFont(f)
end
function love.update(dt)
if math.random(1,2) == 2 then
local star = {}
star.x = math.random(0,640)
star.y = -128
star.size = math.random(0.5,2)
table.insert(stars,star)
end
for i,star in ipairs(stars) do
if star.y < 480 then
star.y = star.y+(150*dt*star.size)
else
table.remove(stars,i)
end
end
if time >= 1 then
love.graphics.setCaption("Watch the stars fly! ("..love.timer.getFPS().." FPS)")
time = 0
else
time = time+dt
end
end
function love.draw()
for _,star in ipairs(stars) do
love.graphics.draw(stars.image,star.x,star.y,0,star.size/4)
end
end
Code: Select all
if math.random(1,2) == 2 then
local star = {}
star.x = math.random(0,640)
star.y = -40
star.size = math.random(0.5,2)
table.insert(stars,star)
end
Code: Select all
if math.random(1,2) == 2 and time>=1 then
I fully support this nonononono.TechnoCat wrote:nononononoCode: Select all
love.graphics.draw(love.graphics.newImage("star.png"),stars[i].x,stars[i].y,0,stars[i].size/4)
Yeaarquivista wrote:Hi! That remember more snow fall than stars but is always a nice effect.Kingdaro wrote:A little somethin' I decided to whip up in a couple hours.
It is happening here too! You are not the only one...Look, shouldn't be a Mac issue but it seems that from time to time things stop a bit. Let's wait for other reports to see if it's only happening here.
Iterating arrays that way can be faster (eliminating iterator function call overhead), but only if you index the table you're iterating no more than ~1-2 times in the loop body, and it only works for arrays. For instance:Kingdaro wrote:I'm much more comfortable with using for i=1, #table [..] I never really bothered to get the concept of pairs/ipairs
Code: Select all
for i=1, #stars do
stars[i].y = stars[i].y+(speed*dt*(stars[i].size/2))
end
for i,star in ipairs(stars) do
star.y = star.y+(speed*dt*(star.size/2))
end
The line "star.x = math.random(-640,1280)" is generating ~65% of your stars off-screen where they will never be seen.Kingdaro wrote:here's the demo in all it's beautified and updated glory. (See first post.)