【问题标题】:Cannot see buttons in Lua在 Lua 中看不到按钮
【发布时间】:2015-02-17 23:13:19
【问题描述】:

我添加了一个徽标和几个按钮,例如 PLAY 和 CREDITS。我没有收到任何错误,我很难看到问题,我错过了什么?

--Background   
local bg = display.newImage("background.png")  
--Buttons  
local title  
local playBtn  
local creditsBtn

--Functions  
local Main=('')  
local startButtonListeners=('')  

--Start of Functions  
function Main()  
title= display.newImage("logo.png")  
playBtn= display.newImage("playbtn.png", 130, 248)  
creditsBtn= display.newImage("creditsbtn.png", 125, 316)
titleView= display.newGroup(title, playBtn, creditsBtn)    

startButtonListeners("add")  
end

【问题讨论】:

  • 欢迎来到 Stack Overflow。你能描述一下你遇到了什么问题吗?

标签: mobile lua coronasdk


【解决方案1】:

如果这是你的全部代码,你从来没有调用过你的 main 函数,并且在 corona 中,你不必调用 main 函数,main.lua 在你的项目开始时运行。所以试着像这样运行你的代码

--Background   
local bg = display.newImage("background.png")  
--Buttons  
local title  
local playBtn  
local creditsBtn

--Functions  
local Main  
local startButtonListeners, anotherButtonListener

--Start of Functions  
Main = function()  
   title= display.newImage("logo.png")  
   playBtn= display.newImage("playbtn.png", 130, 248)  
   creditsBtn= display.newImage("creditsbtn.png", 125, 316)
   titleView= display.newGroup()    
   titleView:insert(title)
   titleView:insert(playBtn)
   titleView:insert(creditsBtn)

   playBtn:addEventListener("tap", startButtonListeners)
   --creditsBtn:addEventListener("tap", anotherButtonListener)
end

startButtonListeners = function(event)
   --Do something here
end
anotherButtonListener = function(event)
   --Do something for the credits here
end

Main()  --Remember to actually call Main to make it run

在 Lua 中,没有声明 main 函数,它只是按顺序运行所有内容。请记住,您不需要像在 C 中那样编写 main 函数,而是更像 Python,它只会运行您编写的内容。

编辑:您为什么不发布您遇到的错误,以便我们可以更好地帮助您?但是扫描代码肯定会让我失望。新组线。 修改后的代码参考上面。

【讨论】:

  • 非常感谢,这很有帮助。在我调用 Main 之前,我会收到错误消息。
猜你喜欢
  • 1970-01-01
  • 2012-12-31
  • 2015-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多