Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save figengungor/5717493 to your computer and use it in GitHub Desktop.
Displaying Objects, Adding Sound, Using Tables and Event Listener in Corona SDK
--James Fajardo is the name of the font that I am using
local advice = display.newText("Take a Walk", 0, 0, "James Fajardo", 70)
--while placing the "advice" display object, regard its center point as a reference
advice: setReferencePoint ( display.CenterReferencePoint )
_W=display.contentWidth --screen's width
_H=display.contentHeight --screen's height
--place the center x and y of the "advice" object to specified points below.
advice.x=_W/2
advice.y=_H/2
--adviceBox table holds some advices as element
adviceBox={"Take a Walk", "Choose to Be Happy", "Listen", "Care About Beloved Ones", "Smile", "Listen Good Music", "Don't Give Up", "Imagine" }
--colorBox table holds rgb tables as element. Rgb tables hold r,g and b values in order.
colorBox=
{
{255, 215, 0}, --gold
{ 50, 205, 50}, --limegreen
{210, 105, 30}, --chocolatte
}
--button sound is loaded first to be played later.
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's color is set to black(rgb code is 0,0,0)
adviceText: setTextColor ( 0, 0, 0 )
--adviceText is brought to front of the adviceButton
adviceText: toFront()
--chooseRandom randomly changes the advice text and background color of the screen and play the button sound.
local chooseRandom=function(event)
--Changing text randomly
local i = math.random(1, #adviceBox) -- i is the lucky advice
advice.text=adviceBox[i]
--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
--chooseRandom function is called when the adviceButton is tapped.
adviceButton: addEventListener("tap", chooseRandom)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment