【问题标题】:Corona SDK: Displaying Word from TableCorona SDK:显示表格中的单词
【发布时间】:2013-12-13 19:55:51
【问题描述】:

好的,我的first question 太模糊了,所以我将从这里开始。我正在尝试从另一个 lua 文件(content.lua)中的表中获取一个随机单词。我已经让代码运行没有错误,但无法在屏幕上或通过命令控制台中的 print 显示单词。我错过了什么?

game.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"
    }
}

【问题讨论】:

  • 为什么content.lua是这样的格式?为什么不return {'after', 'again', 'any', ...}?我假设getRandomOrder 类似于随机播放函数。
  • 我的代码基于一个类似的应用程序。我对该应用程序的部分计划是调用要显示的 3 个单词,然后播放一个单词的录音。然后玩家将点击他们听到的单词,如果答案正确,将进入下一个单词。

标签: lua coronasdk


【解决方案1】:

到目前为止,您没有在任何代码中调用showWord。这可能就是为什么它甚至没有打印到控制台的原因。该函数只包含在scene:enterScene 中并退出到外部作用域,在调用enterScene 时将自身定义为全局变量。

【讨论】:

  • 我试图通过在 enterScene 函数中添加 return(showWord) 来调用这个函数,但我仍然没有得到任何东西。
  • 为什么是return(showWord)?据我所知,这不会调用它。为什么不直接称它为showWord()?有什么理由将它定义为enterScene 中的函数,而不是内联代码?
  • 好的,Ryan,我刚刚使用了showWord(),就像你说的那样,现在我遇到了一个错误,所以有进展了!它是说试图索引一个字段'? 指的是showWord函数中的local word = content[playOrder[currQuestion]].word
  • 听起来playOrder[currQuestion] 返回了"?"content["?"] 不存在。
  • 但我不明白这是怎么回事。
猜你喜欢
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 2016-08-01
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多