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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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) |
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.
@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 ?
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.
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.
@CluelessIdeas I tried it and did not work I'll show the entire code again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | 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 |