Insert Group

Hey I need Help in this director class code. I know is easy I understand it but there’s this line of code I’m stuck. Is this part

local group = display.newGroup  
  
local spawnBalloon = function()  
if spawn == true then  
ball = display.newImage(group, "balloon.pn")  
ball.x = 60 + math.random  
ball.y = 550  
physics.addBody(ball, {density = 0.0 , friction =0.1, bounce = 0.1})  
balls[#balls + 1] = ball  
group:insert(ball)  
end  
end  
timer.performWithDelay(500, spawnBalloon, -1)  
  
local function delete()  
for i=group.numChildren, 1, -1 do  
local child = group[i]  
if group[i].y \< -100 then  
display.remove(group[i])  
end  
end  
end  
Runtime:addEventListener("enterframe", delete)  

Ok from here I trying to figure out when I change scene the balloon don’t appear t in the next scene the problem is when I do group:insert(ball) you will still see the balloon in the next scene. Also when I do localGroup:insert(ball) i get an error when I change scene. I will appreciate if I got help. Please thanks :slight_smile: [import]uid: 17058 topic_id: 19351 reply_id: 319351[/import]

display.newGroup is a function so you need to call it with () like so: display.newGroup(). Also, I don’t see any localGroup variable being defined in here, so unless you already defined it somewhere else it doesn’t exist and that’s the reason for the error when you try to insert anything into it. [import]uid: 61899 topic_id: 19351 reply_id: 74695[/import]

@Cluelessideas yea display.newGroup is a function is I forgot to put (). My problem is when I put group:insert(ball) for changing scene nothing would happen and the balloon would appear in the next scene. What could be the problem ? [import]uid: 17058 topic_id: 19351 reply_id: 74705[/import]

You’re using director right? Everything that is to be handled by director must be in a display group and that group must be returned by the new() function of each module. If you don’t have a line like return group just before the end tag of the new() function that could be the problem. It’s hard to guess without seeing all the code of the module, and it would also help if the code sample had proper indentation. [import]uid: 61899 topic_id: 19351 reply_id: 74708[/import]

Yes I’m using a director class. So you think the problem for this I got to put return Group at the end of the code right?

Sorry i didn’t display most the of the code is I"m not at home but I know from this part of code is I’m struggling. [import]uid: 17058 topic_id: 19351 reply_id: 74710[/import]

@CluelessIdeas I tried it and did not work I’ll show the entire code again.

[code] module(…, package.seeall)

function new()
local localGroup = display.newGroup()

require( “ice” )
local physics = require(“physics”)
local ui = require(“ui”)
local gameUI = require(“gameUI”)
physics.start()
physics.setGravity(0, -1)
local scores = ice:loadBox( “scores” )

local high = scores:retrieve(“best”)
print(high)

display.setStatusBar( display.HiddenStatusBar )

local scoreText
local score = 0
local highscore
local button1
local runMode = true
local spawn = true
local touch = true
local gameOver = false
local bkg = display.newImage( “bkg.png” )
bkg.x = 160; bkg.y = 240
localGroup:insert(bkg)

local score_txt =display.newText("Score: ",0,0,nil,25)
score_txt.x = 160; score_txt.y = 440
score_txt:setTextColor(0,0,0)
score_txt.text = "Score: "… score
localGroup:insert(score_txt)

local spawnBall = function( event )
local t = event.target
local phase = event.phase
if touch == true then
if “began” == phase then
t:removeSelf() – destroy object
score = score + math.random( 1000 )
score_txt.text = "Score: "… score
end
– Stop further propagation of touch event
return true
end
end

local balls = {}

local group = display.newGroup()

local spawnBalloon = function()

if spawn == true then
ball = display.newImage( group, “balloon.png” )
ball.x = 60 + math.random( 160 )
ball.y = 550
physics.addBody( ball, { density=0.0, friction=0.1, bounce=0.1 } )
ball:addEventListener( “touch”, spawnBall )
balls[#balls + 1] = ball
group:insert(ball)
end
end
timer.performWithDelay(500, spawnBalloon, -1)

local function delete()
for i=group.numChildren,1,-1 do
local child = group[i]
if group[i].y < -100 then
display.remove(group[i]) – dont know about this part, try it
end
end
end

Runtime:addEventListener(“enterFrame”, delete)
local bt = display.newImage (“pause.png”)
bt.x = 290
bt.y = 30
localGroup:insert(bt)

local bt2 = display.newImage (“pauseOver.png”)
bt2.x = bt.x
bt2.y = bt.y
bt2.isVisible = false
localGroup:insert(bt2)

local Pause = display.newText(“Pause”, 5, 0, “ArialRoundedMTBold”, 30)
Pause.x = 160
Pause.y = 110
Pause:setTextColor(0,0,0)
Pause.isVisible = false
localGroup:insert(Pause)

local Resume = display.newText(“Resume”, 5, 0, “ArialRoundedMTBold”, 25)
Resume.x = 160
Resume.y = 190
Resume:setTextColor(0,0,0)
Resume.isVisible = false
localGroup:insert(Resume)

local Quit = display.newText(“Quit”, 5, 0, “ArialRoundedMTBold”, 25)
Quit.x = 160
Quit.y = 270
Quit:setTextColor(0,0,0)
Quit.isVisible = false
localGroup:insert(Quit)

–Function for start button
local function done ()
physics.pause()
spawn = false
touch = false
bt.isVisible = false
bt2.isVisible = true
Quit.isVisible = true
Resume.isVisible = true
Pause.isVisible = true
result = timer.pause( timerID )
end
bt:addEventListener(“tap”, done)

–Function for pause button
local function done ()
physics.start()
spawn = true
touch = true
bt.isVisible = true
bt2.isVisible = false
Quit.isVisible = false
Resume.isVisible = false
Pause.isVisible = false
result = timer.resume( timerID )
end
Resume:addEventListener(“tap”, done)

local function quit (event)
if event.phase == “ended” then
director:changeScene (“play”)
Quit.isVisible = false
Resume.isVisible = false
Pause.isVisible = false
spawn = false
end
end
Quit:addEventListener (“touch”, quit)

local count = 60
local hasRun = 0

local time = display.newText( “60”, 5, 0, “ArialRoundedMTBold”, 40)
time:setTextColor( 0, 0, 0 )
localGroup:insert(time)

local timeDelay = 700

function time:timer( event )

count = count-1
hasRun = hasRun+1

self.text = count

if hasRun >= 10 then
timer.cancel( event.source )
director:changeScene (“popover”)
end
end

– Register to call t’s timer method 50 times
timerID = timer.performWithDelay( timeDelay, time, -1 )

local highText = display.newText("", 0,0,nil,30)
highText.x = display.contentWidth/2
highText.y = display.contentHeight/2
highText.text = "HighScore: "… scores:retrieve(“best”)
localGroup:insert(highText)

local function show_highScore()
gameOver = not gameOver

scores:storeIfHigher( “best”, score )
scores:save()
local highText = display.newText("", 0,0,nil,30)
highText.x = display.contentWidth/2
highText.y = display.contentHeight/2
if score > high then
highText.text = "HighScore: "… scores:retrieve(“best”)
else

if score > high then
print(“new high score”)
highText.text = "HighScore: "… scores:retrieve(“best”)
end
end

end

timer.performWithDelay(60000, show_highScore, 1)

return localGroup
end[/code]

Now this should more clear ok so when I put group:insert(ball) the ball would appear in the next scene and I don’t know why everything seems ok except that part of the balloon what could be the problem. [import]uid: 17058 topic_id: 19351 reply_id: 74740[/import]

The group you created at line 55 to hold the balls isn’t being inserted in the localGroup. You probably figured it out by now… sorry, but I didn’t receive any notification of new messages in this thread. [import]uid: 61899 topic_id: 19351 reply_id: 76174[/import]

@Cluelessideas yeah I already got sorry that I didn’t update saying I got it [import]uid: 17058 topic_id: 19351 reply_id: 76191[/import]