Corona sdk随机文字

时间:2015-06-03 16:17:14

标签: text random sdk 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)

local mmDis 
function randomText(event)

if event.phase == "began" then 
 if mmDis then 
   display.remove(mmDis)
 end 

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

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


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  
end 

  play:addEventListener ("tap", randomText )



 end

end
play:addEventListener( "touch", object )

试试这个并看看。

答案 1 :(得分:0)

确保正确本地化变量。您需要在函数外部声明“mmDis”,然后不要在函数内部对它进行本地化。像这样的东西

local mmDis* -- this will ensure code from here on out all use the SAME "mmDis"
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)])

    -- NOTICE how I removed the local keyword below.
    -- This will ensure your code is talking about the same "mmDis" 
    -- you declared before your function
    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 )

最终,您遇到的问题是您没有删除屏幕上的旧文本,因为mmDis已在randomText函数中本地化。确保使用适当的范围对变量进行本地化。这里有一个很好的阅读http://lua-users.org/wiki/ScopeTutorial