how do i make a list app

im new to lua code and im playing around with random things i can do with corona. im trying to figure out how do i make text pop up randomly when i click a button. like lets create a button. i want it to randomize the text that i give it. such as lets say i give it a list of things to say. such as 

1 do the laundry 

2 eat a apple

3 play a game

4 pick up sticks

5 do or die 

6 left or right

how do i get it to randomly print one on the screen but if i click it again it will not choose the same one till all have been chosen, like if i hit the button 3 times it will not choose choice 3 twice intill all have been shown. please help. than k you

You can save all the things you want to say into a table. For example:

local thingsToSay = { "a", "b", "c" }

When your button is tapped, you can get a random index between 1 and then length of the table using:

local index = math.random(1, #thingsToSay)

You can use this to pick a random thing to say. 

local myThingToSay = thingsToSay[index]

You could then remove that element from the table using

table.remove(thingsToSay, idx)

To put it on the screen you could use display.newText()

ok for some reason when i click the button nothing appears. how do i associate the button pressing to the list of things to say(print text on screen). 

First you need to make the display object:

local myText = display.newText( "This is the text object", 100, 200, native.systemFont, 16 )

Then in your tap handler, after you’ve picked out the next thing to say, you can just put:

myText.text = myThingToSay

EDIT: Just looked at your other thread. I’ve made the corrections I think necessary to fix your code. I haven’t tested, however, as I’m currently having trouble with the simulator.

local widget = require("widget") local thingsToSay = { "a", "b", "c", "d", "e", "F" } -----function to handle local function handleButtonEvent( event ) local index = math.random(1, #thingsToSay) local myThingToSay = thingsToSay[index] table.remove(thingsToSay, idx) end ----creating thebutton local myButton = widget.newButton { left = -20, top = 200, width = 350, height = 150, label = "random", onEvent = handleButtonEvent, } Notice that the thingsToSay table must be created before the button event listener, so that the listener knows what the table is.

ok so i did exactly what you suggested. but for some reason it still not showing up at all. i changed the code a little and tried tweaking it as much as possible, i decided to make it more simpler. this is the new code, im not getting any errors, but nothing is still showing up, so i dont know what im still doing wrong. ive been at this almost all day.

[lua]

  1. local widget = require(“widget”)
  2.  
  3. – changing text
  4. local thingsToSay = { “alpha”, “beta”, “charlie”, “delta”, “ecko”, “Foxtrot” }
  5. thingsToSay.x = 100
  6. thingsToSay.y = 100
  7.  
  8. –display
  9. local myText = display.newText( “ThingsToSay”, 100, 200, native.systemFont, 16 )
  10. myText.text =  ThingsToSay
  11.  
  12. – creating the button
  13.  
  14. local button = display.newText(“random”, 50, 50, nil, 26)
  15. button.x = 150
  16. button.y = 400
  17.  
  18.  
  19. – function handler
  20. local function handleButtonEvent( event )
  21.    local index = math.random(1, #thingsToSay)
  22.    local myThingToSay = thingsToSay[index]
  23.    table.remove(thingsToSay, idx)
  24. end
  25.  
  26.  
  27.  – event listener
  28. function screenTap(button)
  29.    local index = math.random(1, #thingsToSay)
  30.   
  31. end
  32.  display.currentStage:addEventListener( “tap”, screenTap )

[/lua]

ok i made my code a little more simpler then what i did. here is the new code.

 

nothing is still being printed out on the screen. i am not even getting any errors. all i am trying to do is make a button, when pressed. it will print a random sentence i give it. but not repeat the sentence till all sentences where printed, once all sentences are printed it will rotate again into the sequence.

 

should i just randomly type each sentence into a new.displaytext. or should i just do it in one group.

Since you stopped making your button with widget.newButton(), it is no longer connected to handleButtonEvent. You can either make it  with widget.newButton() again, or just use:

button:addEventListener("tap", handleButtonEvent)

Your button handler should then look something like this:

local function handleButtonEvent( event ) local index = math.random(1, #thingsToSay) local myThingToSay = thingsToSay[index] myText.text = myThingToSay table.remove(thingsToSay, index) end

You’ll also probably need an if statement to handle the case of being out of thingsToSay items, as math.random(1, 0) will produce an error.

Aslo, sInce you’re assigning index inside your button handler, your screenTap function is kinda redundant. Plus it’s out of scope anyways.

ok i did what you said. i added a if statement and changed it back to widget.newbutton().

this is what i have. i got it to print what only one thing. it stay up on the screen but when i press the button nothing happens. but it does have a limit on how many presses it has since i do have table.remove(list_of_things_to_say, idx). it will do it six times and on the console part. it will show a sequence of numbers counting down from six to one. since i do have a total of six words. but after that it will get a error. also the words are not changing at all. it will stay on alpha. nothing is changing. 

[lua]

local widget = require (“widget”)

----table chart

local list_of_things_to_say = { “alpha”, “beta”, “charlie”, “delta”, “ecko”, “Foxtrot”, }

-----display text

local myText = display.newText( list_of_things_to_say[1], 100, 200, native.systemFont, 16 )

----button

local rndmtxt = widget.newButton

{

    left = 160,

    top = 350,

    id = “newcardbtn”,

    label = “New question?”,

    onEvent = onObjectTouch

}

-----button funtion/event listner

local function onObjectTouch( event )

     if event.phase == “began” then

print (#list_of_things_to_say)

        list_of_things_to_say.text = math.random(1, #list_of_things_to_say)

table.remove(list_of_things_to_say, idx)

     end

        

     return true

end           

rndmtxt:addEventListener( “touch”, onObjectTouch )

[/lua]

Here’s some working code with a few comments that will hopefully be helpful

[lua]

local widget = require (“widget”)
 
----table chart
local list_of_things_to_say = { “alpha”, “beta”, “charlie”, “delta”, “ecko”, “Foxtrot”, }
 
-----display text
local myText = display.newText( “”, 100, 200, native.systemFont, 16 )
 
 
– event handler must be defined before creating button with widget.newButton()
 local function onObjectTouch( event )
    if event.phase == “ended” then
        if #list_of_things_to_say > 0 then
             – calculate random index
             local idx = math.random(1, #list_of_things_to_say)
             – set the text property of your text object to the string at given index of list_of_things_to_say
             myText.text = list_of_things_to_say[idx]
            – remove this string from the table so that it cannot be used again
            table.remove(list_of_things_to_say, idx)
        else – if there’s no more things to say
            myText.text = “No more things to say”
        end
    end
     return true
end           

----button
local rndmtxt = widget.newButton
{
    left = 160,
    top = 350,
    id = “newcardbtn”,
    label = “New question?”,
    onEvent = onObjectTouch
}
 
– widget.newButton() takes care of this bit, so you don’t need to do it.
–rndmtxt:addEventListener( “touch”, onObjectTouch )
 

[/lua]

Nothing was changing before when you tapped the button because you had:

[lua]

list_of_things_to_say.text = math.random(1, #list_of_things_to_say)

[/lua]

you were just giving a table you created a property called “text” and assigning it a random number. The display object you created with display.newText() is what you need to set the text property in. Also, you were just assigning it a random number instead of using a random number as an index to pick out a string from your list of things to say.

yes thank you  now i fully understand thank you so much. nows its a lot more clear with the comments. 

You can save all the things you want to say into a table. For example:

local thingsToSay = { "a", "b", "c" }

When your button is tapped, you can get a random index between 1 and then length of the table using:

local index = math.random(1, #thingsToSay)

You can use this to pick a random thing to say. 

local myThingToSay = thingsToSay[index]

You could then remove that element from the table using

table.remove(thingsToSay, idx)

To put it on the screen you could use display.newText()

ok for some reason when i click the button nothing appears. how do i associate the button pressing to the list of things to say(print text on screen). 

First you need to make the display object:

local myText = display.newText( "This is the text object", 100, 200, native.systemFont, 16 )

Then in your tap handler, after you’ve picked out the next thing to say, you can just put:

myText.text = myThingToSay

EDIT: Just looked at your other thread. I’ve made the corrections I think necessary to fix your code. I haven’t tested, however, as I’m currently having trouble with the simulator.

local widget = require("widget") local thingsToSay = { "a", "b", "c", "d", "e", "F" } -----function to handle local function handleButtonEvent( event ) local index = math.random(1, #thingsToSay) local myThingToSay = thingsToSay[index] table.remove(thingsToSay, idx) end ----creating thebutton local myButton = widget.newButton { left = -20, top = 200, width = 350, height = 150, label = "random", onEvent = handleButtonEvent, } Notice that the thingsToSay table must be created before the button event listener, so that the listener knows what the table is.

ok so i did exactly what you suggested. but for some reason it still not showing up at all. i changed the code a little and tried tweaking it as much as possible, i decided to make it more simpler. this is the new code, im not getting any errors, but nothing is still showing up, so i dont know what im still doing wrong. ive been at this almost all day.

[lua]

  1. local widget = require(“widget”)
  2.  
  3. – changing text
  4. local thingsToSay = { “alpha”, “beta”, “charlie”, “delta”, “ecko”, “Foxtrot” }
  5. thingsToSay.x = 100
  6. thingsToSay.y = 100
  7.  
  8. –display
  9. local myText = display.newText( “ThingsToSay”, 100, 200, native.systemFont, 16 )
  10. myText.text =  ThingsToSay
  11.  
  12. – creating the button
  13.  
  14. local button = display.newText(“random”, 50, 50, nil, 26)
  15. button.x = 150
  16. button.y = 400
  17.  
  18.  
  19. – function handler
  20. local function handleButtonEvent( event )
  21.    local index = math.random(1, #thingsToSay)
  22.    local myThingToSay = thingsToSay[index]
  23.    table.remove(thingsToSay, idx)
  24. end
  25.  
  26.  
  27.  – event listener
  28. function screenTap(button)
  29.    local index = math.random(1, #thingsToSay)
  30.   
  31. end
  32.  display.currentStage:addEventListener( “tap”, screenTap )

[/lua]

ok i made my code a little more simpler then what i did. here is the new code.

 

nothing is still being printed out on the screen. i am not even getting any errors. all i am trying to do is make a button, when pressed. it will print a random sentence i give it. but not repeat the sentence till all sentences where printed, once all sentences are printed it will rotate again into the sequence.

 

should i just randomly type each sentence into a new.displaytext. or should i just do it in one group.

Since you stopped making your button with widget.newButton(), it is no longer connected to handleButtonEvent. You can either make it  with widget.newButton() again, or just use:

button:addEventListener("tap", handleButtonEvent)

Your button handler should then look something like this:

local function handleButtonEvent( event ) local index = math.random(1, #thingsToSay) local myThingToSay = thingsToSay[index] myText.text = myThingToSay table.remove(thingsToSay, index) end

You’ll also probably need an if statement to handle the case of being out of thingsToSay items, as math.random(1, 0) will produce an error.

Aslo, sInce you’re assigning index inside your button handler, your screenTap function is kinda redundant. Plus it’s out of scope anyways.

ok i did what you said. i added a if statement and changed it back to widget.newbutton().

this is what i have. i got it to print what only one thing. it stay up on the screen but when i press the button nothing happens. but it does have a limit on how many presses it has since i do have table.remove(list_of_things_to_say, idx). it will do it six times and on the console part. it will show a sequence of numbers counting down from six to one. since i do have a total of six words. but after that it will get a error. also the words are not changing at all. it will stay on alpha. nothing is changing. 

[lua]

local widget = require (“widget”)

----table chart

local list_of_things_to_say = { “alpha”, “beta”, “charlie”, “delta”, “ecko”, “Foxtrot”, }

-----display text

local myText = display.newText( list_of_things_to_say[1], 100, 200, native.systemFont, 16 )

----button

local rndmtxt = widget.newButton

{

    left = 160,

    top = 350,

    id = “newcardbtn”,

    label = “New question?”,

    onEvent = onObjectTouch

}

-----button funtion/event listner

local function onObjectTouch( event )

     if event.phase == “began” then

print (#list_of_things_to_say)

        list_of_things_to_say.text = math.random(1, #list_of_things_to_say)

table.remove(list_of_things_to_say, idx)

     end

        

     return true

end           

rndmtxt:addEventListener( “touch”, onObjectTouch )

[/lua]

Here’s some working code with a few comments that will hopefully be helpful

[lua]

local widget = require (“widget”)
 
----table chart
local list_of_things_to_say = { “alpha”, “beta”, “charlie”, “delta”, “ecko”, “Foxtrot”, }
 
-----display text
local myText = display.newText( “”, 100, 200, native.systemFont, 16 )
 
 
– event handler must be defined before creating button with widget.newButton()
 local function onObjectTouch( event )
    if event.phase == “ended” then
        if #list_of_things_to_say > 0 then
             – calculate random index
             local idx = math.random(1, #list_of_things_to_say)
             – set the text property of your text object to the string at given index of list_of_things_to_say
             myText.text = list_of_things_to_say[idx]
            – remove this string from the table so that it cannot be used again
            table.remove(list_of_things_to_say, idx)
        else – if there’s no more things to say
            myText.text = “No more things to say”
        end
    end
     return true
end           

----button
local rndmtxt = widget.newButton
{
    left = 160,
    top = 350,
    id = “newcardbtn”,
    label = “New question?”,
    onEvent = onObjectTouch
}
 
– widget.newButton() takes care of this bit, so you don’t need to do it.
–rndmtxt:addEventListener( “touch”, onObjectTouch )
 

[/lua]

Nothing was changing before when you tapped the button because you had:

[lua]

list_of_things_to_say.text = math.random(1, #list_of_things_to_say)

[/lua]

you were just giving a table you created a property called “text” and assigning it a random number. The display object you created with display.newText() is what you need to set the text property in. Also, you were just assigning it a random number instead of using a random number as an index to pick out a string from your list of things to say.

yes thank you  now i fully understand thank you so much. nows its a lot more clear with the comments.