2013-11-27 35 views
0

好吧,我的first question太模糊了,所以我要从这里开始简单。我试图从另一个lua文件(content.lua)中的表格中获取一个随机单词。我已经获得了代码运行没有错误,但不能让一个字显示在屏幕上或通过在命令控制台中打印。我错过了什么?Corona SDK:从表格显示Word

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 
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 

这是我试图显示字存储在operations.lua。我没有包括所有这些。

--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" 
    } 
} 
+0

为什么'content.lua'采用这种格式?为什么不'返回''后','再'','任何',...}'?我假设'getRandomOrder'就像洗牌功能。 –

+0

我已将我的代码从类似的应用程序中删除。我的部分应用程序计划是调用3个单词来显示,然后播放一个单词的录音。然后,玩家将点击他们听到的单词,如果答案正确,就会进入下一个单词。 – user3029068

回答

0

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

+0

我试图通过在enterScene函数中添加return(showWord)来调用这个函数,但我仍然没有得到任何东西。 – user3029068

+0

为什么'return(showWord)'?就我所知,这不会称之为。为什么不把它叫做'showWord()'?是否有任何理由将它定义为'enterScene'中的函数,而不是将代码嵌入? –

+0

好吧,瑞恩,我刚刚使用了'showWord()',因为你说过,现在我得到了一个错误,所以有进展!这是说试图索引一个字段'?' 这是指在showWord函数中的本地词=内容[playOrder [currQuestion]] .word' – user3029068