I have a small scenario where I was trying an image dropping from top, and when I touch it removes itself and a smoke effect is created. Then I tried to to drop many object, basically 4 objects which drop based on the random funciton. But now when I touch I get an error.
Initially I removed the removeSelf function and tried to get only the smoke effect on touching an object, but the smoke function was taking single object position to show the smoke effect. But now there are so many objects, the same variable j has many objects. How do I introduce the array/table concept and how do I use a single smoke function for so many objects. I also want to have multi touch. [import]uid: 183447 topic_id: 33131 reply_id: 333131[/import]
You really need to provide your code here. Without it the best you can hope for is that someone will give you the code to do it, but that doesn’t fix your code or tell you why it’s not behaving the right way.
My best stab at answering your quandry: If you are seeing errors when adding multiple objects and trying to use a single touch listener to remove them, but it works with just one, then you are probably setting a reference which the listener function uses to access the display object to nil, meaning you lose all references to any objects which could bet touched.
Off the top of my head, no guarantees:
main.lua
[lua]require(“physics”)
physics.start()
function touch(e)
if (e.phase == “ended”) then
e.target:removeSelf()
return true
end
return false
end
for i=1, 4 do
local obj = display.newImage(“image.png”,math.random(0,display.width),0)
physics.addBody(obj)
obj:addEventListener(“touch”,touch)
end[/lua] [import]uid: 8271 topic_id: 33131 reply_id: 131554[/import]
You really need to provide your code here. Without it the best you can hope for is that someone will give you the code to do it, but that doesn’t fix your code or tell you why it’s not behaving the right way.
My best stab at answering your quandry: If you are seeing errors when adding multiple objects and trying to use a single touch listener to remove them, but it works with just one, then you are probably setting a reference which the listener function uses to access the display object to nil, meaning you lose all references to any objects which could bet touched.
Off the top of my head, no guarantees:
main.lua
[lua]require(“physics”)
physics.start()
function touch(e)
if (e.phase == “ended”) then
e.target:removeSelf()
return true
end
return false
end
for i=1, 4 do
local obj = display.newImage(“image.png”,math.random(0,display.width),0)
physics.addBody(obj)
obj:addEventListener(“touch”,touch)
end[/lua] [import]uid: 8271 topic_id: 33131 reply_id: 131554[/import]
I also don’t understand why that code would work with only one image. You do not appear to have declared the ‘j’ variable when you try to call ‘removeSelf’ on it.
I’ll take a look later
[import]uid: 8271 topic_id: 33131 reply_id: 131612[/import]
Here is my code,
– smoke function
[lua] local physics = require(“physics”)
physics.start()
–physics.setDrawMode( “hybrid” )
physics.setGravity(0,1)
local gameUI = require(“gameUI”)
–Emitter
local emitter = require(“emitter”) --I called the emitter function over here
–Burst variables are described
local mainGroup = display.newGroup()
local emit1
local PARTICLES_PER_BURST = 100
local i
local mem = display.newText(“Memory :”…tostring(collectgarbage(“count”)), 0, 0, nil, 20)
local j = {} --for the image table
local countb = 0
local rand
local dropimages
–I pass values over here for the emitter function
local function init()
emit1 = emitter.createEmitter(50, 4, 1500, 0.2, 0)
emitter.setColor(emit1,235,200,0)
end
–[[This is the burst function which will use the emitter function and the current image values to display the burst function at the specified co ordinates(the co ordinates are specified by a and b below.–]]
local function burst(event)
if(event.phase == “ended” ) then
a = j.x
b = j.y
j:removeSelf()
for i = 1,PARTICLES_PER_BURST do
emitter.emit(emit1, mainGroup, a, b)
end
mem.text = “Memory :”…tostring(collectgarbage(“count”))
end
end
Runtime:addEventListener(“collision”, burst)–[[I am calling collision between main object and a kinematic object–]]
function main()
–background = display.newImage(“background.png”)
images()
kimage() --[[I havent pasted code for this object. It is just a kinematic object which will come into play when I am looking for collision between images and this kimage–]]
init()
end
function images()
for countb = 1, 1 do
rand = math.random( 100 )
if (rand < 25) then
j = display.newImage(“image1.png”);
j.x = 200 + math.random( 160 )
j.y = -50
physics.addBody( j, { friction=0.5, bounce=0.3 } )
elseif (rand < 50) then
j = display.newImage(“image2.png”);
j.x = 200 + math.random( 160 )
j.y = -50
physics.addBody( j, { friction=0.5, bounce=0.3} )
elseif (rand < 75) then
j = display.newImage(“image3.png”);
j.x = 200 + math.random( 160 )
j.y = -50
physics.addBody( j, { friction=0.5, bounce=0.3 } )
else
j = display.newImage(“image4.png”);
j.x = 200 + math.random( 100 )
j.y = -50
physics.addBody( j, { friction=0.5, bounce=0.3 } )
end
end
local function dragBody( event )
return gameUI.dragBody( event )
end
j:addEventListener(“touch”, dragBody)
end
dropimages = timer.performWithDelay( 1000, images, 10)[/lua]
Here when I use this code I get an error for remove self which says "attempt to call method removeSelf ". In the above dropimages variable if the call the images function once, that is instead of 10 I write 1, it is working fine. But when it is more than 1, I am getting the error. [import]uid: 183447 topic_id: 33131 reply_id: 131601[/import]
I have added the other necessary part to the above code with explanation, please take a look at it. I tried to make it more readable by editing the spacing, but it was not working, please excuse me for that. [import]uid: 183447 topic_id: 33131 reply_id: 131703[/import]
This is going to be difficult to debug without the “emitter.lua” file. [import]uid: 8271 topic_id: 33131 reply_id: 131717[/import]
The problem is with removing the object as well, nothing more specific towards emitter.lua. Anyways here is the code for emitter.lua:
[lua]module(…, package.seeall)
math.randomseed(os.time())
function createEmitter(radiusRange,thickness, particleDuration, initAlpha, endAlpha)
local customEmitter = {}
customEmitter.radiusRange = radiusRange
customEmitter.thickness = thickness
customEmitter.particleDuration = particleDuration
customEmitter.initAlpha = initAlpha
customEmitter.endAlpha = endAlpha
customEmitter.colorR = nil
customEmitter.colorG = nil
customEmitter.colorB = nil
return customEmitter
end
function setColor(customEmitter, red, green, blue)
if(red~=nil) then
customEmitter.colorR = red
end
if(green~=nil) then
customEmitter.colorG = green
end
if(blue~=nil) then
customEmitter.colorB = blue
end
end
function emit(customEmitter, group, ex, ey)
local rx = math.random(ex - (customEmitter.radiusRange/15), ex + (customEmitter.radiusRange/15))
local ry = math.random(ey - (customEmitter.radiusRange/15), ey + (customEmitter.radiusRange/15))
local dx = math.random( ex - customEmitter.radiusRange, ex + customEmitter.radiusRange)
local ranSign = math.random(2)
if( ranSign == 2) then ranSign = -1 end
local dy = (ranSign*math.sqrt((customEmitter.radiusRange*customEmitter.radiusRange) - ((dx - ex)*(dx - ex)))) + ey
local particle = display.newRect( group, rx - customEmitter.thickness/2, ry - customEmitter.thickness/2, customEmitter.thickness, customEmitter.thickness)
particle.alpha = customEmitter.initAlpha
if( customEmitter.colorR ~= nil) then
if(customEmitter.colorR == -1) then
particle:setFillColor( math.random(0,255), math.random(0,255), math.random(0,255))
else
particle:setFillColor( customEmitter.colorR, customEmitter.colorG, customEmitter.colorB)
end
end
local transC = transition.to(particle, {time = customEmitter.particleDuration, x = dx, y = dy, alpha = customEmitter.endAlpha, transition = easing.outQuad, onComplete = function(event)
particle:removeSelf()
customEmitter = nil
group = nil
ex = nil
ey = nil
rx = nil
ry = nil
dx = nil
dy = nil
ranSign = nil
transC = nil
end})
end[/lua] [import]uid: 183447 topic_id: 33131 reply_id: 131724[/import]
I’ve copied the files you’ve provided and used by own images for the images1…4.png. Here is the error I see:
[lua]Copyright © 2009-2012 C o r o n a L a b s I n c .
Version: 2.0.0
Build: 2012.971
Runtime error
…\dropbox\public\help\removeselfparticles\emitter.lua:42: attempt to index local ‘customEmitter’ (a nil valu
e)
stack traceback:
[C]: ?
…\dropbox\public\help\removeselfparticles\emitter.lua:42: in function ‘emit’
…nts\dropbox\public[/lua] [import]uid: 8271 topic_id: 33131 reply_id: 131726[/import]
I have another kinematic object as I have said, and I am trying to remove the image when I drag the image and make to collide with the kinematic object. That is the image should be removed on collision.
I am getting the above error but also another error which says:
attempt to call ‘removeSelf’ .
If you want to repro the second error exactly call the images function only once in the timerPerformWithDelay function [import]uid: 183447 topic_id: 33131 reply_id: 131727[/import]
Resolve the first error, then you may find the second error disappears as it may be caused, in part, by the first error. [import]uid: 8271 topic_id: 33131 reply_id: 131734[/import]
Can you suggest me how to approach the resolution of the first error [import]uid: 183447 topic_id: 33131 reply_id: 131737[/import]
Just putting some simple print statements into the top of each function showed me that your ‘main()’ and ‘init()’ functions never get called, so the ‘emit1’ variable never gets set.
Just FYI - lua is not like C… main() will not get called as the starting point… main.lua gets called as the starting point. If you want a function called main() to be called you have to do it yourself. [import]uid: 8271 topic_id: 33131 reply_id: 131741[/import]
I also don’t understand why that code would work with only one image. You do not appear to have declared the ‘j’ variable when you try to call ‘removeSelf’ on it.
I’ll take a look later
[import]uid: 8271 topic_id: 33131 reply_id: 131612[/import]
Here is my code,
– smoke function
[lua] local physics = require(“physics”)
physics.start()
–physics.setDrawMode( “hybrid” )
physics.setGravity(0,1)
local gameUI = require(“gameUI”)
–Emitter
local emitter = require(“emitter”) --I called the emitter function over here
–Burst variables are described
local mainGroup = display.newGroup()
local emit1
local PARTICLES_PER_BURST = 100
local i
local mem = display.newText(“Memory :”…tostring(collectgarbage(“count”)), 0, 0, nil, 20)
local j = {} --for the image table
local countb = 0
local rand
local dropimages
–I pass values over here for the emitter function
local function init()
emit1 = emitter.createEmitter(50, 4, 1500, 0.2, 0)
emitter.setColor(emit1,235,200,0)
end
–[[This is the burst function which will use the emitter function and the current image values to display the burst function at the specified co ordinates(the co ordinates are specified by a and b below.–]]
local function burst(event)
if(event.phase == “ended” ) then
a = j.x
b = j.y
j:removeSelf()
for i = 1,PARTICLES_PER_BURST do
emitter.emit(emit1, mainGroup, a, b)
end
mem.text = “Memory :”…tostring(collectgarbage(“count”))
end
end
Runtime:addEventListener(“collision”, burst)–[[I am calling collision between main object and a kinematic object–]]
function main()
–background = display.newImage(“background.png”)
images()
kimage() --[[I havent pasted code for this object. It is just a kinematic object which will come into play when I am looking for collision between images and this kimage–]]
init()
end
function images()
for countb = 1, 1 do
rand = math.random( 100 )
if (rand < 25) then
j = display.newImage(“image1.png”);
j.x = 200 + math.random( 160 )
j.y = -50
physics.addBody( j, { friction=0.5, bounce=0.3 } )
elseif (rand < 50) then
j = display.newImage(“image2.png”);
j.x = 200 + math.random( 160 )
j.y = -50
physics.addBody( j, { friction=0.5, bounce=0.3} )
elseif (rand < 75) then
j = display.newImage(“image3.png”);
j.x = 200 + math.random( 160 )
j.y = -50
physics.addBody( j, { friction=0.5, bounce=0.3 } )
else
j = display.newImage(“image4.png”);
j.x = 200 + math.random( 100 )
j.y = -50
physics.addBody( j, { friction=0.5, bounce=0.3 } )
end
end
local function dragBody( event )
return gameUI.dragBody( event )
end
j:addEventListener(“touch”, dragBody)
end
dropimages = timer.performWithDelay( 1000, images, 10)[/lua]
Here when I use this code I get an error for remove self which says "attempt to call method removeSelf ". In the above dropimages variable if the call the images function once, that is instead of 10 I write 1, it is working fine. But when it is more than 1, I am getting the error. [import]uid: 183447 topic_id: 33131 reply_id: 131601[/import]
I have added the other necessary part to the above code with explanation, please take a look at it. I tried to make it more readable by editing the spacing, but it was not working, please excuse me for that. [import]uid: 183447 topic_id: 33131 reply_id: 131703[/import]
This is going to be difficult to debug without the “emitter.lua” file. [import]uid: 8271 topic_id: 33131 reply_id: 131717[/import]
The problem is with removing the object as well, nothing more specific towards emitter.lua. Anyways here is the code for emitter.lua:
[lua]module(…, package.seeall)
math.randomseed(os.time())
function createEmitter(radiusRange,thickness, particleDuration, initAlpha, endAlpha)
local customEmitter = {}
customEmitter.radiusRange = radiusRange
customEmitter.thickness = thickness
customEmitter.particleDuration = particleDuration
customEmitter.initAlpha = initAlpha
customEmitter.endAlpha = endAlpha
customEmitter.colorR = nil
customEmitter.colorG = nil
customEmitter.colorB = nil
return customEmitter
end
function setColor(customEmitter, red, green, blue)
if(red~=nil) then
customEmitter.colorR = red
end
if(green~=nil) then
customEmitter.colorG = green
end
if(blue~=nil) then
customEmitter.colorB = blue
end
end
function emit(customEmitter, group, ex, ey)
local rx = math.random(ex - (customEmitter.radiusRange/15), ex + (customEmitter.radiusRange/15))
local ry = math.random(ey - (customEmitter.radiusRange/15), ey + (customEmitter.radiusRange/15))
local dx = math.random( ex - customEmitter.radiusRange, ex + customEmitter.radiusRange)
local ranSign = math.random(2)
if( ranSign == 2) then ranSign = -1 end
local dy = (ranSign*math.sqrt((customEmitter.radiusRange*customEmitter.radiusRange) - ((dx - ex)*(dx - ex)))) + ey
local particle = display.newRect( group, rx - customEmitter.thickness/2, ry - customEmitter.thickness/2, customEmitter.thickness, customEmitter.thickness)
particle.alpha = customEmitter.initAlpha
if( customEmitter.colorR ~= nil) then
if(customEmitter.colorR == -1) then
particle:setFillColor( math.random(0,255), math.random(0,255), math.random(0,255))
else
particle:setFillColor( customEmitter.colorR, customEmitter.colorG, customEmitter.colorB)
end
end
local transC = transition.to(particle, {time = customEmitter.particleDuration, x = dx, y = dy, alpha = customEmitter.endAlpha, transition = easing.outQuad, onComplete = function(event)
particle:removeSelf()
customEmitter = nil
group = nil
ex = nil
ey = nil
rx = nil
ry = nil
dx = nil
dy = nil
ranSign = nil
transC = nil
end})
end[/lua] [import]uid: 183447 topic_id: 33131 reply_id: 131724[/import]
I’ve copied the files you’ve provided and used by own images for the images1…4.png. Here is the error I see:
[lua]Copyright © 2009-2012 C o r o n a L a b s I n c .
Version: 2.0.0
Build: 2012.971
Runtime error
…\dropbox\public\help\removeselfparticles\emitter.lua:42: attempt to index local ‘customEmitter’ (a nil valu
e)
stack traceback:
[C]: ?
…\dropbox\public\help\removeselfparticles\emitter.lua:42: in function ‘emit’
…nts\dropbox\public[/lua] [import]uid: 8271 topic_id: 33131 reply_id: 131726[/import]
I have another kinematic object as I have said, and I am trying to remove the image when I drag the image and make to collide with the kinematic object. That is the image should be removed on collision.
I am getting the above error but also another error which says:
attempt to call ‘removeSelf’ .
If you want to repro the second error exactly call the images function only once in the timerPerformWithDelay function [import]uid: 183447 topic_id: 33131 reply_id: 131727[/import]