AddEventListener: Listener cannot be nil - lua

Can someone please explain what is going wrong here? It went wrong as soon as I added the AddEventListener
newBalloon:addEventListener( "tap", pushBalloon )
Complete Code:
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require( "physics" )
physics.start()
-- Configure image sheet
local positioninsheetOptions = 144.1
local sheetOptions =
{
frames =
{
{
x = 0,
y = 0,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*2,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*3,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*4,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*5,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*6,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*7,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*8,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*9,
width = 112,
height = 142
},
{
x = 0,
y = positioninsheetOptions*10,
width = 112,
height = 142
},
},
}
local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions )
local tapCount = 0
local platform
local tapText
local balloonsTable = {}
local leftBorder
local rightBorder
local backGroup
local mainGroup
local uiGroup
local platform
local platform2
local function createBalloon()
local randomBalloon = math.random( 10 )
local newBalloon = display.newImageRect( objectSheet, randomBalloon, 112, 142 )
if newBalloon then
table.insert( balloonsTable, newBalloon )
physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0 } )
newBalloon.myName = "bigBalloon"
newBalloon.alpha = 0.75
newBalloon.gravityScale = randomBalloon/-150
newBalloon:addEventListener( "tap", pushBalloon )
end
local whereFrom = math.random( 3 )
if ( whereFrom == 1 ) then
-- From the left
newBalloon.x = 100
newBalloon.y = display.contentHeight+300
elseif ( whereFrom == 2 ) then
-- From the top
newBalloon.x = 160
newBalloon.y = display.contentHeight+300
elseif ( whereFrom == 3 ) then
-- From the right
newBalloon.x = 220
newBalloon.y = display.contentHeight+300
end
end
local function gameLoop()
-- Create new balloon
createBalloon()
-- Remove balloons which have drifted off screen
for i = #balloonsTable, 1, -1 do
local thisBalloon = balloonsTable[i]
if ( thisBalloon.x < -100 or
thisBalloon.x > display.contentWidth + 100 or
thisBalloon.y < -100 )
then
display.remove( thisBalloon )
table.remove( balloonsTable, i )
end
end
end
local function pushBalloon( event )
local tappedBalloon = event.target
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
if event.phase == "began" then
tappedBalloon.gravityScale = 10
end
end
local function pushBalloon2()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon2.gravityScale = 10
balloon2:applyLinearImpulse( 0.1, 0, balloon2.x, balloon2.y )
end
local function pushBalloon3()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon3.gravityScale = 10
balloon3:applyLinearImpulse( -0.1, 0, balloon3.x, balloon3.y )
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
physics.pause()
local background = display.newImageRect( "background.png", 360, 570 )
background.x = display.contentCenterX
background.y = display.contentCenterY
platform = display.newImageRect( "platform.png", 10, display.contentHeight*5 )
platform.x = -5
platform.y = 0
physics.addBody( platform, "static", { friction=0.5, bounce=0.3 } )
platform2 = display.newImageRect( "platform.png", 10, display.contentHeight*5 )
platform2.x = display.contentWidth+5
platform2.y = 0
physics.addBody( platform2, "static", { friction=0.5, bounce=0.3 } )
createBalloon()
end
-- show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
gameLoopTimer = timer.performWithDelay( 1250, gameLoop, 0 )
-- Code here runs when the scene is entirely on screen
physics.start()
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
physics.pause()
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene
Thank you.

You refer to function (pushBalloon) do not exist yet when add event listener. So put definition of function above line you add event listener like that
local function pushBalloon( event )
local tappedBalloon = event.target
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
if event.phase == "began" then
tappedBalloon.gravityScale = 10
end
end
...
newBalloon:addEventListener( "tap", pushBalloon )

At the point in the file where you are adding the listener, pushBalloon() is not defined. You should add a forward declaration earlier in the file:
local scene = ...
local pushBalloon
...
-- createBalloon() and other existing code
...
pushBalloon = function ( event )
-- your function code
end

Related

How can I remove these DisplayObjects at end of a scene?

I want the balloons in this scene, which are DisplayObjects, to disappear when the scene ends. I have added them to the local sceneGroup = scene.view by passing this as the first argument to display.newImageRect() and assumed this would be enough for them to be removed as they are listed in the destroy.scene bit at the bottom. However when it transitions to the next scene they are still there. Why are these DisplayObjects not being removed?
local composer = require( "composer" )
local scene = composer.newScene()
local Balloons
local positioninsheetOptions = 100
local sheetOptions =
{
frames =
{
{
x = 0,
y = 0,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*2,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*3,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*4,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*5,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*6,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*7,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*8,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*9,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*10,
width = 71,
height = 100
},
{
x = 0,
y = positioninsheetOptions*11,
width = 71,
height = 100
},
},
}
local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions )
Balloons = {}
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
local function gotoGame()
composer.gotoScene( "game" )
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
local background = display.newImageRect( sceneGroup, "background.png", 800, 1400 )
background.x = display.contentCenterX
background.y = display.contentCenterY
local balloonBlue1 = display.newImageRect(sceneGroup, objectSheet, 6, 71, 100)
local balloonYellow1 = display.newImageRect(sceneGroup, objectSheet, 8, 71, 100)
local balloonRed1 = display.newImageRect(sceneGroup, objectSheet, 10, 71, 100)
local balloonBlue2 = display.newImageRect(sceneGroup, objectSheet, 6, 71, 100)
local Balloons = display.newGroup()
Balloons:insert( balloonBlue1 )
Balloons:insert( balloonYellow1 )
Balloons:insert( balloonRed1 )
Balloons:insert( balloonBlue2 )
Balloons:addEventListener( "tap", gotoGame )
balloonBlue1.x = (display.contentWidth/8)
balloonBlue1.y = balloonBlue1.height
balloonYellow1.x = (display.contentWidth/8)*3
balloonYellow1.y = balloonBlue1.height
balloonRed1.x = (display.contentWidth/8)*5
balloonRed1.y = balloonBlue1.height
balloonBlue2.x = (display.contentWidth/8)*7
balloonBlue2.y = balloonBlue1.height
end
-- show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene
You need to add the Balloon group (of which the balloons are children) to your sceneGroup.
In scene:destroy, make sure you remove any listeners and cancel any transitions on the things you want destroyed. If you have, when the scene's view is destroyed, the Balloon group and all it's children will also be destroyed.

Expecting a table?

I started learning Corona yesterday so sorry for my ignorance. Can someone please explain why it is "expecting table" on line 108 (physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0.8 } )). All I want it to do is take the image from the object sheet and display it.
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require( "physics" )
physics.start()
-- Configure image sheet
local sheetOptions =
{
frames =
{
{
x = 0,
y = 0,
width = 112,
height = 142
},
{
x = 0,
y = 142,
width = 112,
height = 284
},
{
x = 0,
y = 284,
width = 112,
height = 568
},
{
x = 0,
y = 568,
width = 112,
height = 710
},
{
x = 0,
y = 710,
width = 112,
height = 852
},
{
x = 0,
y = 852,
width = 112,
height = 994
},
{
x = 0,
y = 994,
width = 112,
height = 1136
},
{
x = 0,
y = 1136,
width = 112,
height = 1278
},
{
x = 0,
y = 1278,
width = 112,
height = 1420
},
},
}
local objectSheet = graphics.newImageSheet( "gameObjects.png", sheetOptions )
local tapCount = 0
local platform
local balloon
local balloon2
local balloon3
local tapText
local function createBalloon()
newBalloon = display.newImageRect( mainGroup, objectSheet, 1, 112, 142 )
physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0.8 } )
newBalloon.myName = "Bigballoon"
local whereFrom = math.random( 3 )
if ( whereFrom == 1 ) then
-- From the left
newBalloon.x = 30
newBalloon.y = math.random( display.contentHeight )
elseif ( whereFrom == 2 ) then
-- From the top
newBalloon.x = 60
newBalloon.y = math.random( display.contentHeight )
elseif ( whereFrom == 3 ) then
-- From the right
newBalloon.x = 90
newBalloon.y = math.random( display.contentHeight )
end
end
local function pushBalloon()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon.gravityScale = 10
balloon:applyLinearImpulse( 0.1, 0, balloon.x, balloon.y )
end
local function pushBalloon2()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon2.gravityScale = 10
balloon2:applyLinearImpulse( 0.1, 0, balloon2.x, balloon2.y )
end
local function pushBalloon3()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
balloon3.gravityScale = 10
balloon3:applyLinearImpulse( -0.1, 0, balloon3.x, balloon3.y )
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
physics.pause()
local background = display.newImageRect( "background.png", 360, 570 )
background.x = display.contentCenterX
background.y = display.contentCenterY
local platform = display.newImageRect( "platform.png", 300, 50 )
platform.x = display.contentCenterX
platform.y = display.contentHeight-25
balloon = display.newImageRect( "balloon.png", 112, 112 )
balloon.x = display.contentCenterX
balloon.y = display.contentCenterY
balloon.alpha = 0.8
balloon2 = display.newImageRect( "balloon.png", 112, 112 )
balloon2.x = display.contentCenterX-100
balloon2.y = display.contentCenterY
balloon2.alpha = 0.8
balloon3 = display.newImageRect( "balloon.png", 112, 112 )
balloon3.x = display.contentCenterX+100
balloon3.y = display.contentCenterY
balloon3.alpha = 0.8
createBalloon()
tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 )
tapText:setFillColor( 0, 0, 0 )
physics.addBody( platform, "static" )
physics.addBody( balloon, "dynamic", { radius=50, bounce=0.6 } )
physics.addBody( balloon2, "dynamic", { radius=50, bounce=0.6 } )
physics.addBody( balloon3, "dynamic", { radius=50, bounce=0.6 } )
balloon:addEventListener( "tap", pushBalloon )
balloon.gravityScale = 0
balloon:setLinearVelocity( 0, -10 )
balloon2:addEventListener( "tap", pushBalloon2 )
balloon2.gravityScale = 0
balloon2:setLinearVelocity( 0, -10 )
balloon3:addEventListener( "tap", pushBalloon3 )
balloon3.gravityScale = 0
balloon3:setLinearVelocity( 0, -10 )
end
-- show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
physics.start()
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
physics.pause()
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene
You should declare newBalloon as local when you initialize it in createBalloon(). Also, mainGroup == nil when you pass it as the parent group to display.newImageRect(). For these reasons, newBalloon is probably nil when you pass it to physics.addBody() instead of being a DisplayObject, which is what that method requires as its first argument.
For safety, in createBalloon(), you could do this:
local newBalloon = display.newImageRect( ... )
if newBalloon then
physics.addBody( newBalloon, ... )
else
print("WARNING: newBalloon is nil in createBalloon")
end
By the way, if you want all the balloons to be in the same GroupObject, you could add this:
local balloonGroup -- forward declaration
...
local function createBalloon()
local newBalloon = display.newImageRect( balloonGroup, ... )
...
end
local scene:create()
...
balloonGroup = display.newGroup()
sceneGroup:insert( balloonGroup )
balloonGroup:toFront()
...
balloonGroup:insert( balloon )
balloonGroup:insert( balloon2 )
...
end

Failed to back to Menu from Game Over . Score unable to display on game over screen

I'm very new to corona. I learned all from the web and this is what i produced. The game seems fine but when game over screen is displayed, the button i put to back to menu scene doesn't work and the score that player get failed to be displayed to..
Here is my code...someone kindly please help me out.. what code should i change or add to it??
Thank you. Any help is much appreciated.
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require("physics")
local widget = require "widget"
physics.start()
rand = math.random( 20 )
local slap_sound = audio.loadSound("Sound/slap2.mp3")
local ow = audio.loadSound("Sound/ow.mp3")
local buttonSound = audio.loadSound("Sound/sound2.mp3")
local background
local back
local count={total1=0,total=0,touch=0,life=3}
local total
local time_remain = 5
local mossie
local bee
local shade
local gameOverScreen
local winScreen
local countdown
local life
local pauseBtn
local resumeBtn
local gametmr
---------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE
-- unless "composer.removeScene()" is called.
---------------------------------------------------------------------------------
local gameOver = function()
composer.removeScene("easy")
physics.pause()
--audio.play(gameOverSound)
background = display.newImageRect( "Images/bg.jpg", display.contentWidth, display.contentHeight )
background.anchorX = 0
background.anchorY = 0
background.x, background.y = 0, 0
gameOverScreen = display.newImage("Images/gameover.png",400,300)
gameOverScreen.x = 160
gameOverScreen.y = 240
gameOverScreen.alpha = 0
transition.to(gameOverScreen,{time=500,alpha=1})
--total.isVisible = true
total.text="Score : "..count.touch
total.x = 160
total.y = 400
--total:setTextColor(000000)
botwall.isVisible = false
mossie.isVisible = false
bee.isVisible = false
life.isVisible = false
countdown.isVisible = false
pauseBtn.isVisible = false
resumeBtn.isVisible = false
local myButton = widget.newButton
{
left = 100,
top = 100,
id = "myButton",
label = "Menu",
onEvent = handleButtonEvent
}
myButton.isVisible = true
end
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
composer.gotoScene ("menu")
end
end
local function countDown(e)
time_remain = time_remain-1
countdown.text = time_remain
if time_remain == 0 then
gameOver()
end
end
local pauseGame = function(e)
if(e.phase=="ended") then
audio.play(buttonSound)
physics.pause()
timer.pause(gametmr)
pauseBtn.isVisible = false
resumeBtn.isVisible = true
return true
end
end
local resumeGame = function(e)
if(e.phase=="ended") then
audio.play(buttonSound)
physics.start()
timer.resume(gametmr)
pauseBtn.isVisible = true
resumeBtn.isVisible = false
return true
end
end
local collisionListener=function(self,event)
if(event.phase=="began")then
if(event.other.type=="mossie")then
audio.play(ow)
count.life=count.life-1
if(count.life==0) then
gameOver()
end
event.other:removeSelf()
event.other=nil
else
event.other:removeSelf()
event.other=nil
end
end
end
function onTouch(mossie)
audio.play(slap_sound)
count.touch=count.touch+1
total.text="Score : "..count.touch
mossie.target:removeSelf()
end
function killIt(e)
if(e.phase == "ended") then
gameOver()
end
end
local bottomWall = function()
botwall=display.newImage("Images/tangan.png")
botwall.x = 160
botwall.y = 500
botwall:setFillColor(22,125,185,255)
botwall.type="botwall"
botwall.collision=collisionListener
physics.addBody(botwall,"static",{ density=100.0, friction=0.0, bounce=0.0} )
botwall:addEventListener("collision",botwall)
end
local function newMossie(event)
total.text="Score : "..count.touch
life.text="Life : "..count.life
mossie = display.newImage("Images/biasa.png")
mossie.x = 60 + math.random( 160 )
mossie.y = -100
mossie.type="mossie"
mossie:setFillColor(255,0,0)
physics.addBody( mossie, { density=0.3, friction=0.2, bounce=0.5} )
mossie.name = "mossie"
mossie:addEventListener("touch",onTouch)
end
local function newBee(event)
bee = display.newImage("Images/lebah.png")
bee.x = 60 + math.random( 160 )
bee.y = -100
bee.type="other"
physics.addBody( bee, { density=1.4, friction=0.3, bounce=0.2} )
bee:addEventListener("touch",killIt)
end
-- local forward references should go here
---------------------------------------------------------------------------------
-- "scene:create()"
function scene:create( event )
local sceneGroup = self.view
background = display.newImageRect( "Images/bg.jpg", display.contentWidth, display.contentHeight )
background.anchorX = 0
background.anchorY = 0
background.x, background.y = 0, 0
total=display.newText("Score : 0",display.contentWidth * 0.5, 20, "Arial", 26)
total:setTextColor(000000)
countdown=display.newText(time_remain ,display.contentWidth * 0.9, 20, "Arial", 26)
countdown:setTextColor(000000)
life = display.newText("Life : 3 " ,display.contentWidth * 0.5, 50, "Arial", 26)
life:setTextColor(000000)
pauseBtn = display.newImage("Images/pause.png")
pauseBtn.x = display.contentWidth * 0.1
pauseBtn.y = display.contentHeight - 450
resumeBtn = display.newImage("Images/playb.png")
resumeBtn.x = display.contentWidth * 0.1
resumeBtn.y = display.contentHeight - 450
botwall=display.newImage("Images/tangan.png")
botwall.x = 160
botwall.y = 500
botwall:setFillColor(22,125,185,255)
botwall.type="botwall"
botwall.collision=collisionListener
physics.addBody(botwall,"static",{ density=100.0, friction=0.0, bounce=0.0} )
sceneGroup:insert(background)
sceneGroup:insert(total)
sceneGroup:insert(countdown)
sceneGroup:insert(life)
sceneGroup:insert(pauseBtn)
sceneGroup:insert(resumeBtn)
sceneGroup:insert(botwall)
resumeBtn.isVisible = false
pauseBtn:addEventListener("touch", pauseGame)
resumeBtn:addEventListener("touch", resumeGame)
botwall:addEventListener("collision",botwall)
dropMossie = timer.performWithDelay( 2000 , newMossie, -1 )
dropBee = timer.performWithDelay( 1800 , newBee, -1)
gametmr = timer.performWithDelay(1000, countDown, -1)
-- Initialize the scene here.
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
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
--physics.stop()
--timer.cancel(gametmr)
--pauseBtn:removeEventListener("touch", pauseGame)
--resumeBtn:removeEventListener("touch", resumeGame)
--botwall:removeEventListener("collision",botwall)
--bee:removeEventListener("touch",killIt)
--mossie:removeEventListener("touch",onTouch)
-- 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
I suggest to make another composer scene for gameOverScreen (just like you did with these code block you post). Then do a composer.gotoScene(gameOver) read more here. And for the score I suggest that you make something like this.
The funtion handleButtonEvent is declared after you are creating a button widget. You can get the desired result by moving the function about the widget code.
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
composer.gotoScene ("menu")
end
end
local myButton = widget.newButton
{
left = 100,
top = 100,
id = "myButton",
label = "Menu",
onEvent = handleButtonEvent
}
myButton.isVisible = true
end

How to change the direction (angle) of bullet after collision in the direction of motion in corona sdk

I want to change the bullet angle after collision, it should change the direction in which it is moving after collision, and must stop rotating after collision.... please give any suggestions... thanks
Sample code is written below
local physics = require("physics")
physics.start()
local obj
physics.setScale( 60 )
physics.setGravity( 0, 0 )
display.setStatusBar( display.HiddenStatusBar )
local obstacle = display.newCircle( 250, 250, 60 )
obstacle.x = display.contentWidth/2; obstacle.y = 200
physics.addBody( obstacle, { density=150, friction=0.2, bounce=0} )
obstacle.isBullet=true
obj = display.newRect(0, 0,20,40)
obj.x = display.contentWidth/2; obj.y = 780
obj.isBullet = true
obj.color = "white"
physics.addBody( obj, { density=1, friction=0.4, bounce=0.1} )
local target = display.newCircle( 250, 250, 60 )
target.x = obj.x; target.y = obj.y; target.alpha = 0
local function Shot( event )
local t = event.target
local phase = event.phase
if "began" == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
t:setLinearVelocity( 0, 0 )
t.angularVelocity = 0
target.x = t.x
target.y = t.y
startRotation = function()
target.rotation = target.rotation + 4
end
Runtime:addEventListener( "enterFrame", startRotation )
local showTarget = transition.to( target, { alpha=0.4, xScale=0.4, yScale=0.4, time=200 } )
myLine = nil
elseif t.isFocus then
if "moved" == phase then
if ( myLine ) then
myLine.parent:remove( myLine ) -- erase previous line, if any
end
myLine = display.newLine( t.x,t.y, event.x,event.y )
myLine:setColor( 255, 255, 255, 50 )
myLine.width = 8
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
local stopRotation = function()
Runtime:removeEventListener( "enterFrame", startRotation )
end
local hideTarget = transition.to( target, { alpha=0, xScale=1.0, yScale=1.0, time=200, onComplete=stopRotation } )
if ( myLine ) then
myLine.parent:remove( myLine )
end
t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )
end
end
return true
end
obj:addEventListener( "touch", Shot)
local physics = require "physics"
physics.start()
local rect1 = display.newRect(480,0,10,320)
physics.addBody(rect1, "static")
local rect2 = display.newRect(20,100,20,20)
local function shot()
physics.addBody(rect2, "dynamic")
rect2:applyForce( 1000, 0, rect2.x, rect2.y )
rect2.myName = "rect2"
end
rect2:addEventListener("touch", shot)

corona sdk storyboard using press button to move next

I'm bundling storyboard and i want user to move to next scene after filling fields and then press a button
I have a problem with the btn ,---------------------------------------------------------------------------------
-- scence2.lua
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
-- BEGINNING OF YOUR IMPLEMENTATION
local widget = require( "widget" )
require("hijacks")
local sysFonts = native.getFontNames()
local tHeight -- forward reference
local roundedRect = display.newRoundedRect( 40, 120, 200, 40, 8 )
roundedRect:setFillColor( 0, 0, 0, 170 )
local t = display.newText( "Waiting for button event...", 0, 0, "AmericanTypewriter-Bold", 18 )
t.x, t.y = display.contentCenterX, 70
local function fieldHandler( textField )
return function( event )
if ( "began" == event.phase ) then
-- This is the "keyboard has appeared" event
-- In some cases you may want to adjust the interface when the keyboard appears.
elseif ( "ended" == event.phase ) then
-- This event is called when the user stops editing a field: for example, when they touch a different field
elseif ( "editing" == event.phase ) then
elseif ( "submitted" == event.phase ) then
-- This event occurs when the user presses the "return" key (if available) on the onscreen keyboard
print( textField().text )
-- Hide keyboard
native.setKeyboardFocus( nil )
end
end
end
-- Predefine local objects for use later
local nameField, phoneField
local fields = display.newGroup()
-- Note: currently this feature works in device builds or Xcode simulator builds only (also works on Corona Mac Simulator)
local isAndroid = "Android" == system.getInfo("platformName")
local inputFontSize = 18
local inputFontHeight = 30
tHeight = 30
if isAndroid then
-- Android text fields have more chrome. It's either make them bigger, or make the font smaller.
-- We'll do both
inputFontSize = 14
inputFontHeight = 42
tHeight = 40
end
nameField = native.newTextField( 40, 120, 200, tHeight )
nameField.font = native.newFont( native.systemFontBold, inputFontSize )
nameField:addEventListener( "userInput", fieldHandler( function() return nameField end ) )
phoneField = native.newTextField( 40, 160, 200, tHeight )
phoneField.font = native.newFont( native.systemFontBold, inputFontSize )
phoneField.inputType = "phone"
phoneField:addEventListener( "userInput", fieldHandler( function() return phoneField end ) )
-- Add fields to our new group
fields:insert(nameField)
fields:insert(phoneField)
-- * Add field labels *
local defaultLabel = display.newText( "الاسم", 250, 120, native.systemFont, 18 )
defaultLabel:setTextColor( 255, 0, 0 )
local defaultLabel = display.newText( "رقم الجوال", 250, 160, native.systemFont, 18 )
defaultLabel:setTextColor( 255, 0, 0 )
-- These are the functions triggered by the buttons
local button1Press = function( event )
t.text = "Button 1 pressed"
end
local button1Release = function( event )
t.text = "Button 1 released"
end
local buttonHandler = function( event )
t.text = "id = " .. event.target.id .. ", phase = " .. event.phase
end
-- This button has individual press and release functions
-- (The label font defaults to native.systemFontBold if no font is specified)
local button1 = widget.newButton{
default = "buttonRed.png",
over = "buttonRedOver.png",
onPress = button1Press,
onRelease = button1Release,
label = "موافق",
emboss = true
}
local button2 = widget.newButton{
default = "buttonBlue.png",
over = "buttonBlue.png",
onEvent = buttonHandler,
onPress = button1Press,
onRelease = button1Release,
label = "ok",
emboss = true;
labelColor = { default = { 51, 51, 51, 255 } },
fontSize = 32,
}
button1.x = 160; button1.y = 320
-- These are the functions triggered by the buttons
local button1Press = function( event )
nameField.text = "Button 1 pressed"
nameField.text = "hello"
storyboard.gotoScene( true, "scene3", "fade", 800 )
end
-- local button1Release = function( event )
-- nameField.text = "Button 1 released"
-- end
local funcction buttonHandler = function( event )
-- nameField.text = "hello"
-- storyboard.gotoScene( true, "scene3", "fade", 800 )
local background = display.newImage("buttonBlue.png", true) -- flag overrides large image downscaling
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
end
local image, text1, text2, text3
local function onSceneTouch( self, event )
if event.phase == "began" then
-- first argument means show native activity indicator while transitioning
--storyboard.gotoScene( true, "scene3", "fade", 800 )
return true
end
end
-- Called when the scene's view does not exist:
function scene:createScene( event )
local screenGroup = self.view
image = display.newImage( "bg2.jpg" )
screenGroup:insert( image )
image.touch = onSceneTouch
text1 = display.newText( "Scene 2", 0, 0, native.systemFontBold, 24 )
text1:setTextColor( 255 )
text1:setReferencePoint( display.CenterReferencePoint )
text1.x, text1.y = display.contentWidth * 0.5, 270
screenGroup:insert( text1 )
text2 = display.newText( "MemUsage: ", 0, 0, native.systemFont, 16 )
text2:setTextColor( 255 )
text2:setReferencePoint( display.CenterReferencePoint )
text2.x, text2.y = display.contentWidth * 0.5, display.contentHeight * 0.5
screenGroup:insert( text2 )
text3 = display.newText( "Touch to continue.", 0, 0, native.systemFontBold, 18 )
text3:setTextColor( 255 ); text3.isVisible = false
text3:setReferencePoint( display.CenterReferencePoint )
text3.x, text3.y = display.contentWidth * 0.5, display.contentHeight - 100
screenGroup:insert( text3 )
print( "\n2: createScene event" )
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
print( "2: enterScene event" )
-- remove previous scene's view
storyboard.purgeScene( "scene1" )
-- Update Lua memory text display
local showMem = function()
image:addEventListener( "touch", image )
text3.isVisible = true
text2.text = text2.text .. collectgarbage("count")/1000 .. "MB"
text2.x = display.contentWidth * 0.5
end
local memTimer = timer.performWithDelay( 1000, showMem, 1 )
end
-- Called when scene is about to move offscreen:
function scene:exitScene()
print( "2: exitScene event" )
-- remove touch listener for image
image:removeEventListener( "touch", image )
-- reset label text
text2.text = "MemUsage: "
end
-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
print( "((destroying scene 2's view))" )
end
-- END OF YOUR IMPLEMENTATION
-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )
-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )
-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )
-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )
return scene it is not move to next scene..
the action works fine, it show testing message..
enter code here

Resources