嗨,我是电晕初学者,我将如何在屏幕上随机生成报价
示例:
"首先引用" "第二个引用" "第三次"
当用户点击它时,它只显示这三个
中随机生成的一个引号由于
答案 0 :(得分:1)
您可以按照以下方式执行此操作:
--Create your quote array with quote strings
local myQuoteArray = {"first quote",
"second quote",
"third quote"}
--Display a label and postion it on the screen --
local myLabel = display.newText("Initial string",0,0,nil,14)
myLabel.x = display.contentWidth/2
myLabel.y = display.contentHeight/2
myLabel:setTextColor(255)
--Function for restting the label with any random string from 'myQuoteArray'
local function setLabelString()
--Take any string from the array within its range/count (#myQuoteArray)
--And reset the label/text with the new string/quote
myLabel.text = myQuoteArray[math.random(#myQuoteArray)]
end
Runtime:addEventListener("tap",setLabelString)
保持编码....................:)
答案 1 :(得分:0)
您可以使用以下三个引号定义表:
local quotes= {}
quotes[1] = "first quote"
quotes[2] = "second quote"
quotes[1] = "third quote"
创建标签
local label=display.newText{
text="",
x=0,y=100,
width=100,height=40,
font=native.systemFrom,
fontSize=16
}
初始化随机数生成器
math.randomseed( os.time() )
然后创建一个按钮:
local handleRelease = function(event)
local index=math.random(3)
local quote=quotes[index]
label.text = quote
end
local button = widget.newButton{
x=0, y=0,
width=100, height=40,
label="Press me",
onRelease = handleRelease
}
您需要在场景中调用它才能显示它们。