任何人都可以帮助我。我面临以下问题: 我正在开发一个关于问题和答案的游戏。 我使用widget API添加按钮。 在菜单屏幕(Menu.lua)中,我现在添加了2个按钮。游戏和选项。 一旦我按下它就会进入第一级。
现在的问题如下: 1-一旦我将按钮添加到groupScene。他们消失了。 2-一旦我从groupScene中删除它们就会出现。并将出现在下一个看到的。
所以我应该怎么做。我应该逐一删除它们。还是有其他解决方案。
代码如下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