Add physics automatic + timer

Hello everybody,

I’m searching for a tutorial that shows me to add the physic “angry” (look in the code) automatic after a few seconds. Also i have a timer in the code, but i want when the time is over the game will be go to another screen.

Do you know some tutorials that learn how to do that?

display.setStatusBar(display.HiddenStatusBar) local physics = require( "physics" ) physics.start() physics.setGravity(0, 0) local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local secondsLeft = 20 \* 60 -- 20 minutes \* 60 seconds local background = display.newRect( centerX, centerY, actualW, actualH ) background:setFillColor( 0, 0, 1 ) local score = 0 local scoreTxt = display.newText( "0", 0, 0, native.systemFontBold, 40 ) scoreTxt.x = centerX + actualW/2 - 20 scoreTxt.y = centerY - actualH/2 + 20 local function updateScore(event) score = score + 1 scoreTxt.text = string.format("%d", score ) end local happy = display.newCircle( 0, 0, 10 ) happy.x = 45 happy.y = 45 physics.addBody( happy, "dynamic" ) happy:setFillColor( 0, 1, 0 ) happy.myName = "happy" local angry = display.newCircle( 0, 0, 10 ) angry.x = 420 angry.y = 280 physics.addBody(angry, "static") angry:setFillColor( 1, 0, 0 ) angry.myName = "angry" local clockText = display.newText("20:00", display.contentCenterX, 80, native.systemFontBold, 80) clockText:setFillColor( 0.7, 0.7, 1 ) local function updateTime() -- decrement the number of seconds secondsLeft = secondsLeft - 1 -- time is tracked in seconds. We need to convert it to minutes and seconds local minutes = math.floor( secondsLeft / 60 ) local seconds = secondsLeft % 60 -- make it a string using string format. local timeDisplay = string.format( "%02d:%02d", minutes, seconds ) clockText.text = timeDisplay end local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft ) function touchScreen(event) if event.phase == "began" then transition.to(happy, {time=1000, x=event.x, y=event.y}) end end Runtime:addEventListener( "touch", touchScreen ) function moveAngry() transition.to(angry, {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 ), onComplete=moveAngry }) end moveAngry() local function functions() display.remove(angry) updateScore() end function onCollision(event) if event.phase == "began" then if event.target.myName == "angry" and event.other.myName == "happy" then functions() end end end angry:addEventListener( "collision", onCollision )

Already a happy new year!

Ok so i made it so angry gets his physics powers after 10 seconds. Then i made it so when the secondsLeft reaches 0 then it prints “bye” to the console. Thats where you would put you goToGamever stuff.

display.setStatusBar(display.HiddenStatusBar) local physics = require( "physics" ) physics.start() physics.setGravity(0, 0) local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local secondsLeft = 20 \* 60 -- 20 minutes \* 60 seconds local background = display.newRect( centerX, centerY, actualW, actualH ) background:setFillColor( 0, 0, 1 ) local score = 0 local scoreTxt = display.newText( "0", 0, 0, native.systemFontBold, 40 ) scoreTxt.x = centerX + actualW/2 - 20 scoreTxt.y = centerY - actualH/2 + 20 local function updateScore(event) score = score + 1 scoreTxt.text = string.format("%d", score ) end local happy = display.newCircle( 0, 0, 10 ) happy.x = 45 happy.y = 45 physics.addBody( happy, "dynamic" ) happy:setFillColor( 0, 1, 0 ) happy.myName = "happy" local angry = display.newCircle( 0, 0, 10 ) angry.x = 420 angry.y = 280 local function addAfterFew() physics.addBody(angry, "static" ) end timerAdd = timer.performWithDelay( 10000, addAfterFew, 1 ) --This adds the physics to it after 20 seconds. angry:setFillColor( 1, 0, 0 ) angry.myName = "angry" local clockText = display.newText("20:00", display.contentCenterX, 80, native.systemFontBold, 80) clockText:setFillColor( 0.7, 0.7, 1 ) local function updateTime() -- decrement the number of seconds secondsLeft = secondsLeft - 1 -- time is tracked in seconds. We need to convert it to minutes and seconds local minutes = math.floor( secondsLeft / 60 ) local seconds = secondsLeft % 60 -- make it a string using string format. local timeDisplay = string.format( "%02d:%02d", minutes, seconds ) clockText.text = timeDisplay end local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft ) function touchScreen(event) if event.phase == "began" then transition.to(happy, {time=1000, x=event.x, y=event.y}) end end Runtime:addEventListener( "touch", touchScreen ) function moveAngry() transition.to(angry, {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 ), onComplete=moveAngry }) end moveAngry() local function functions() display.remove(angry) updateScore() end function onCollision(event) if event.phase == "began" then if event.target.myName == "angry" and event.other.myName == "happy" then functions() end end end angry:addEventListener( "collision", onCollision ) local function endGame() if secondsLeft == 0 then print("bye") Runtime:removeEventListener( "enterFrame", endGame ) end end Runtime:addEventListener( "enterFrame", endGame )

Yes the Runtime listener isnt the most efficient way but it works!

–SonicX278

You’ll get a faster response if you post in newbie forums or the general question. Just a tip!

–SonicX278

Ok so i made it so angry gets his physics powers after 10 seconds. Then i made it so when the secondsLeft reaches 0 then it prints “bye” to the console. Thats where you would put you goToGamever stuff.

display.setStatusBar(display.HiddenStatusBar) local physics = require( "physics" ) physics.start() physics.setGravity(0, 0) local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local secondsLeft = 20 \* 60 -- 20 minutes \* 60 seconds local background = display.newRect( centerX, centerY, actualW, actualH ) background:setFillColor( 0, 0, 1 ) local score = 0 local scoreTxt = display.newText( "0", 0, 0, native.systemFontBold, 40 ) scoreTxt.x = centerX + actualW/2 - 20 scoreTxt.y = centerY - actualH/2 + 20 local function updateScore(event) score = score + 1 scoreTxt.text = string.format("%d", score ) end local happy = display.newCircle( 0, 0, 10 ) happy.x = 45 happy.y = 45 physics.addBody( happy, "dynamic" ) happy:setFillColor( 0, 1, 0 ) happy.myName = "happy" local angry = display.newCircle( 0, 0, 10 ) angry.x = 420 angry.y = 280 local function addAfterFew() physics.addBody(angry, "static" ) end timerAdd = timer.performWithDelay( 10000, addAfterFew, 1 ) --This adds the physics to it after 20 seconds. angry:setFillColor( 1, 0, 0 ) angry.myName = "angry" local clockText = display.newText("20:00", display.contentCenterX, 80, native.systemFontBold, 80) clockText:setFillColor( 0.7, 0.7, 1 ) local function updateTime() -- decrement the number of seconds secondsLeft = secondsLeft - 1 -- time is tracked in seconds. We need to convert it to minutes and seconds local minutes = math.floor( secondsLeft / 60 ) local seconds = secondsLeft % 60 -- make it a string using string format. local timeDisplay = string.format( "%02d:%02d", minutes, seconds ) clockText.text = timeDisplay end local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft ) function touchScreen(event) if event.phase == "began" then transition.to(happy, {time=1000, x=event.x, y=event.y}) end end Runtime:addEventListener( "touch", touchScreen ) function moveAngry() transition.to(angry, {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 ), onComplete=moveAngry }) end moveAngry() local function functions() display.remove(angry) updateScore() end function onCollision(event) if event.phase == "began" then if event.target.myName == "angry" and event.other.myName == "happy" then functions() end end end angry:addEventListener( "collision", onCollision ) local function endGame() if secondsLeft == 0 then print("bye") Runtime:removeEventListener( "enterFrame", endGame ) end end Runtime:addEventListener( "enterFrame", endGame )

Yes the Runtime listener isnt the most efficient way but it works!

–SonicX278

You’ll get a faster response if you post in newbie forums or the general question. Just a tip!

–SonicX278