2017-01-02 22 views
1

我遇到了與我的表有關的問題。我無法將表格連接到我的其他lua文件。我在本地lblGiven = display.newText上有錯誤。它顯示Error bad argument#-1 newText。該程序的機制是如果您單擊按鈕,表格的一個部分將顯示爲標籤。Corona sdk將表連接到其他lua file.Error錯誤參數#-1 newText

這是我的文件名爲questions.lua

local M = { 

{ 
    "What is the answer 1", 
    answer = "17", 

}, 
{ 
    "What is the answer 2", 
    answer = "18", 

}, 
{ 
    "What is the answer 3", 
    answer = "25", 

}, 
}, 

return M 

這是我main.lua

local given = require("questions") 

local lblGiven = display.newText(
    { 
     text = given[math.random(#given)], 
     x = 160, 
     y = 310, 
     font = native.systemFont, 
     align = "center" 
    } 
) 

回答

1

嘗試

local M = { 

{ 
    "What is the answer 1", 
    answer = "17", 

}, 
{ 
    "What is the answer 2", 
    answer = "18", 

}, 
{ 
    "What is the answer 3", 
    answer = "25", 

}, 
} -- remove comma 

return M 

local given = require("questions") 

local lblGiven = display.newText(
    { 
     text = given[math.random(#given)][1], 
     x = 160, 
     y = 310, 
     font = native.systemFont, 
     align = "center" 
    } 
) 

指令given[math.random(#given)]給你表格。例如given[1]等於{"What is the answer 1", answer = "17"}。要得到唯一的問題,你需要使用方括號和索引。閱讀更多關於Understanding lua tables in corona sdk的表格。