Hey guys, could someone teach me about particles?
I need it to finish my game
To do this you would first have to create a particle system, which you would do with a list
init = function()
particles = []
end
In that list you will place the particles you want; in update you will have to put a system that deletes the particles that have already reached the end of their lifespan
update = function()
// A temporary list is created
local temp = []
for e in particles
/*
If "e.update()" is "true", then the particle
is still alive
*/
if e.update() then
// ...then it is placed on the temporary list
temp.push(e)
end
end
/*
Then the original list is replaced by the
temporary one, getting rid of all the particles
that are nolonger alive
*/
particles = temp
end
I recommend not using list.removeElement(particle) as it can cause certain visual problems
Then in draw you must make it draw all the particles, checking that it draws each one
draw = function()
screen.clear(0)
for e in particles
e.draw()
end
end
Now that you have your particle system, you will need to create the particle, for example:
Explosion = object
// This defines the initial properties of the particle
constructor = function(x,y)
this.x = x
this.y = y
this.vx = random.next()*4-2
this.vy = random.next()*4-2
/*
It is important to have a timer to be able
to trigger events or indicate when the particle is removed
*/
this.time = 0
end
/*
This serves to indicate the actions of the
particle and when it should be deleted or not
*/
update = function()
// If the timer reaches 14 or more, the particle will be eliminated
if time < 14 then
x += vx
y += vy
// You increase the timer
time += 1
return true // e.update() == true
else
// This indicates when the particle will be removed
return false // e.update() == false
end
end
/*
This indicates what the particle will display
which is just an orange square
*/
draw = function()
screen.fillRect(x,y,5,5,"rgb(255,179,28)")
end
end
Now you must place the particle in the particle list; to do this you must use particles.push(new Explosion(10,10))
Or you can also do the following:
update = function()
if mouse.pressed then
particles.push(new Explosion(mouse.x,mouse.y))
end
...
And that's it! With this, you can have several different particles without having to create multiple lists or do anything complicated with loops. You just need to create a new object with a different name, its constructor, update and draw functions, and push it to the particles list (And of course, specify that its update returns true or false, so you can know when the particle is removed)
Although the function above only adds one orange square, you can also create a function that creates multiple orange squares, thus simulating a better explosion
ok thanks you