2011-06-17 70 views
0

我的按钮和场景变化都很糟糕,我的titlescreen场景中的按钮都可以工作并直接进入相应的屏幕,但是它们只会执行一次。所以我无法浏览选项,然后回到标题屏幕,然后再回到选项 - 我不知道为什么?使用Lua和Corona SDK时的场景和群组问题

这里是我的titlescreen文件:

module(..., package.seeall) 

local assetPath = "assets/" 

local mainGroup = display.newGroup() 

function new() 


    local ui = require("ui") 
    local titleScreen = display.newImageRect(assetPath .. "mainMenu.png", display.contentWidth, display.contentHeight) 
    titleScreen.x = display.contentWidth/2 
    titleScreen.y = display.contentHeight/2 
    mainGroup:insert(titleScreen) 

     local onPlayTouch = function(event) 
      if event.phase == "release" then           
      director:changeScene("gameScreen") 
      end 
     end 

     local playButton = ui.newButton{ 
     defaultSrc = assetPath .. "playnowbtn.png", 
     defaultX = 222, 
     defaultY = 62, 
     overSrc = assetPath .. "playnowbtn-over.png", 
     overX = 222, 
     overY = 62, 
     onEvent = onPlayTouch 
     } 

     playButton.x = display.contentWidth/2 
     playButton.y = 50; 
     mainGroup:insert(playButton) 

     local onOptionTouch = function(event) 
      if event.phase == "release" then           
      director:changeScene("optionsScreen") 
      end 
     end 

     local optionButton = ui.newButton{ 
     defaultSrc = assetPath .. "playnowbtn.png", 
     defaultX = 222, 
     defaultY = 62, 
     overSrc = assetPath .. "playnowbtn-over.png", 
     overX = 222, 
     overY = 62, 
     onEvent = onOptionTouch 
     } 

     optionButton.x = display.contentWidth/2 
     optionButton.y = 190; 
     mainGroup:insert(optionButton) 

    return mainGroup 
end 

我的选项文件看起来是这样的:

module(..., package.seeall) 

function new() 

local assetPath = "assets/" 

local localGroup = display.newGroup() 

local background = display.newImage (assetPath .."optionsScreen.png") 
localGroup:insert(background) 

    local onBackTouch = function(event) 
     if event.phase == "release" then           
     director:changeScene("titleScreen") 
     end 
    end 

    local backButton = ui.newButton{ 
    defaultSrc = assetPath .. "playnowbtn.png", 
    defaultX = 222, 
    defaultY = 62, 
    overSrc = assetPath .. "playnowbtn-over.png", 
    overX = 222, 
    overY = 62, 
    onEvent = onBackTouch 
    } 

    backButton.x = display.contentWidth/2 
    backButton.y = display.contentHeight/2 
    localGroup:insert(backbutton) 

    return localGroup 
end 

现在的按钮显示在选择场景和响应触摸,但不会直接回到titlescreen。

我想我对群组感到困惑,只将图像分配给场景而不是整个游戏?

任何人都可以帮助我,谢谢。

编辑:

点击按钮时,我也得到这些运行时错误。

运行时错误 /Users/Lewis/Desktop/proj/optionsScreen.lua:30:错误:表预计。如果这是一个函数调用,则可能使用了'。'而不是':' 堆栈回溯: [C]:?在功能'插入' /用户/路由器/桌面/程序/选项屏幕。 loadScene' /Users/Lewis/Desktop/proj/director.lua:415:in function'changeScene' /Users/Lewis/Desktop/proj/titlescreen.lua:67:in function'onEvent' /Users/Lewis/Desktop/proj/ui.lua:94:功能 ?:功能 运行时错误 /Users/Lewis/Desktop/proj/director.lua:151:尝试调用字段'unloadMe'(一个零值) 堆栈回溯: [C]:in function'unloadMe' /Users/Lewis/Desktop/proj/director.lua:151:in function'_listener' ?:在功能 ?:在功能

回答

0

我不使用director.lua我自己所以我不是100%肯定,但在你的options.lua你把以下两行内的new()函数:

local assetPath = "assets/" 
local localGroup = display.newGroup() 

但是,在你的titlescreen.lua这些行高于new()函数,我认为这就是它的需要。

一般而言,您应该使缩进一致,以便很容易注意哪些代码位于哪个代码块内。

0

在你的错误打印输出中,它要求卸载我的功能。

尝试增加给你的代码(return语句以上),看看它是否有差别:

function unloadMe() 
    print("test") 
end 

,看看是否能摆脱错误的。另一种选择是放弃ui.lua并改用新的小部件库的widget.newButton()函数。这里是文档页面(语法几乎相同,你已经有了,所以不应该有太大的变化需要):

http://developer.anscamobile.com/reference/index/widgetnewbutton