2013-11-27 63 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