I have Balls that fall from the bottom of the screen . But if they fall to the bottom of the screen I want to send the user to the restart screen if 5 balls fall past the bottom . But how do I do this ? I need help please.
- Download this example:
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/02/balloonPop2.zip
- Take it apart and examine it piece by piece. It is pretty well commented and very short.
You should then be good to go.
I’ll be happy to answer specific questions about the example, but you’re on your own as far as applying what you learn from the example to your game and the problem you want to solve.
https://www.youtube.com/watch?v=P-yHOfo5_Ag&feature=youtu.be
https://gist.github.com/roaminggamer/615a00aa0d726ec1861c8237a6f65942#file-main-lua
Thanks a lot .
This code works , but when I try to implement it in my code nothing happens with no errors. Can you please help me
Roaminggamer gave you a great start… but to break it down for you, simply track the x,y position of your balls and if the y value is greater than the y bound of your screen then add that to a counter of balls that have gone out of scope. Once that count >= 5 then show your restart screen.
If you are using physics, then simply set a boundary at the bottom of the screen and count the ball collisions.
How do I put a boundary ? This is what I have so far
local function enterFrame( self ) -- End the Game if any balloon's CENTER falls below the bottom of the screen if( self.y \>= bottom ) then endGame() end end
Assuming you have an array of balls then I would do the following…
function onFrame (event) for i=1, #balls do if ball[i].y \> display.screenOriginX + display.contentHeight then ballsOutOfScope = ballsOutOfScope + 1 end end if ballsOutOfScope \> 5 then showRestartScreen() end end Runtime:addEventListener( "enterFrame", onFrame )
I keep getting this error :
79: attempt to get length of global 'ball' (a nil value)
This is my code :
-- requires local physics = require "physics" physics.start() local composer = require( "composer" ) local scene = composer.newScene() -- background local top = display.contentCenterY - display.actualContentHeight/2 local bottom = top + display.actualContentHeight function scene:create(event) local screenGroup = self.view -- Set default screen background color to blue display.setDefault( "background", 4, 2, 1 ) display.setDefault("fillColor", 0, 3, 1) end local numberBall = 5 math.randomseed( os.time() ) local grpBall = display.newGroup( ) -- listener for touching the ball. local function lstBallTouch(event) -- Using 'began' as otherwise it's when your finger is lifted which isn't as expected. if ( event.phase == "began" ) then grpBall:remove( event.target ) display.remove( event.target ) -- Remove what was touched. end end -- Spawn numberBall at random locations. (Loop X times) local function spawnBall( randomPosition ) for i= 1,numberBall do local randomPosition = math.random(10,300) Ball = display.newCircle(60, 60, 13) Ball.x = randomPosition Ball.y = 50 Ball:setFillColor( 0, 5, 0 ) grpBall:insert( Ball ) Ball:addEventListener("touch", lstBallTouch) -- Give it a listener to process a touch. transition.to( Ball, { time=math.random(4000,8000), x=math.random(-10,100) , y=600, onComplete=clearBall } ); physics.addBody( Ball, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } ); end end local timeLimit = 60 timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 24) timeLeft:setTextColor(255,0,0) local fncDoSpawn = function() return spawnBall( randomPosition ) end local tmrDoSpawn = timer.performWithDelay( 3000, fncDoSpawn, 0 ) function fncCountdownTimer() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit == 0)then display.remove(grpBall) -- This should remove the listeners too. local GameOver = display.newText( "Time Limit Reached", display.contentCenterX, display.contentCenterY, native.systemFont, 48 ) timer.cancel( tmrDoSpawn ) -- Stop the balls from spawning. return true end end local timeLimit = timer.performWithDelay(1000,fncCountdownTimer,timeLimit) function onFrame (event) for i=1, #ball do if ball[i].y \> display.screenOriginX + display.contentHeight then ballOutOfScope = ballOutOfScope + 1 end end if ballOutOfScope \> 5 then showRestartScreen() end end function scene:show(event) Runtime:addEventListener( "enterFrame", onFrame ) end function scene:hide(event) composer.removeScene( "restart" ) end function scene:destroy(event) end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destroy", scene) return scene
At a quick glance I can’t see where you are loading your array ‘ball’, you seem to spawn circle display objects as ‘Ball’, but not store them in the ‘ball’ array/table, so statements like #ball and ball[i] will fail.
How do I store them in an array/table ?
Something like this will populate 5 balls into the balls array
local function spawnBall() local ball = display.newCircle(grpball, 60, 60, 13) ball.x, ball.y = math.random(10,300), 50 ball:setFillColor( 0, 1, 0 ) ball:addEventListener("touch", lstballTouch) transition.to( ball, { time=math.random(4000,8000), x=math.random(-10,100) , y=600, onComplete=clearball } ); physics.addBody( ball, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } ); return ball end local balls = {} local aNewBall = nil for i = 1, 5 do aNewBall = spawnBall() balls[#balls+1] = aNewBall end
- Download this example:
https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/02/balloonPop2.zip
- Take it apart and examine it piece by piece. It is pretty well commented and very short.
You should then be good to go.
I’ll be happy to answer specific questions about the example, but you’re on your own as far as applying what you learn from the example to your game and the problem you want to solve.
https://www.youtube.com/watch?v=P-yHOfo5_Ag&feature=youtu.be
https://gist.github.com/roaminggamer/615a00aa0d726ec1861c8237a6f65942#file-main-lua
Thanks a lot .
This code works , but when I try to implement it in my code nothing happens with no errors. Can you please help me
Roaminggamer gave you a great start… but to break it down for you, simply track the x,y position of your balls and if the y value is greater than the y bound of your screen then add that to a counter of balls that have gone out of scope. Once that count >= 5 then show your restart screen.
If you are using physics, then simply set a boundary at the bottom of the screen and count the ball collisions.
How do I put a boundary ? This is what I have so far
local function enterFrame( self ) -- End the Game if any balloon's CENTER falls below the bottom of the screen if( self.y \>= bottom ) then endGame() end end
Assuming you have an array of balls then I would do the following…
function onFrame (event) for i=1, #balls do if ball[i].y \> display.screenOriginX + display.contentHeight then ballsOutOfScope = ballsOutOfScope + 1 end end if ballsOutOfScope \> 5 then showRestartScreen() end end Runtime:addEventListener( "enterFrame", onFrame )
I keep getting this error :
79: attempt to get length of global 'ball' (a nil value)
This is my code :
-- requires local physics = require "physics" physics.start() local composer = require( "composer" ) local scene = composer.newScene() -- background local top = display.contentCenterY - display.actualContentHeight/2 local bottom = top + display.actualContentHeight function scene:create(event) local screenGroup = self.view -- Set default screen background color to blue display.setDefault( "background", 4, 2, 1 ) display.setDefault("fillColor", 0, 3, 1) end local numberBall = 5 math.randomseed( os.time() ) local grpBall = display.newGroup( ) -- listener for touching the ball. local function lstBallTouch(event) -- Using 'began' as otherwise it's when your finger is lifted which isn't as expected. if ( event.phase == "began" ) then grpBall:remove( event.target ) display.remove( event.target ) -- Remove what was touched. end end -- Spawn numberBall at random locations. (Loop X times) local function spawnBall( randomPosition ) for i= 1,numberBall do local randomPosition = math.random(10,300) Ball = display.newCircle(60, 60, 13) Ball.x = randomPosition Ball.y = 50 Ball:setFillColor( 0, 5, 0 ) grpBall:insert( Ball ) Ball:addEventListener("touch", lstBallTouch) -- Give it a listener to process a touch. transition.to( Ball, { time=math.random(4000,8000), x=math.random(-10,100) , y=600, onComplete=clearBall } ); physics.addBody( Ball, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } ); end end local timeLimit = 60 timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 24) timeLeft:setTextColor(255,0,0) local fncDoSpawn = function() return spawnBall( randomPosition ) end local tmrDoSpawn = timer.performWithDelay( 3000, fncDoSpawn, 0 ) function fncCountdownTimer() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit == 0)then display.remove(grpBall) -- This should remove the listeners too. local GameOver = display.newText( "Time Limit Reached", display.contentCenterX, display.contentCenterY, native.systemFont, 48 ) timer.cancel( tmrDoSpawn ) -- Stop the balls from spawning. return true end end local timeLimit = timer.performWithDelay(1000,fncCountdownTimer,timeLimit) function onFrame (event) for i=1, #ball do if ball[i].y \> display.screenOriginX + display.contentHeight then ballOutOfScope = ballOutOfScope + 1 end end if ballOutOfScope \> 5 then showRestartScreen() end end function scene:show(event) Runtime:addEventListener( "enterFrame", onFrame ) end function scene:hide(event) composer.removeScene( "restart" ) end function scene:destroy(event) end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destroy", scene) return scene
At a quick glance I can’t see where you are loading your array ‘ball’, you seem to spawn circle display objects as ‘Ball’, but not store them in the ‘ball’ array/table, so statements like #ball and ball[i] will fail.
How do I store them in an array/table ?