How can i make random generated quotes - coronasdk

Hi i am beginner in corona how would i randomly generate quote on screen
example:
"first quote"
"second quote"
"third qoute"
and when user taps it shows only one randomly generated quote from those three
Thanks

You can do it as follows:
--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)
Keep Coding.................... :)

You can define an table with these three quotes:
local quotes= {}
quotes[1] = "first quote"
quotes[2] = "second quote"
quotes[1] = "third quote"
Create the label
local label=display.newText{
text="",
x=0,y=100,
width=100,height=40,
font=native.systemFrom,
fontSize=16
}
Init the random number generator
math.randomseed( os.time() )
Then create a button:
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
}
You need to call this from within a scene to get them displayed.

Related

Text is not a valid member of Frame - how do I fix that?

It gives me an error when I execute it "Text is not a valid member of Frame". It's a slider script.
--SLIDER:
local SliderFrame = script.Parent.WalkSpeedSlider
local UIS = game:GetService("UserInputService")
local WalkSpeedSlider = script.Parent.WalkSpeedSlider
local SliderFrame = WalkSpeedSlider.SliderFrame
local Slider = SliderFrame.Slider
local WalkspeedReset = SliderFrame.Reset
local WalkspeedDragging = false
local WalkSpeedDisplay = SliderFrame.WalkSpeedNum
Slider.MouseButton1Down:Connect(function()
WalkspeedDragging = true
end)
UIS.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
WalkspeedDragging = false
end
end)
UIS.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
if WalkspeedDragging == true then
local MouseL = UIS:GetMouseLocation()
local RelativePos = MouseL-SliderFrame.AbsolutePosition
local percent = math.clamp(RelativePos.X/SliderFrame.AbsoluteSize.X,0,1)
Slider.Position = UDim2.new(percent,0,0,0)
game.Players.LocalPlayer.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed = math.round(percent*120)
WalkSpeedDisplay.Text = math.round(game.StarterPlayer.CharacterWalkSpeed)
end
end
end)
I was trying to make a slider which I succeeded, but I wanted my textbox which is WalkSpeedDisplay to be able to change to the speed that I put with the slider, but WalkSpeedDisplay.Text gives me an error "Text is not a valid member of Frame" and I don't know why.
Frames don't have a property known as text. If you want to add text to a frame, insert a text label in the frame and customize it to how you want.
I may be wrong about this, it would be helpful to see all the children and parents of the frame.
Also you probably shouldn't use a variable name two times, such as you did with SliderFrame.

Paste text from hs.chooser in hammerspoon

I am trying to create a shortcut where I store a set of text templates by using hs.chooser. And, the user can paste this by clicking on the drop down from hs.chooser.
I use the below code which displays my template but doesn't paste the text.
Can someone point me what I am doing wrong?
hs.hotkey.bind({"Q"}, "W", function()
local current = hs.application.frontmostApplication()
local chooser = hs.chooser.new(function(choice)
if not choice then focusLastFocused(); return end
hs.pasteboard.setContents(choice["chars"])
focusLastFocused()
hs.eventtap.keyStrokes(hs.pasteboard.getContents())
end)
chooser:queryChangedCallback(function(string)
local choices = {
{
["text"] = "Testing",
["subText"] = "Testing my text"
}
}
chooser:choices(choices)
end)
chooser:searchSubText(true)
chooser:show()
end)
I figured out the answer
-- Focus the last used window.
local function focusLastFocused()
local wf = hs.window.filter
local lastFocused = wf.defaultCurrentSpace:getWindows(wf.sortByFocusedLast)
if #lastFocused > 0 then lastFocused[1]:focus() end
end
-- On selection, copy the text and type it into the focused application.
local chooser = hs.chooser.new(function(choice)
if not choice then focusLastFocused(); return end
hs.pasteboard.setContents(choice["subText"])
focusLastFocused()
hs.eventtap.keyStrokes(hs.pasteboard.getContents())
end)
chooser:choices({
{
["text"] = "Browser\n",
["subText"] = "I used these browsers",
},
{
["text"] = "Device\n",
["subText"] = "I used these devices",
},
})
hs.hotkey.bind({"E"}, "E", function() chooser:show() end)

How to remove a number from a table?

I have no idea why this is not working. I am trying to delete the numbers in the table one by one (the randomCheck picks a number from the table and I want to delete that exact number) when I press the button 'MyButton'
math.randomseed(os.time())
_X = display.contentCenterX
_Y=display.contentCenterY
local numbers = {1,2,3,4,5,6,7,8,9,10}
local randomCheck = numbers[ math.random( #numbers) ]
local text = display.newText(""..randomCheck.."",_X,_Y,"Helvetica",50)
function removeNumber()
for i = 1, 10 do
if(randomCheck == i ) then
table.remove(numbers,i)
text.text = (""..i.."")
end
end
end
myButton = display.newRect(_X,_Y+100,100,80 )
myButton:addEventListener("tap", removeNumber)
In your loop, instead of
if(randomCheck == i)
use
if(randomCheck == numbers[i])
But all of that work is really unnecessary.
Instead of
local randomCheck = numbers[math.random( #numbers)]
use
local randomIndex = math.random(#numbers)
local randomCheck = numbers[randomIndex]
Then you can just
table.remove(numbers, randomIndex)

How do I get a random index of a table in Lua?

How do I get a random question from qus, a table of four questions?
-- qus = table of questions
for i = 1 , 4 do
qus = {}
qus[i] = "what is your name?"
qus[i] = "how old are you?"
qus[i] = "where are you living?"
qus[i] = "what are you doing?"
local label = display.newText(qus[i],160,100)
end
print(qus[i])
-- Prints:
-- what are you doing
-- what are you doing
-- what are you doing
-- what are you doing
I tried this:
qus[1] = "what is your name?"
qus[2] = "how old are you?"
qus[3] = "where are you living?"
qus[4] = "what are you doing?"
label = all qus shows
Thank you for anyone who can help.
Use math.random() function:
local qus = {}
qus[1] = "what is your name?"
qus[2] = "how old are you?"
qus[3] = "where are you living?"
qus[4] = "what are you doing?"
math.randomseed(os.time ()) -- init generator
local index = math.random(#qus) -- between 1 and 4 (size of table)
print(qus[index])

Corona SDK: Displaying Word from Table

OK my first question was too vague so I'm going to start simple here. I am trying to get a random word from a table in another lua file (content.lua). I have gotten the code to run without errors but cannot get a word to display on the screen or via print in the command console. What am I missing?
game.lua
--lua for game
--Loading the local variables
--creates the storyboard variable and calls the storyboard api
local storyboard = require ("storyboard")
--calls the mydata.lua module
local myData = require( "mydata" )
--calls the sfx.lua where sounds are stored
local sfx = require( "sfx" )
--calls the operations.lua
local operations = require("operations")
local content = require("content")
local playOrder
local wordGraphic
local currQuestion = 1
local homeButton
--tells storyboard to create a new scene
local scene = storyboard.newScene()
function scene:createScene(event)
local gameScreen = self.view
--creates a transparent background image centered on the display
local gameBackground = display.newImage("images/graphics/jungle1.jpg")
gameBackground.x = display.contentWidth/2
gameBackground.y = display.contentHeight/2
gameScreen:insert(gameBackground)
homeButton = display.newImage("images/buttons/home.png")
homeButton.alpha = .8
homeButton.y = 70
gameScreen:insert(homeButton)
playOrder = operations.getRandomOrder(#content)
end
local function onHomeTouch(event)
if event.phase == "began" then
storyboard.gotoScene("start")
end
end
function scene:enterScene(event)
homeButton:addEventListener("touch", onHomeTouch)
audio.play(sfx.Bkgd)
--uses the operations.lua to get words in a random order from the content.lua
--shows a random word from the content.lua table
function showWord()
local word = content[playOrder[currQuestion]].word
print(word)
wordGraphic = self.view
wordGraphic:insert(word)
wordGraphic.x = display.contentWidth/2
wordGraphic.y = display.contentHeight/2
end
end
--next question function which clears the screen and loads a new random word
function scene:exitScene(event)
homeButton:removeEventListener("touch", onHomeTouch)
end
function scene:destroyScene(event)
end
--the actual event listeners that make the functions work
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
Here is the operations.lua that gets the random order function
--operations.lua
module(..., package.seeall)
--function to get a random piece of data
function getRandomOrder(amount)
local order ={}
local i
local temp
local temp1
for n = 1,amount do
order[n] = n
end
for i=0,9 do
for temp = 1,amount do
n = math.random(1, amount)
temp1 = order[temp]
order[temp] = order[n]
order[n] = temp1
end
end
return order
end
This is where the words I am attempting to display are stored. I did not include all of them.
--content.lua
return {
{
id = "after",
word = "after"
},
{
id = "again",
word = "again"
},
{
id = "an",
word = "an"
},
{
id = "any",
word = "any"
},
{
id = "ask",
word = "ask"
},
{
id = "as",
word = "as"
},
{
id = "by",
word = "by"
}
}
You're not calling showWord in any of the code that you've shown so far. This is probably why it isn't even printing to the console. The function is just contained within scene:enterScene and exits to the outer scope, defining itself as a global variable when enterScene is called.

Resources