几周前我才刚开始LUA。我能够创建一个简单的角色,并随着分数跳动,然后使用Corona Simulator运行它。但是我想创建一个菜单系统,以便从main.lua创建并加载menu.lua,然后从此处转到Start(即启动游戏,即game.lua),因此可以玩游戏了。但是它没有出现。我试图添加如下所示的eventListeners o
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
startButton:addEventListener( "tap", gotoGame )
highScoresButton:addEventListener( "tap", gotoHighScores )
end
end
除此之外,我不明白为什么菜单系统不会转到Game.lua,因此我可以玩游戏。另外我也不明白为什么加载menu时为什么会出错。我已经使用了场景和作曲家管理库,从理论上讲应该可以。
图像文件位于一个文件夹中
main.lua 文件1
local composer = require( "composer" )
-- Hide status bar
display.setStatusBar( display.HiddenStatusBar )
-- Seed the random number generator
math.randomseed( os.time() )
-- Go to the menu screen
composer.gotoScene("menu")
Menu.lua 文件2
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()"
-- -----------------------------------------------------------------------------------
local function gotoGame()
composer.gotoScene( "game" )
end
local function gotoHighScores()
composer.gotoScene( "highscores" )
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 title = display.newImageRect( sceneGroup, "title.png", 500, 80 )
title.x = display.contentCenterX
title.y = 200
local startButton = display.newText( sceneGroup, "Start", display.contentCenterX, 700, native.systemFont, 44 )
startButton:setFillColor( 0.82, 0.86, 1 )
local highScoresButton = display.newText( sceneGroup, "High Scores", display.contentCenterX, 810, native.systemFont, 44 )
highScoresButton:setFillColor( 0.75, 0.78, 1 )
startButton:addEventListener( "tap", gotoGame )
highScoresButton:addEventListener( "tap", gotoHighScores )
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
startButton:addEventListener( "tap", gotoGame )
highScoresButton:addEventListener( "tap", gotoHighScores )
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
game.lua 文件3
local composer = require("composer")
local scene = composer.newScene()
display.setStatusBar(display.HiddenStatusBar)
local physics = require "physics"
function updateScore()
score=score+1
scoreText.text = "Score: "..score
scoreText.x, scoreText.y=80,40
end
function onTouch(event)
if(event.phase=="began") then
if(event.x<player.x) then
player:setLinearVelocity(-30,-200)
updateScore()
else
player:setLinearVelocity(30,-200)
updateScore()
end
end
end
-- create()
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared
physics.pause()
score=0
local scoreText
local ground = display.newImage("ground.jpg")
local player = display.newImage("player.png")
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()
scoreText=display.newText("Score:0",0,0,native.systemFont,40)
scoreText.x, scoreText.y=80,40
ground.x=460
ground.y=1300
physics.addBody(ground, "static")
player.x=360
player.y=120
physics.addBody(player)
Runtime:addEventListener("touch", onTouch)
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
答案 0 :(得分:1)
创建场景时
function scene:create( event )
您总是要这样做,将对象(本地对象)插入sceneGroup
sceneGroup:insert( object )
sceneGroup:insert( object2 )
我也怀疑事件监听器是轻触触摸而不是轻按(通过查看重复项):
startButton:addEventListener( "touch", gotoGame )
highScoresButton:addEventListener( "touch", gotoHighScores )
无论如何,我建议您访问documentation:它涵盖了所有内容,而且很有趣。