Skip to content

Instantly share code, notes, and snippets.

@figengungor
Last active December 18, 2015 03:28
Show Gist options
  • Select an option

  • Save figengungor/5718016 to your computer and use it in GitHub Desktop.

Select an option

Save figengungor/5718016 to your computer and use it in GitHub Desktop.
Advice Code Utilized. (Elements do no repeat untill all elements in the table are displayed)
local advice = display.newText("Take a Walk", 0, 0, "James Fajardo", 70)
advice: setReferencePoint ( display.CenterReferencePoint )
_W=display.contentWidth
_H=display.contentHeight
advice.x=_W/2
advice.y=_H/2
adviceBox={"Take a Walk", "Choose to Be Happy", "Listen", "Care About Beloved Ones", "Smile", "Listen Good Music", "Don't Give Up", "Imagine" }
colorBox=
{
{255, 215, 0}, --gold
{ 50, 205, 50}, --limegreen
{210, 105, 30}, --chocolatte
}
local buttonSound = audio.loadSound("button.wav")
local adviceButton = display.newRoundedRect(0,0,300,100, 20)
adviceButton: setReferencePoint(display.BottomCenterReferencePoint)
adviceButton.x = _W/2
adviceButton.y = _H-50
local adviceText = display.newText("Tell Me More", 0, 0, nil, 35)
adviceText: setReferencePoint(display.CenterReferencePoint)
adviceText.x = _W/2
adviceText.y=adviceButton.y-adviceButton.height/2
adviceText: setTextColor ( 0, 0, 0 )
adviceText: toFront()
--if you don't wanna change your original table but still wanna do some operations on elements,
--you can copy your table to a brand new table object and return it and do whatever operations you want on it.
local copyTable=function(t)
local ct={}
for i=1,#t do
ct[i]=t[i]
end
return ct
end
local adviceBoxT = copyTable(adviceBox)
--print(table.concat(adviceBoxT))
local chooseRandom=function(event)
if #adviceBoxT==0 then
adviceBoxT = copyTable(adviceBox)
end
--Changing text randomly
local i = math.random(1, #adviceBoxT) -- i is the lucky advice
advice.text=adviceBoxT[i]
--Remove the used adviceBoxT element from the table
table.remove(adviceBoxT,i)
--print(#adviceBoxT)
--Changing background color randomly
local a = math.random(1, #colorBox) -- a is the lucky rgb color code
display.setDefault("background", colorBox[a][1], colorBox[a][2], colorBox[a][3])
--Playing a sound
local buttonChannel = audio.play(buttonSound)
end
adviceButton: addEventListener("tap", chooseRandom)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment