Particle = Core.class(Sprite) particlesArray = {'woodPiece.png','woodPiece2.png','woodPiece3.png'} function Particle:init(x, y, pressed, parent, type) local drop if (type == "wood") then --If we recive wood as a parameter for type, then we create a Wood particle. local bitmap = math.random(1,3) drop = bitmapHandler:getBitmap(particlesArray[bitmap]) local woodscale = math.random(1,8)/10 local rotation = math.random(0,360) drop:setRotation(rotation) drop:setScale(1+woodscale) --If we recive water as a parameter for type, then we create a Water particle. else if (type == "water") then drop = bitmapHandler:getBitmap('waterDrop2.png') --If we recive finish as a parameter for type, then we create an Star particle that indicates the finish. else if (type =="finish") then drop = bitmapHandler:getBitmap('particleSprite.png') drop:setScale(0.5+(math.random(1,8)/10)) drop:setRotation(math.random(0,360)) local r=255 local g=math.random(220,255)/255 local b=math.random(0,140)/255 drop:setColorTransform(r,g,b) --If we recieve erase as a parameter for type, then we create a particle for erasing the hole. else if (type == "erase") then drop = bitmapHandler:getBitmap('particleSprite.png') drop:setScale(0.5+(math.random(1,8)/10)) drop:setRotation(math.random(0,360)) else if (type == "magic") then drop = bitmapHandler:getBitmap('waterDrop.png') local r=math.random(100,250)/255 local g=math.random(100,200)/255 local b=math.random(100,200)/255 drop:setColorTransform(r,g,b) end end end end end --We add the particle to the sprite. self:addChild(drop) --This is in order to make the particles appear in different places, so they don't look one over another. local separationx = math.random(-15,15) self:setX(x + separationx) local separationy = math.random(-5,5) self:setY(y + separationy) ------------ --We set the scale of the particle. self.scale = math.random(8,10)/10 if (type == "wood") then self:setScale(1+self.scale) else self:setScale(self.scale) end self.degree = math.random(1,359) self.parent = parent self.pressed = pressed self.type = type if (pressed == false) then self.speed = math.random(150,300) self.life = math.random(100,150) else self.speed = math.random(800,1000) self.life = math.random(350,500) end self.xspeed = math.cos(self.degree)*self.speed self.yspeed = math.sin(self.degree)*self.speed self.xmax = math.abs(math.cos(self.degree)*self.life) self.ymax = math.abs(math.sin(self.degree)*self.life) self.totalx = 0 self.totaly = 0 if (self.pressed) then self.size = 3 --The size of the drop when pressed else self.size = 10 -- The size of the drop when not pressed end if (self.type == "finish") then self.size = 3 end end function Particle:update(ts) if (self.type == "finish") then ts = 1 end if (self.type == "wood") then ts = ts*3 end local dx = (self.xspeed/60)*ts local dy = (self.yspeed/60)*ts self:setX(self:getX() + dx) self:setY(self:getY() + dy) self.totalx = self.totalx + math.abs(dx) self.totaly = self.totaly + math.abs(dy) ---Doing a math formula to simulate the water from being from small to big to small again, depending it's life cycle. if (self.xmax > self.ymax) then actual = (self.totalx/self.xmax)*self.size else actual = (self.totaly/self.ymax)*self.size end local scaleChange = (-1*(1/45)*(math.pow(actual,2))) + ((10/45)*actual) self:setScale(self.scale + (scaleChange*2)) if (self.type == "finish") then self:setAlpha(1-scaleChange) end ----------------------------------------- end --This function tells if the particle needs to be killed. function Particle:deleteOld() if (self.totalx > self.xmax and self.totaly > self.ymax) then answer = true else answer = false end return answer end