2015-06-06 56 views
0

我有一個問題,因爲當我觸摸我的按鈕有時候我的文字沒有出現,我不明白什麼是錯誤的。Corona sdk不能查看隨機文本

代碼:

function randomText(event) 
    display.remove(mmDis) 
    local a = {"Cristiano ronaldo jest najlepszy!", 
       "messi jest dobry!","lewandowski jest ok","diego lopez to bramkarz realu"} 

    com = (a[math.random(1,#a)]) 


    local mmDis = display.newText(tostring(com), 
     display.contentWidth*0.57, display.contentHeight*0.7,    
     display.contentWidth*0.9, display.contentHeight*0.8, "Impact", 30) 
     mmDis.y=20 
     mmDis.x=190 
     mmDis:setFillColor(0, 0, 0, 1) 
     mmDis.anchorY = 0  
end 

play:addEventListener ("tap", randomText) 

    end 

end 
play:addEventListener("touch", object) 
+0

歡迎#1。爲了幫助您獲得答案,我已使用代碼格式按鈕正確格式化您的代碼,並添加了可幫助專家找到您的問題的標籤。 –

回答

0

要充分認識科羅娜SDK的display.newText API,您可以在這裏查看:https://docs.coronalabs.com/api/library/display/newText.html

對於點擊事件您可以在這裏查看: https://docs.coronalabs.com/api/event/tap/index.html

但我已經爲您修復了您的代碼。但我真的不明白你爲什麼在那裏有「影響」。但這裏是一個你可以嘗試的工作代碼:

local mmDis 
local play 
local com 

function randomText(event) 

    if (event.numTaps == 1) then 

     display.remove(mmDis) 
     print("called") 
     local a = {"Cristiano ronaldo jest najlepszy!", 
        "messi jest dobry!","lewandowski jest ok","diego lopez to bramkarz realu"} 

     com = (a[math.random(1,#a)]) 


     mmDis = display.newText(tostring(com),0,0, native.systemFont, 9) 
     mmDis.x = display.contentWidth/2 
     mmDis.y = display.contentHeight/2 + 50 

     print(com) 
    end 
end 

    play = display.newImageRect("images/bg_btn_play.png", 102, 48) -- THIS IS MY BUTTON (CHANGE THIS TO YOUR BUTTON) 
    play.x = display.contentWidth/2 
    play.y = display.contentHeight/2 


play:addEventListener ("tap", randomText) 

快樂編碼和歡迎來到Stackoverflow。 Goodluck隊友!

0

試試這個:

local mmDis 
local play 
local com 

    local a = {"Cristiano ronaldo jest najlepszy!", 
       "messi jest dobry!","lewandowski jest ok","diego lopez to bramkarz realu"} 

    mmDis = display.newText("",0,0, native.systemFont, 9) 
    mmDis.x = display.contentWidth/2 
    mmDis.y = display.contentHeight/2 + 5 

play = display.newImageRect("images/bg_btn_play.png", 102, 48) 
play.x = display.contentWidth/2 
play.y = display.contentHeight/2 function randomText(event) 
local function randomText (event) 
    if event.phase == "ended" then 
    if (event.numTaps == 1) then 
     com = math.random(1,#a) 
     print(com) 
     mmDis.text = a[com] 
    end 
    return true 
end  
play:addEventListener ("touch", randomText)