Can anyone help me. i am facing the below issue:
I am developing a game that is about questions and answers.
i used widget API to add buttons.
in menu screen (Menu.lua) i added 2 buttons for now. Play and options.
once i press play it go to the 1st level.
now the problem is as below:
1- once i add the buttons to the groupScene. they disappear.
2- once i remove them from groupScene they will appear. and will appear in the next seen.
so what should i do. should i remove them one by one. or is there another solution.
code as below
1- Main.Lua
`W = display.viewableContentWidth
H = display.viewableContentHeight
local widget = require( "widget")
local composer = require("composer" )
composer.purgeOnSceneChange = true
display.setStatusBar(display.HiddenStatusBar)
-- most commonly used screen coordinates
centerX = display.contentCenterX
centerY = display.contentCenterY
screenLeft = display.screenOriginX
screenWidth = display.contentWidth - screenLeft * 2
screenRight = screenLeft + screenWidth
screenTop = display.screenOriginY
screenHeight = display.contentHeight - screenTop * 2
screenBottom = screenTop + screenHeight
display.contentWidth = screenWidth
display.contentHeight = screenHeight
local bg = display.newRect( 0, 0, 2000,3000 )
bg:setFillColor( .26, .26, .26 )
composer.gotoScene( "Menu",{effect = "slideDown"} )
`
2- Menu.lua
local sceneName = ...
local composer = require( "composer" )
local scene = composer.newScene("Menu")
-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called
-- -----------------------------------------------------------------------------------------------------------------
-- Local forward references should go here
-- -------------------------------------------------------------------------------
local function tapped(event)
local id = event.target.id
if id == "Start" then
composer.gotoScene( "Level1", {effect = "slideDown"} )
print("Level 1")
else
composer.gotoScene( "About_Us", {effect = "slideDown"} )
print("about us")
end
end
-- "scene:create()"
function scene:create( event )
local widget = require( "widget")
local sceneGroup = self.view
-- Initialize the scene here
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
--local title = display.newText( "abed" , centerX, centerY ,"Helvetica", 20 )
------------------------------------------start
startButton = widget.newButton {
id = "Start",
onRelease = tapped,
defaultFile = "button.png",
overFile = "buttonclicked.png",
label = "إبدأ اللعبة",
labelColor = { default={ 1, 1, 1 } },
}
startButton.x=centerX
startButton.y = centerY+-50
--sceneGroup:insert( startButton )
------------------------------------------options
optionbutton = widget.newButton {
id = "options",
onRelease = tapped,
defaultFile = "button.png",
overFile = "buttonclicked.png",
label = "خيارات",
labelColor = { default={ 1, 1, 1 } }
}
optionbutton.x=centerX
optionbutton.y = centerY+50
--sceneGroup:insert( optionbutton )
--local start = display.newImageRect( [parent,], filename, [baseDir,], width, height )
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
-------------------------------------------code
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
-- Called prior to the removal of scene's view
-- 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
3- Level1.lua
local sceneName = ...
local composer = require( "composer" )
local scene = composer.newScene("Level1")
-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called
-- -----------------------------------------------------------------------------------------------------------------
local function tapped(event)
local id = event.target.id
if id == "option1" then
local answerCover = display.newRect( centerX, centerY, 400, 35)
answerCover:setFillColor( .26, .26, .26 )
local title = display.newText( "لقد ربحت" , centerX, centerY ,"Helvetica", 20 )
composer.gotoScene( "About_Us")
end
if id == "Option2" then
local answerCover = display.newRect( centerX, centerY, 400, 35)
answerCover:setFillColor( .26, .26, .26 )
local title = display.newText( "لقد خسرت" , centerX, centerY ,"Helvetica", 20 )
end
if id == "Option3" then
local answerCover = display.newRect( centerX, centerY, 400, 35)
answerCover:setFillColor( .26, .26, .26 )
local title = display.newText( "لقد خسرت" , centerX, centerY ,"Helvetica", 20 )
end
if id == "option4" then
local answerCover = display.newRect( centerX, centerY, 400, 35)
answerCover:setFillColor( .26, .26, .26 )
local title = display.newText( "لقد خسرت" , centerX, centerY ,"Helvetica", 20 )
end
end
-- Local forward references should go here
-- -------------------------------------------------------------------------------
-- "scene:create()"
function scene:create( event )
local widget = require( "widget")
local sceneGroup = self.view
local Q = display.newImageRect( "Questions/Q1.png", 300, 150 )
Q.x = centerX
Q.y = centerY-150
------------------------------------------option1
local option1 = widget.newButton {
id = "option1",
onRelease = tapped,
defaultFile = "button.png",
overFile = "buttonclicked.png",
label = "12",
labelColor = { default={ 1, 1, 1 } }
}
option1.x=centerX
option1.y = screenBottom-180
--sceneGroup:insert(option1)
------------------------------------------option2
local option2 = widget.newButton {
id = "Option2",
onRelease = tapped,
defaultFile = "button.png",
overFile = "buttonclicked.png",
label = "5",
labelColor = { default={ 1, 1, 1 } }
}
option2.x=centerX
option2.y = screenBottom - 130
------------------------------------------option3
local option3 = widget.newButton {
id = "Option3",
onRelease = tapped,
defaultFile = "button.png",
overFile = "buttonclicked.png",
label = "9",
labelColor = { default={ 1, 1, 1 } }
}
option3.x=centerX
option3.y = screenBottom-80
------------------------------------------option4
local option4 = widget.newButton {
id = "option4",
onRelease = tapped,
defaultFile = "button.png",
overFile = "buttonclicked.png",
label = "4",
labelColor = { default={ 1, 1, 1 } }
}
option4.x=centerX
option4.y = screenBottom -30
-- Initialize the scene here
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
-- local title = display.newText( "abed" , centerX, centerY ,"Helvetica", 20 )
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
-- Called prior to the removal of scene's view
-- 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
Related
The output I should get are the chick can jump right when i tap right and vice versa. But the two problems that I can't solve here are when I tap it will output in console like I tap twice. The second problem is the lanes are in array and keep on overlap everything, even the chick which is the player.
For this i include 2 lua files and 3 png.
This is my main.lua
display.setStatusBar( display.HiddenStatusBar )
local composer = require( "composer" )
print("entering gotoScene")
composer.gotoScene( "game2" )
print("out from gotoScene")
This is my game2.lua
---REQUIRES
local composer = require( "composer" )
local scene = composer.newScene()
local widget = require( "widget" )
local physics = require "physics"
physics.start()
physics.setGravity(0,0)
local lanes = {}
local laneID = 1
scroll = 2
---SIZE PHONE DECLARATION
local screenW = display.contentWidth --640
local screenH = display.contentHeight --1136
local halfX = display.contentWidth * 0.5 --half width 320
local halfY = display.contentHeight * 0.5 --half height 568
----------------------
----------------------
---WHEN TAP CHICK MOVE
local function tapListener( event )
local object = event.target
if object.name == "Right Side" then
print( object.name.." TAPPED!" )
if laneID < 3 then
laneID = laneID + 1;
transition.to(chick, {x=lanes[laneID].x,time=50})
print( "At lane "..laneID.." to the right")
end
return true
end
if object.name == "Left Side" then
print( object.name.." TAPPED!" )
if laneID > 1 then
laneID = laneID - 1;
transition.to(chick, {x=lanes[laneID].x,time=50})
print( "At lane "..laneID.." to the left")
end
return true
end
end
----------------------
---CREATE
-- Initialize the scene here.
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
function scene:create( )
local group = self.view
---TAP BACKGROUND
RightSide = display.newRect(500,halfY, halfX+50, screenH + 100 )
RightSide.alpha = 0.1
RightSide.name = "Right Side"
LeftSide = display.newRect(140, halfY,halfX+50, screenH + 100)
LeftSide.alpha = 0.1
LeftSide.name = "Left Side"
----------------------
---TAP LABEL
rightLabel = display.newText({ text = "", x = 0, y = 0 , fontSize = 50 } )
rightLabel:setTextColor( 0 ) ; rightLabel.x = 500 ;rightLabel.y = halfY
leftLabel = display.newText({ text = "", x = 0, y = 0, fontSize = 50 } )
leftLabel:setTextColor( 0 ) ; leftLabel.x = 150 ; leftLabel.y = halfY
----------------------
---PATHWAY (BACKGROUND)
path1 = display.newImageRect("road.png",screenW,screenH)
path1.x = halfX
path1.y = halfY
path2 = display.newImageRect("road.png",screenW,screenH)
path2.x = halfX
path2.y = halfY - screenH
----------------------
---LANES
for i=1,3 do -- loop 3 times to create 3 lanes for our game
--myGroup=display.newGroup()
laneimg = display.newImageRect("lanesroad.png", 150, 1300)
lanes[i] = laneimg
lanes[i].x = (display.contentCenterX - 140*2) + (i*140)
lanes[i].y = display.contentCenterY
lanes[i].id = i
end
----------------------
---CHICK
chick = display.newImageRect("chick.png",100,100)
chick.anchorY = 1
chick.x = lanes[2].x
chick.y = 1000
physics.addBody(chick)
chick.bodyType = "dynamic"
----------------------
path1:toBack();
path2:toBack();
group:insert( RightSide )
group:insert( LeftSide )
group:insert( rightLabel )
group:insert( leftLabel )
group:insert( laneimg)
group:insert( chick )
end
----------------------
---BACKGROUND SCROLL
function pathScroll (self,event)
path1.y = path1.y + scroll
path2.y = path2.y + scroll
if path1.y == screenH * 1.5 then
path1.y = screenH * -.5
end
if path2.y == screenH * 1.5 then
path2.y = screenH * -.5
end
end
----------------------
---SHOW --that will show in scene
function scene:show (event)
---FOR ROAD TO SCROLL
path1.enterFrame = pathScroll
Runtime:addEventListener("enterFrame", pathScroll)
path2.enterFrame = pathScroll
Runtime:addEventListener("enterFrame", pathScroll)
----------------------
---WHEN TAP TO RIGHT
RightSide:addEventListener( "tap", tapListener )
rightLabel.text = "right"
----------------------
---WHEN TAP TO LEFT
LeftSide:addEventListener( "tap", tapListener )
leftLabel.text = "left"
----------------------
end
----------------------
---HIDE
function scene:hide (event)
end
----------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
return scene
enter image description here
enter image description here
The method scene:show is called twice. Once with event.phase paramater equal to will and second time with event.phase paramater equal to did. It also apply to scene.hide method. Code below check that.
At botom I put scene template from Corona documentation. It can be used as start point for new scene.
I commented out line with Runtime:addEventListener("enterFrame", pathScroll). You have two but one is enough.
More information you find with these links
Composer Library
Introducing the Composer API
Tap / Touch / Multitouch
Try (tested)
...
---SHOW --that will show in scene
function scene:show (event)
local phase = event.phase
if ( phase == 'will' ) then
elseif ( phase == 'did' ) then
---FOR ROAD TO SCROLL
path1.enterFrame = pathScroll
Runtime:addEventListener("enterFrame", pathScroll)
path2.enterFrame = pathScroll
--Runtime:addEventListener("enterFrame", pathScroll)
----------------------
---WHEN TAP TO RIGHT
RightSide:addEventListener( "tap", tapListener )
rightLabel.text = "right"
----------------------
---WHEN TAP TO LEFT
--LeftSide:addEventListener( "tap", tapListener )
leftLabel.text = "left"
----------------------
end
end
----------------------
...
Scene template
local composer = require( "composer" )
local scene = composer.newScene()
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
-- -----------------------------------------------------------------------------------
-- 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
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
-- 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
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
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
My storyboard scenes transition to each other and so do my composer scenes. But when I want a storyboard scene to transition to a composer scene the screen goes dark and things stop dead.
Scene one - storyboard:
----------------------------------------
-- firstBar1.lua
----------------------------------------
local storyboard = require "storyboard"
local scene = storyboard.newScene()
function scene:createScene(event)
local screenGroup = self.view
local widget = require "widget"
local function onFirstBar2BtnRelease()
storyboard.gotoScene("firstBar2", "fade", 40)
return true
end
image1 = "images/staveBlankgrey2.png" -- the only button necessary
local firstBar2Btn = widget.newButton{
defaultFile = image1,
width = 480, height = 320,
onRelease = onFirstBar2BtnRelease
}
screenGroup:insert(firstBar2Btn)
-- clef24
image2 = display.newImageRect("images/clef24C.png", 100, 118)
image2.x = display.contentWidth
image2.y = display.contentHeight
image2.x, image2.y = 62, 149
screenGroup:insert(image2)
-- workout count
image3 = display.newImageRect("images/ex1of4.png", 60, 30)
image3.x = display.contentWidth
image3.y = display.contentHeight
image3.x, image3.y = 32, 20
screenGroup:insert(image3)
-- note1
image4 = display.newImageRect("images/crUp.png", 40, 75)
image4.x = display.contentWidth
image4.y = display.contentHeight
image4.x, image4.y = 170, 167
screenGroup:insert(image4)
-- note2
image5 = display.newImageRect("images/crDown.png", 40, 75)
image5.x = display.contentWidth
image5.y = display.contentHeight
image5.x, image5.y = 320, 142
screenGroup:insert(image5)
end
scene:addEventListener("createScene", scene)
return scene
Second scene - composer:
-----------------------------------------------
-- firstBar2.lua
-----------------------------------------------
local composer = require ( "composer" )
local scene = composer.newScene()
local function showEvenBars()
local options = {
effect = "slideLeft",
time = 30,
}
composer.gotoScene( "evenBars", options )
end
-- create scene
function scene:createScene ( event )
local sceneGroup = self.view
end
The code works up to here.
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will") then
local background = display.newImage("images/staveBlankgrey2.png", 240, 160)
note1 = display.newImage( "images/crUp.png", 130, 187)
note2 = display.newImage( "images/crUp.png", 320, 187)
sceneGroup:insert( background )
sceneGroup:insert( note1 )
sceneGroup:insert( note2 )
elseif ( phase == "did") then
timer.performWithDelay(tempo, showEvenBars)
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
end
function scene:destroy( event )
local sceneGroup = self.view
end
scene:addEventListener( "create", scene)
scene:addEventListener( "show", scene)
scene:addEventListener( "hide", scene)
scene:addEventListener( "destroy", scene)
return scene
This second scene, firstBar2 lua doesn't come on screen.
I'd be very grateful for any suggestions.
It is impossible to switch between storyboard and composer scene because they two are different libraries so their structure is different.
If you want your app to run properly you must always use one scene manager.
I recommend Composer.
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
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