Lua Corona SDK:显示表中的单词

Lua Corona SDK:显示表中的单词,lua,coronasdk,Lua,Coronasdk,好吧,我的答案太模糊了,所以我从这里简单开始。我试图从另一个lua文件(content.lua)中的表中获取一个随机单词。我已经让代码无错误地运行,但无法在屏幕上显示单词或通过命令控制台中的打印。我错过了什么 游戏,卢阿 --lua for game --Loading the local variables --creates the storyboard variable and calls the storyboard api local storyboard = require (

好吧,我的答案太模糊了,所以我从这里简单开始。我试图从另一个lua文件(content.lua)中的表中获取一个随机单词。我已经让代码无错误地运行,但无法在屏幕上显示单词或通过命令控制台中的打印。我错过了什么

游戏,卢阿

--lua for game


--Loading the local variables

--creates the storyboard variable and calls the storyboard api
local storyboard = require ("storyboard")

--calls the mydata.lua module 
local myData = require( "mydata" )

--calls the sfx.lua where sounds are stored 
local sfx = require( "sfx" )
--calls the operations.lua
local operations = require("operations")
local content = require("content")
local playOrder
local wordGraphic
local currQuestion = 1
local homeButton


--tells storyboard to create a new scene
local scene = storyboard.newScene()

function scene:createScene(event)

    local gameScreen = self.view

        --creates a transparent background image centered on the display
    local gameBackground = display.newImage("images/graphics/jungle1.jpg")
        gameBackground.x = display.contentWidth/2
        gameBackground.y = display.contentHeight/2
        gameScreen:insert(gameBackground)

    homeButton = display.newImage("images/buttons/home.png")
        homeButton.alpha = .8
        homeButton.y = 70
        gameScreen:insert(homeButton)

    playOrder = operations.getRandomOrder(#content)

end


local function onHomeTouch(event)
        if event.phase == "began" then
        storyboard.gotoScene("start")
        end
    end 

    function scene:enterScene(event)
    homeButton:addEventListener("touch", onHomeTouch)   
    audio.play(sfx.Bkgd)

    --uses the operations.lua to get words in a random order from the content.lua



    --shows a random word from the content.lua table
    function showWord()
    local word = content[playOrder[currQuestion]].word
    print(word)

        wordGraphic = self.view
        wordGraphic:insert(word)

    wordGraphic.x = display.contentWidth/2
    wordGraphic.y = display.contentHeight/2
    end 
end



    --next question function which clears the screen and loads a new random word


    function scene:exitScene(event)
    homeButton:removeEventListener("touch", onHomeTouch)
    end




function scene:destroyScene(event)

end


--the actual event listeners that make the functions work
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)



return scene
下面是获取随机顺序函数的operations.lua

--operations.lua
module(..., package.seeall)


--function to get a random piece of data
function getRandomOrder(amount)
    local order ={}
    local i
    local temp
    local temp1
    for n = 1,amount do
        order[n] = n
    end
    for i=0,9 do
        for temp = 1,amount do
            n = math.random(1, amount)
            temp1 = order[temp]
            order[temp] = order[n]
            order[n] = temp1
        end
    end
    return order
end 
这就是我试图显示的单词的存储位置。我并没有把它们全部包括在内

--content.lua
return {
    {
        id = "after",
        word = "after"
    },

    {
        id = "again",
        word = "again"
    },

    {
        id = "an",
        word = "an"
    },

    {
        id = "any",
        word = "any"
    },

    {
        id = "ask",
        word = "ask"
    },

    {
        id = "as",
        word = "as"
    },

    {
        id = "by",
        word = "by"
    }
}

您没有在迄今为止显示的任何代码中调用
showWord
。这可能就是为什么它甚至没有打印到控制台。该函数仅包含在
scene:entersecene
中,并退出到外部范围,在调用
entersecene
时将自身定义为全局变量。

为什么
content.lua
采用这种格式?为什么不
返回{'after','reach','any',…}
?我假设
getRandomOrder
类似于一个洗牌函数。我的代码基于一个类似的应用程序。我的应用程序计划的一部分是调用3个要显示的单词,然后播放一个单词的录音。然后,播放机将点击他们听到的单词,如果答案正确,将进入下一个单词。我已尝试通过在enterScene函数中添加return(showWord)来调用此函数,但仍然没有得到任何结果。为什么
return(showWord)
?据我所知,这还不算什么。为什么不直接像
showWord()
那样调用它呢?你有什么理由把它定义为
entersecene
中的一个函数,而不是内联代码吗?好的,Ryan,我刚才用了
showWord()
,正如你所说,这让我现在有了一个错误,所以有了进步!它的意思是尝试索引一个字段“?”,它指的是
local word=content[playOrder[currQuestion]]。showWord函数中的word
听起来像
playOrder[currQuestion]
返回的
“?”
content[“?”]
都不存在。但我不明白这是怎么回事。