2014-03-25 69 views
0

嗨,我是在電暈初學者如何將我隨意在屏幕上我怎樣才能使隨機生成的報價

例如生成報價:

「一次報價」 「第二次試舉」 「第三qoute」

並且當用戶輕敲它僅示出一個來自這三個

由於隨機生成的報價

+0

你是什麼意思「引用」? – Dai

+0

你的意思是像[財富](http://en.wikipedia.org/wiki/Fortune_%28Unix%29)? –

回答

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) 

保持編碼.................... :)

+0

感謝它的工作完美 – user3425220

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 
} 

你需要調用它從場景中進行顯示。

相關問題