Corona sdk不查看随机文本

时间:2015-06-06 17:43:57

标签: text random lua corona

我有一个问题,因为当我触摸我的按钮时,有时我的文字没有出现,我不明白出了什么问题。

代码:

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 )

2 个答案:

答案 0 :(得分:0)

要完全了解Corona SDK的 display.newText API,您可以在此处查看:https://docs.coronalabs.com/api/library/display/newText.html

对于点击事件,您可以在此处查看: https://docs.coronalabs.com/api/event/tap/index.html

但我已经为你修复了你的代码。但我真的无法理解为什么你有" Impact" 。但这是一个可以尝试的工作代码:

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。古德勒克队友!

答案 1 :(得分: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 )