I’ve been developing a plane game similar to Cube Runner and I want to use Corona’s particleSystem functions to create blocks that “explode” upon contact with the plane. I create physics objects based on square display objects, but once they touch the plane, I want a particle group of the same size and color to be created.
However, I have been unable to successfully call the particleSystem:createGroup() function in my hit function! The strangest part is that I can call the createGroup() function outside the hit function, and with a timer. However, once I try to use createGroup() in my hit function, even if I call the same function that the timer calls, nothing happens.
Please help me figure this out. Is there something wrong with my hit() function? Am I calling Joe() or createGroup() incorrectly? Is there something wrong with Corona function itself?
Uncomment the red text to experiment with different ways of calling createGroup().
local physics = require( “physics” )
physics.start()
physics.setGravity(0,0)
display.setDefault(“background”, 1)
local colorwheel = {
[1] = 1,
[2] = 102/255,
[3] = 178/255
}
local particleSystem = physics.newParticleSystem{
filename = “particle.png”,
colorMixingStrength = 0,
radius = .5,
imageRadius = 5
}
local delete, over = false,false
local speed = 5
local loopy
local function Joe ()
print(“Before creation in Joe function”)
particleSystem:createGroup(
{
x = 50,
y = 50,
halfWidth = 10/2,
halfHeight = 10/2,
flags = { “water”, “colorMixing” },
color = {math.random(),math.random(),math.random()},
lifetime = 4.0
}
)
print(“After creation in Joe function”)
end
local function hit(event)
system.vibrate()
event.target.touch=true
local r
local flags
local groupFlags
local x, y, width, height
local params
–Joe()
print(“Before creation in hit function”)
particleSystem:createGroup(
{
x = event.target.x,
y = event.target.y,
halfWidth = event.target.width/2,
halfHeight = event.target.height/2,
flags = { “water”, “colorMixing” },
color = {event.target.r,event.target.b,event.target.g},
}
)
print(“After creation in hit function”)
event.target:removeSelf()
end
local function onTimer( self, event )
if (over == false and self.ob.touch == false and self.ob.y~=nil) then
if (delete == true) then
sceneGroup:remove( self.ob )
end
self.ob.y = self.ob.y + speed
self.ob.height, self.ob.width = self.ob.height + .2*speed, self.ob.width + .2*speed
self.ob.alpha = self.ob.alpha*1.1
if(self.randDir<0)then
self.ob.x = self.ob.x+self.randDir+(self.randDir*self.ob.x)/150
elseif (self.randDir>0) then
self.ob.x = self.ob.x+self.randDir+(self.randDir*(480-self.ob.x))/150
–elseif (self.randDir==0) then
self.ob:removeSelf()
end
if ( self.ob.y > 350 ) then
self.ob:removeSelf()
timer.cancel(event.source)
else
timer.performWithDelay( 50, self )
end
else
timer.cancel(event.source)
end
end
local function doLoopy()
while (delete == false and over == false) do
local loopy = {
ob = display.newRect(display.contentWidth/2, 20, 20, 20),
i = 0,
–randDir = (math.random(100)*2-100)/12,
randDir = 0,
timer = onTimer
}
local r,b,g = 0,0,0
while(r==b and r==g and g==b do
r = colorwheel[math.random(1,3)]
b = colorwheel[math.random(1,3)]
g = colorwheel[math.random(1,3)]
end
loopy.ob.r = r
loopy.ob.b = b
loopy.ob.g = g
loopy.ob:setFillColor(loopy.ob.r,loopy.ob.b,loopy.ob.g)
loopy.ob.strokeWidth = 2
loopy.ob:setStrokeColor( 0,0,0 )
loopy.ob.alpha = .2
physics.addBody( loopy.ob,“kinematic”,{ density=1.4, friction=0.3, bounce=1}
)
loopy.ob:addEventListener (“collision”, hit)
loopy.ob.touch=false
timer.performWithDelay( 1, loopy )
return loopy
end
end
local vertices = { 40,0, 0,-50, -40,0, 0,-20, }
plane = display.newPolygon( display.contentWidth/2, 270, vertices )
plane:setStrokeColor( 0,0,0 )
plane.strokeWidth = 2
physics.addBody( plane, “dynamic”, { density=1.4, friction=0.3, bounce=1} )
timer.performWithDelay( 1000, doLoopy, 20 )
–timer.performWithDelay( 250, Joe, 20 )
–Joe()