Issue with spawning display.circle with physic.addBody [RADIUS]

Hey Guys, 

I have a problem with spwaning display.newCirlce…  physics.addBody - 
Structure of my script. 

  1. Display all objects with Physics.addbody - radius. 
    2. Local function onCollision(event) - for collision detection 
    3. Runtime:addEventListener(“collision”, onCollision)

    • 3 objects are there = physics.addBody = true
      1 object = egg1 (rect)
      2 object = egg2 (Circle)
      3 object = egg3 (Circle)

X object = Player 

X Object must hit all 3 objects with one touch event to win. If X object collides with 1-2 objects they disappear. 
if not win then automatically I let respwan the Hit objects egg1,egg2 and egg3, becuase the Objective is to Hit all Elements with 1 touch event. 

While Doing this respawn all objects everything Works fine with display.newRects.  BUT the display.newCirlce objetcs somehow they have physics.addBody and they Collide with (X object = Player ), HOWEVER the detection in Runtime:addEventListener(“collision”, onCollision) the [#2. Local function onCollision(event) - for collision detection] does not work anymore…

I do not why.  Could you help me with this? 
Short code here… 

WITH function _reCreate( ) I call to respawn egg1,egg2,egg3 … display.Rects are working. Display.new Circle unfortunately not …  Please help. Thanks in advance … 

Sry for typos, if the written down does not convey clear the problem please let me know. 

Best regs

 function createEgg3() schlossZetra3 = false egg3 = display.newCircle(\_CX+50, \_CY-100, 40 ) egg3.alpha=0.1 egg3.id = "egg3" function egg3Physics( ) if ( event.phase == "ended" ) then physics.addBody( egg3, "static", { density=1.0, friction=0.3, bounce=0.2,radius=35, } ) egg3.isSensor = true -- transMenu3 = transition.to( egg3, { xScale=1, yScale=1, time=300, transition=easing.outBack} ) end end transMegg3 = transition.to( egg3, { alpha=1, xScale=1, yScale=1, time=750, transition=easing.outElastic,onComplete=egg3Physics} ) -- print("h31\_@") end createEgg1() ... After some scripts ... local function onCollision(event) local function removeOnBirdy3(obj1, obj2) schlossZetra3 = true function disappearBird3() if ( event.phase == "began" ) then -- physics.removeBody( egg3 ) display.remove(egg3) -- transition.scaleTo( egg3, { time=1, alpha=0, xScale=4.0, yScale=4.0} ) end end timer.performWithDelay( 1, disappearBird3 ) end local function \_reCreate( event ) display.remove(proj) if (callWinMia == false) then if ( schlossZetra1 == true) then schlossZetra1 = false createEgg1() end if ( schlossZetra2 == true ) then schlossZetra2 = false createEgg2() end if ( schlossZetra3 == true ) then schlossZetra3 = false createEgg3() end -- Runtime:addEventListener( "touch", screenTouch ) end return true end if (event.object1.id == "egg1" and event.object2.id == "proj") then callWin () removeOnBirdy() print("1xI ") elseif (event.object1.id == "egg2" and event.object2.id == "proj") then callWin () removeOnBirdy2() print("2xI ") elseif (event.object1.id == "egg3" and event.object2.id == "proj") then callWin () schlossZetra3 = true display.remove(egg3) elseif (event.object1.id == "deadBorder" and event.object2.id == "proj") then \_reCreate() print("4xI \_reCreate") end return true end Runtime:addEventListener("collision", onCollision) end

Hi @hcalikbasi,

I’m not clear on the issues. Can you please clarify?

  1. Are the eggs not being rendered as circles?

  2. Are the eggs not triggering a collision event with each other?

  3. Something else?

Best regards,

Brent

HI @Brent

thanks for the quick response. No Problem and of course I can do. 

1.) The eggs are rendert as circle

2.) The eggs have physics  
3.) The eggs disappear if “X object = Player” touch/ Collides with these. 

But: “X object” must collide with all eggs ( ) to get to the next level.  
In our case we have for example 3 eggs. 

If 2 of them where touched by X object they disappear. The third one is still there
and X object did not touch it.  

X object will fall down without touched egg3 and will collide with Borders.

“X object disappears” and function checkwin() will be called.

​This function Checks if the eggs are displayed or just nil.
​Only if all objects are nill  checkwin() will call the win() function. 

If not all eggs are nil then checkwin() function detects which objects are nil 
and create them totally new. Furthermore It creates also the physic for the objects. 

Now here is the Problem. 

After everything is again created and objects as well as phyics are ready my function 
onCollision(event) does not detect anymore a collision. That Happens only to display.new circle …

The entire level is created is in that way:
 

 function createEgg3() schlossZetra3 = false egg3 = display.newCircle(\_CX+50, \_CY-100, 40 ) egg3.alpha=0.1 egg3.id = "egg3" function egg3Physics( ) if ( event.phase == "ended" ) then physics.addBody( egg3, "static", { density=1.0, friction=0.3, bounce=0.2,radius=35, } ) egg3.isSensor = true end end transMegg3 = transition.to( egg3, { alpha=1, xScale=1, yScale=1, time=750, transition=easing.outElastic,onComplete=egg3Physics} ) end createEgg1()

So I call createEgg1() to create the egg. 
I need to say that I have used global eggs, because when I use local in front the game removing and creating does not work with the collision event. 

X Object is also global. Because I call many of them and delete many of them. Also with the entire level. That how it Works basically. Is that a wrong way? 

I tried to use local but 

Can you help me with this? Did you unterstand now my issue? 
Is it because of local and global values? 

if not i can repost again and cut down the code to convey my issues. 

What I did not Understand is it Works with rects but with cirlces it does not … 

Thanks in advance 

 

 

 

 

Hi @hcalikbasi,

My first suggestion is that you simplify your code. There should be no need for multiple “create egg” functions like “createEgg1”, “createEgg2”, and “createEgg3”. This somewhat defeats the purpose of functions and their DRY (Don’t Repeat Yourself) power. See here for some tutorials that discuss this:

https://docs.coronalabs.com/tutorial/basics/functionArguments/index.html

https://docs.coronalabs.com/tutorial/basics/basicSpawning/index.html

Next, you are setting your eggs as “static” physical objects. If you study the following guide, you’ll see that static bodies do not register collisions with other static bodies:

https://docs.coronalabs.com/guide/physics/physicsBodies/index.html#body-type

So, you should make your eggs “dynamic” physics bodies, which is the default.

Hope this helps,

Brent

Thanks! I’ll update you here if I could manage it to do … sometime it Works and sometimes it dose not… need to clean my code

My solution is … that I will not remove the objects anymore… I found in an other blogpost that just unsing transition and placing it out of the screen is a better and smoth solution and that Works fine. I need to say Thank you Brent also very much, Because for me that is new to consider “writing smart code”. Learning that on my own and yes your absolutely right… Amazing thanks for sharing. I think your advice will imrpove my app development significantly. Great!  :) 

I’ll now use tables and arrays… try to avoid repating,…using Focus on DRY and try to avoid if else if possible … 

Maybe I can write again another issues I am currently facing. It better to ask then fight alone … 

 

local groupArray = display.newGroup() groupArray.id = "groupArray"; do local keyTable = { -- WHAT I TRIED for newCircle/ sceneGroup/ : Creating a group and give pyhsics at the end to the group did not work as well :/ and it changes its x y position.. I just let scengroup there egg1 = display.newCircle(sceneGroup,\_CX, \_CY-100, 25 ), -- display.newRect(sceneGroup, 100,300, 40, 40 ), z = function(x) -- WHAT I TRIED for timer/ physics.addBody/ : instead of "egg1" i worte "keyTable[z]" or "keyTable[egg1]", but that give me an runtime error timer.performWithDelay(2230, function() physics.addBody(egg1, "static", { bounce=0.4, friction=1.0,radius=20,filter=blueCollisionFilter } ); egg1.gravityScale = 0; egg1.isBodyActive = true; egg1.isVisible = true; print( "please run with physics... " ) end) end, egg2= display.newCircle(\_CX, \_CY, 5 ), banana="yellow", lime="green", grape="purple" } function saykeyTable(lettersArray) for ki,vp in ipairs(lettersArray) do local si = type(keyTable[vp]) == "function" and keyTable[vp]() or keyTable[vp] or "blah" print(si) end end end saykeyTable{'egg1','z'}

Generation with for the Object egg1 with pyhsics … did not find a solution … 

so I am using now this way… but i think its weacker and need more power… 

 

action = { [1] = function (x) egg1 = display.newCircle(sceneGroup, 200, 300, 25 ); egg1:setFillColor( 0.72, 0.9, 0.16, 0.78 ); egg1.id = "egg1"; schlossZetra1 = false; timer.performWithDelay(10, function() physics.addBody(egg1, "static", { bounce=0.4, friction=1.0,radius=20,filter=blueCollisionFilter } ); egg1.gravityScale = 0; egg1.isBodyActive = true; egg1.isVisible = true; end) end, } action[1](params)

Or what do you think? do you have a better solution? 
Am I on the right way … Thanks for any commencts and improvementsugesstions … 

I think I will Repatriierung also that Part of code Overall times … Do not know how to Write it in a better way. 
If can give to that issue a solution I owuld be very glad… 

Thanks 

Hi @hcalikbasi,

I’m not clear on the issues. Can you please clarify?

  1. Are the eggs not being rendered as circles?

  2. Are the eggs not triggering a collision event with each other?

  3. Something else?

Best regards,

Brent

HI @Brent

thanks for the quick response. No Problem and of course I can do. 

1.) The eggs are rendert as circle

2.) The eggs have physics  
3.) The eggs disappear if “X object = Player” touch/ Collides with these. 

But: “X object” must collide with all eggs ( ) to get to the next level.  
In our case we have for example 3 eggs. 

If 2 of them where touched by X object they disappear. The third one is still there
and X object did not touch it.  

X object will fall down without touched egg3 and will collide with Borders.

“X object disappears” and function checkwin() will be called.

​This function Checks if the eggs are displayed or just nil.
​Only if all objects are nill  checkwin() will call the win() function. 

If not all eggs are nil then checkwin() function detects which objects are nil 
and create them totally new. Furthermore It creates also the physic for the objects. 

Now here is the Problem. 

After everything is again created and objects as well as phyics are ready my function 
onCollision(event) does not detect anymore a collision. That Happens only to display.new circle …

The entire level is created is in that way:
 

 function createEgg3() schlossZetra3 = false egg3 = display.newCircle(\_CX+50, \_CY-100, 40 ) egg3.alpha=0.1 egg3.id = "egg3" function egg3Physics( ) if ( event.phase == "ended" ) then physics.addBody( egg3, "static", { density=1.0, friction=0.3, bounce=0.2,radius=35, } ) egg3.isSensor = true end end transMegg3 = transition.to( egg3, { alpha=1, xScale=1, yScale=1, time=750, transition=easing.outElastic,onComplete=egg3Physics} ) end createEgg1()

So I call createEgg1() to create the egg. 
I need to say that I have used global eggs, because when I use local in front the game removing and creating does not work with the collision event. 

X Object is also global. Because I call many of them and delete many of them. Also with the entire level. That how it Works basically. Is that a wrong way? 

I tried to use local but 

Can you help me with this? Did you unterstand now my issue? 
Is it because of local and global values? 

if not i can repost again and cut down the code to convey my issues. 

What I did not Understand is it Works with rects but with cirlces it does not … 

Thanks in advance 

 

 

 

 

Hi @hcalikbasi,

My first suggestion is that you simplify your code. There should be no need for multiple “create egg” functions like “createEgg1”, “createEgg2”, and “createEgg3”. This somewhat defeats the purpose of functions and their DRY (Don’t Repeat Yourself) power. See here for some tutorials that discuss this:

https://docs.coronalabs.com/tutorial/basics/functionArguments/index.html

https://docs.coronalabs.com/tutorial/basics/basicSpawning/index.html

Next, you are setting your eggs as “static” physical objects. If you study the following guide, you’ll see that static bodies do not register collisions with other static bodies:

https://docs.coronalabs.com/guide/physics/physicsBodies/index.html#body-type

So, you should make your eggs “dynamic” physics bodies, which is the default.

Hope this helps,

Brent

Thanks! I’ll update you here if I could manage it to do … sometime it Works and sometimes it dose not… need to clean my code

My solution is … that I will not remove the objects anymore… I found in an other blogpost that just unsing transition and placing it out of the screen is a better and smoth solution and that Works fine. I need to say Thank you Brent also very much, Because for me that is new to consider “writing smart code”. Learning that on my own and yes your absolutely right… Amazing thanks for sharing. I think your advice will imrpove my app development significantly. Great!  :) 

I’ll now use tables and arrays… try to avoid repating,…using Focus on DRY and try to avoid if else if possible … 

Maybe I can write again another issues I am currently facing. It better to ask then fight alone … 

 

local groupArray = display.newGroup() groupArray.id = "groupArray"; do local keyTable = { -- WHAT I TRIED for newCircle/ sceneGroup/ : Creating a group and give pyhsics at the end to the group did not work as well :/ and it changes its x y position.. I just let scengroup there egg1 = display.newCircle(sceneGroup,\_CX, \_CY-100, 25 ), -- display.newRect(sceneGroup, 100,300, 40, 40 ), z = function(x) -- WHAT I TRIED for timer/ physics.addBody/ : instead of "egg1" i worte "keyTable[z]" or "keyTable[egg1]", but that give me an runtime error timer.performWithDelay(2230, function() physics.addBody(egg1, "static", { bounce=0.4, friction=1.0,radius=20,filter=blueCollisionFilter } ); egg1.gravityScale = 0; egg1.isBodyActive = true; egg1.isVisible = true; print( "please run with physics... " ) end) end, egg2= display.newCircle(\_CX, \_CY, 5 ), banana="yellow", lime="green", grape="purple" } function saykeyTable(lettersArray) for ki,vp in ipairs(lettersArray) do local si = type(keyTable[vp]) == "function" and keyTable[vp]() or keyTable[vp] or "blah" print(si) end end end saykeyTable{'egg1','z'}

Generation with for the Object egg1 with pyhsics … did not find a solution … 

so I am using now this way… but i think its weacker and need more power… 

 

action = { [1] = function (x) egg1 = display.newCircle(sceneGroup, 200, 300, 25 ); egg1:setFillColor( 0.72, 0.9, 0.16, 0.78 ); egg1.id = "egg1"; schlossZetra1 = false; timer.performWithDelay(10, function() physics.addBody(egg1, "static", { bounce=0.4, friction=1.0,radius=20,filter=blueCollisionFilter } ); egg1.gravityScale = 0; egg1.isBodyActive = true; egg1.isVisible = true; end) end, } action[1](params)

Or what do you think? do you have a better solution? 
Am I on the right way … Thanks for any commencts and improvementsugesstions … 

I think I will Repatriierung also that Part of code Overall times … Do not know how to Write it in a better way. 
If can give to that issue a solution I owuld be very glad… 

Thanks