You need to create a fresh ‘local g’ display group for every circle&text object you create. Otherwise you have one big display group called ‘g’ which contains all the circles and text, so when any element of ‘g’ hits the line, the whole display group is removed.
I think I am doing that. In my createCircle function I have local g = display.newGroup() isn’t that what you are talking about? Plus why would it make the whole display group disappear when it touches a line and not when I click on a circle? It “seems” (in MHO) that it should do both the same way. Thanks!
Can you post all the code?
I made a short project that has everything main.lua [lua] local composer = require “composer” local scene = composer.scene local physics = require(“physics”) physics.start() composer.isDebug = true display.setStatusBar(display.HiddenStatusBar) _W = display.contentWidth _H = display.contentHeight local physics = require “physics” physics.setGravity( 0, 0 ) local function main() composer.gotoScene( “ifc.startPlay” ) end timer.performWithDelay(200,main) [/lua] StartPlay.lua [lua] local composer = require( “composer” ) local scene = composer.newScene() – ----------------------------------------------------------------------------------------------------------------- – All code outside of the listener functions will only be executed ONCE unless “composer.removeScene()” is called. – ----------------------------------------------------------------------------------------------------------------- --local circleGroup = display.newGroup() local circles = {} local texts = {} – “scene:create()” function scene:create( event ) local sceneGroup = self.view print("_W and _H is " … _W … _H ) local floor = display.newImageRect(sceneGroup,“images/floor.png”,_W,2) floor.x=_W/2 floor.y=_H-1 floor.myName=“Floor” floor.id=“Floor” sceneGroup:insert(floor) --physics.addBody(floor, “static”, {isSensor = True}) physics.addBody(floor, “static”) level1b( sceneGroup, 20, 30, 40) math.randomseed( os.time() ) end function level1b( group, objective, speed, points) local created=0 local loop_counter=0 local i=0 local j=0 – worry about points later repeat loop_counter=loop_counter+1 j=math.floor(loop_counter/5) i=loop_counter-j*5 created = created+1 createCircle(group, 60*i+30,-70*j, 28, created, speed, objective) until (created == 100) end function createCircle(group, xCenter, yCenter, radius, created, speed, objective) – I noticed I am not really using this group - is that a problem? local c = display.newCircle(group, xCenter, yCenter, radius) local choice=math.random(1,100000) local text = display.newText(group,choice,xCenter,yCenter,Helvetica, 12) local g = display.newGroup() g:insert© text:setTextColor(0,0,1,255) g:insert(text) physics.addBody(g, {density = 0.01, radius = radius, bounce = 0.0}) g:setLinearVelocity(0,speed) g:addEventListener(“touch”, touchCircle) – to make it touchable g:addEventListener(“collision”, CollisionWithLine) – to make it check if collided. g.id = choice g.myName = choice circles[created]=c texts[created]=text end function touchCircle(event) local obj = event.target local cid = obj.id if event.phase == “began” then print (“Circle number " … cid … " was clicked!”) obj.isVisible = false end end function CollisionWithLine(event) local obj = event.target local cid = obj.id local targ = event.other if event.phase == “began” then if targ.myName == “Floor” then print (“Circle number " … cid … " collided with floor!”) print ("obj1 is " … targ.myName … " obj2 is " … obj.myName) obj.isVisible = false – then remove the object after 50 cycles – local function removeObject() – took this out to show objects going invisible enough for this demo. – obj.isVisible = false – display.remove(obj) – obj = nil – end – timer.performWithDelay(50, removeObject, 1) end end end – “scene:show()” function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == “will” ) then – Called when the scene is still off screen (but is about to come on screen). elseif ( phase == “did” ) then – Called when the scene is now on screen. – Insert code here to make the scene come alive. – Example: start timers, begin animation, play audio, etc. end end – “scene:hide()” function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == “will” ) then – Called when the scene is on screen (but is about to go off screen). – Insert code here to “pause” the scene. – Example: stop timers, stop animation, stop audio, etc. elseif ( phase == “did” ) then – Called immediately after scene goes off screen. end end – “scene:destroy()” function scene:destroy( event ) local sceneGroup = self.view – Called prior to the removal of scene’s view (“sceneGroup”). – Insert code here to clean up the scene. – Example: remove display objects, save state, etc. end – Listener setup scene:addEventListener( “create”, scene ) scene:addEventListener( “show”, scene ) scene:addEventListener( “hide”, scene ) scene:addEventListener( “destroy”, scene ) return scene [/lua]
Try adding sceneGroup:insert(g) at the bottom of createCircle.
Tried.
Didn’t work. 
Strange one. I won’t be back in front of my dev machine for a few hours, I can try running the code then.
Thanks. That is why I tore everything else out and worked on these pieces only. It just appears to me that the groups are being treated differently by the touch and the collusion. Appreciate any assistance you can give me. Thanks!
Hi @universalenglishinchina,
As general advice, I can’t recommend that you try to add a physics body and sense collision detection on a group (display group). Groups and physics can be a difficult mix and you shouldn’t move groups around independently of other groups when you’re trying to detect physics collisions.
Take care,
Brent
So, are you saying I cannot have a bunch of circles on the screen where I wrote inside them? Like numbers and letters? And try to detect collisions?
I have to make png’s of all those items I want???
Let’s assume I cannot do what I want (that would be terrible!).
Is there a way to check (without countless iterations) to know when each object is at a certain y position (like if ypos < 50 from bottom.)
If I MUST (really last resort) as long as I can know right when it disappears off edge. But MUCH prefer just a piece of it is going off edge.
Goal is put a math problem in circle and if they don’t select it because they think it is wrong and it hits floor then do something. Like take points away and flash letting them know they made a mistake.
So, I thought about collusion of the group with the floor. but, you said no. So, is there another way to solve this WITHOUT me having my artist draw 1,000,000 circles with unique math problems in the middle???
Sorry, I’d forgotten that using groups as physics objects was a no-no. Don’t panic though - there’s always a way! 
2 options I can think of:
- When you spawn a circle/text object, create it as a display group as you’ve done, then save that group as a snapshot to disk. Then destroy the group, load the image and then add the physics, myName etc. If you save them like ‘circle_A_text_44’, you can check whether that circle/text combination exists already and load it if so, or create the new snapshot if not.
I used this method on my app Football Faces: https://itunes.apple.com/us/app/id889897741?mt=8&ign-mpt=uo%3D8
All those images had the effects applied, then they were saved, destroyed, and re-loaded as normal images. I did this because applying live effects to all those images at once slowed the app to a crawl whenever they were scrolled.
- Create the circle and text objects separately, making just the circle a physics object. Make the text object a child of the circle - i.e. c.text = text. Add both to sceneGroup.
Use a runtime enterFrame listener, which is a function that runs every frame. Then loop through all the circle objects that currently exist in the circles table, and do circles[i].text.x, circles[i].text.y = circles[i].x, circles[i].y. That way the text will follow its circle wherever it goes.
When you destroy the circle, remember to destroy the text object first. i.e. display.remove(obj.text).
Thank you - I found what you are referring to for number 2.
I saw how many times it runs, my goodness. Isn’t that labor intensive for the mobile devices?
But, it does work like a charm!
Thank you SOOO much for your extensive help and thanks Brent for being the bearer of bad news! haha - at least it made it possible to find a solution quickly.
Thank you!
I don’t think you can add a physics body to a line object. Instead try creating a long, thin rectangle.
Thanks for your reply Nick. I read this in the documentation. “You should not add a physics body to a line object created with display.newLine(), unless the body type is “static”.” I did make it static. Anyway I changed it. local floor = display.newImageRect(sceneGroup,“images/floor.png”,_W,2) floor.x=_W/2 floor.y=_H-1 floor.myName=“Floor” --didn’t know what to put here floor.id=“Floor” – didn’t really need this but I put it anyway. physics.addBody(floor, “static”) and I still get the SAME strange behavior.
Are both the circle and the rect inserted into the same display group?
Yes. I see it getting triggered. Just null is used. Here is my code - some stuff taken out. local floor = display.newImageRect(sceneGroup,“images/floor.png”,_W,2) – sceneGroup:insert(floor) ( I don’t think I need this, I tried both ways - with and without) physics.addBody(floor, “static”) level1b( sceneGroup, composer.objective, composer.gravity, composer.points) function level1b( group, objective, speed, points) … createCircle(group, 60*i+30,-60*j, 28, created, speed, objective) … end function createCircle(group, xCenter, yCenter, radius, created, speed, objective) local c = display.newCircle(group, xCenter, yCenter, radius) local choice=“Hello” local text = display.newText(group,choice,xCenter,yCenter,Helvetica, 12) physics.addBody(c, {density = 0.01, radius = radius, bounce = 0.0}) physics.addBody(text, {density = 0.01, radius = radius, bounce = 0.0}) c:setLinearVelocity(0,speed) text:setLinearVelocity(0,speed) c:addEventListener(“touch”, touchCircle) – to make it touchable c:addEventListener(“collision”, CollisionWithLine) – to make it check if collided. c.id = choice text.id = choice c.objective = objective c.myName = choice end function touchCircle(event) local obj = event.target local cid = obj.id local objective = obj.objective if event.phase == “began” then print (“Circle number " … cid … " was clicked!”) print ("objective is " … objective) end end function CollisionWithLine(event) local obj = event.target – will worry about this later local cid = obj.id – will worry about this later local objective = obj.objective – will worry about this later if (event.object1 == nil) then print(“nothing in object1”) – All I get back is this!!! return false end if (event.object2 == nil) then print(“nothing in object2”) return false end if event.phase == “began” then print (“Circle number " … cid … " collided with floor!”) print ("obj1 is " … event.object1.myName … " obj2 is " … event.object2.myName) print ("objective is " … objective) end end As soon as I create the circles, I get a Bunch of “nothing in object1” Then when the balls hit the bottom line I get “nothing in object1” Sorry for taking so much of your time. I am probably doing something stupid.
It just gets frustrating.
what is with the formatting? It looked great when I typed it!!! UGH!
You need to wrap code in lua tags.
Put [*lua*] (excluding the asterisks) before the code and [*/lua*] after the code.
Ok, trying to type it again.
Yes. I see it getting triggered. Just null is used. Here is my code - some stuff taken out.
local floor = display.newImageRect(sceneGroup,“images/floor.png”,_W,2)
– sceneGroup:insert(floor) ( I don’t think I need this, I tried both ways - with and without)
physics.addBody(floor, “static”)
level1b( sceneGroup, composer.objective, composer.gravity, composer.points)
function level1b( group, objective, speed, points)
…
createCircle(group, 60*i+30,-60*j, 28, created, speed, objective)
…
end
function createCircle(group, xCenter, yCenter, radius, created, speed, objective)
local c = display.newCircle(group, xCenter, yCenter, radius)
local choice=“Hello”
local text = display.newText(group,choice,xCenter,yCenter,Helvetica, 12)
physics.addBody(c, {density = 0.01, radius = radius, bounce = 0.0})
physics.addBody(text, {density = 0.01, radius = radius, bounce = 0.0})
c:setLinearVelocity(0,speed)
text:setLinearVelocity(0,speed)
c:addEventListener(“touch”, touchCircle) – to make it touchable
c:addEventListener(“collision”, CollisionWithLine) – to make it check if collided.
c.id = choice
text.id = choice
c.objective = objective
c.myName = choice
end
function touchCircle(event)
local obj = event.target
local cid = obj.id
local objective = obj.objective
if event.phase == “began” then
print (“Circle number " … cid … " was clicked!”)
print ("objective is " … objective)
end
end
function CollisionWithLine(event)
local obj = event.target – will worry about this later
local cid = obj.id – will worry about this later local
objective = obj.objective – will worry about this later
if (event.object1 == nil) then
print(“nothing in object1”) – All I get back is this!!!
return false
end
if (event.object2 == nil) then
print(“nothing in object2”)
return false
end
if event.phase == “began” then
print (“Circle number " … cid … " collided with floor!”)
print ("obj1 is " … event.object1.myName … " obj2 is " … event.object2.myName)
print ("objective is " … objective)
end
end
As soon as I create the circles, I get a Bunch of “nothing in object1” Then when the balls hit the bottom line I get “nothing in object1” Sorry for taking so much of your time. I am probably doing something stupid.
It just gets frustrating.