Getting the text of a clicked display group in Corona - lua

To simplify my problem:
I have the following loop:
local arguments =
{
{ text="foo", x=0, y=0, font=native.systemFont, size=32 },
{ text="bar", x=0, y=0, font=native.systemFont, size=32 }
}
for _,item in ipairs( arguments ) do
local text = display.newText( item.text, item.x, item.y, item.font, item.size )
text:setFillColor( 1 ) -- white
text.x = 50 + 50 * i
text.y = 100
i = i + 1
text:addEventListener( "touch", onTouch )
end
The function onTouch is defined previously and it responds by allowing the user to drag the object around the screen.
The function works fine. However, I would like to be able to access the text of the object the user clicks in from within the onTouch function. For example, if a user clicks on text that contains the string "foo", I would be able to access this string and work with it. Is this possible? I am using Corona Starter (the free one) in case that is relevant.
Thanks in advance.

In short:
Yes, it is possible you should simply declare a variable with a string on the object as such:
text.string = "foo"
And then in your onTouch function you can reach it through
event.target.string

Related

CoronaSDK Touch Events

Currently creating a game with Corona SDK is it possible to have an image and when it is clicked it displays 3 images and once one them 3 images are clicked the score increases by 1. Also Im only a beginner at coding , this is a new language to me. Thanks.
local CButton = display.newImage("+5.jpg" , 100 , 600)
CButton.alpha = 0.5
CButton.name = "CButton"
local CButtonLabel = display.newText( { text = "", x = 0, y = 0, fontSize = 28 } )
CButtonLabel:setTextColor( 0 ) ; CButtonLabel.x = 100 ; CButtonLabel.y = 45
local function touchCListener( event )
local object = event.target
print( event.target.name.." TOUCH on the '"..event.phase.."' Phase!" )
local ChordCOne = display.newImage("+5.jpg", 900,300)
local ChordCTwo = display.newImage("+5.jpg", 1000,300)
local ChordCThree = display.newImage("+5.jpg", 1100,300)
end
--add "touch" listener -- LABEL IS FOR TESTING!
CButton:addEventListener( "touch", touchCListener)
ChordCOne:addEventListener( "touch", updateScore)
CButtonLabel.text = "touch"
Yes, new DisplayObjects can be created in a listener function and listeners can be added to those objects as well.
In your code, you have not added the DisplayObjects created in your listener to any GroupObject (such as scene.view), which will give unexpected results.
Since the variables pointing to the newly created DisplayObjects (ChordCOne, etc.) are local to the function where they are instantiated, you cannot call addEventListener() on them outside the function. You should add the listener when they are created.
Also, the updateScore() listener function isn't defined anywhere. Make sure updateScore is not nil when and wherever you give it as an argument to addEventListener().

Corona SDK display.remove() in lua

this code (lua) gives you a random value from the table "local a" by pressing the text "new". Unfortunately the new random value just appears above the old one. I've tried to remove the old value e.g. with display.remove(mmDis), but it doesn't work.
The second problem is that sometimes I also get back the value "nil" and not only the four entries from the table.
Both things must be easy to solve, but as newbie to lua and working on these small things for almost 4 hours now I just don't get what to change to make it work.
-- references
local mmDis
-- functions
function randomText(event)
display.remove(mmDis)
local a = {"Banana!","Apple!","Potato","Pie"}
com = (a[math.random(0.5,#a)])
local mmDis = display.newText(tostring(com),
display.contentWidth*0.57, display.contentHeight*0.7,
display.contentWidth*0.9, display.contentHeight*0.8, "Calibri", 60)
end
-- menu button
local textnew = display.newText("New", 0, 0, "Calibri", 40)
textnew.x = display.contentWidth*0.2
textnew.y = display.contentHeight*0.9
textnew:addEventListener ("tap", randomText )
It's hard to understand what you are trying to do because when you create textnew you have it in one position and size, while in randomeText() you seem to want to replace that text object with a new one, but you put it at a different pos and size. It appears you want to change the object's text every time you press on tap; in this case, you don't need to replace the text object, you just replace its text.
Also:
you have two "local mmDis", the second one will hide the first, not sure what you're after there
read carefully the docs for math.random
com is already a string so you don't need tostring
Try this code and let me know if this is not what you are after:
local menuTextOptions = {
text = "New",
x = display.contentWidth*0.2,
y = display.contentHeight*0.9,
align = 'left',
font = "Calibri",
fontSize = 40,
}
local textnew = display.newText(menuTextOptions)
-- functions
function randomText(event)
local a = {"Banana!","Apple!","Potato","Pie"}
local com = a[math.random(1,#a)]
textnew.text = com
-- if you want to change position too:
-- textnew.x = display.contentWidth*0.2
-- textnew.y = display.contentHeight*0.9
-- if you want to change size too, but only used for multiline text:
-- textnew.width = display.contentWidth*0.9
-- textnew.height = display.contentHeight*0.8
end
textnew:addEventListener ("tap", randomText )
Sometimes it looks like the button doesn't do anything when you tap but that's because the random number happens to be same as previous, you could put a loop to guard against that. Ig you actually want to change the pos and/or width, then the above code makes it clear where you should do that.
So, that's the basic code (without any extras at the moment :)) how it should be. Big thanks to Scholli!:
local menuTextOptions = {
text = "New",
x = display.contentWidth*0.2,
y = display.contentHeight*0.9,
align = 'left',
font = "Calibri",
fontSize = 40,
}
local textnew = display.newText(menuTextOptions)
local replacement = display.newText(menuTextOptions)
replacement.y = display.contentHeight*0.5
-- functions
function randomText(event)
local a = {"Banana!","Apple!","Potato","Pie"}
local com = a[math.random(1,#a)]
replacement.text = com
end
textnew:addEventListener ("tap", randomText )

How to hide native.newTextField after clicking submit?

I'm trying trying to have users name fade in one letter at a time vertically. Example: Adam "A" would appear after 1 second , "d" would appear after 3 seconds under the displayed A, "a" would appear after 5 seconds under the displayed d, "m" would appear after 7 seconds under the displayed a. The visuals would have a sort of domino effect.When they appear they would stay displayed on screen.
When if I comment out userNameField:removeSelf () then the code works fine. I get the effect I want, but the problem is that I still have the userNamefield showing.
Please let me know if you need to see more code.
local greeting = display.newText( "Greetings, enter your name",0,0,native.systemFont, 20 )
greeting.x = display.contentWidth/2
greeting.y = 100
local submitButton = display.newImage( "submit.png" ,display.contentWidth/2, display.contentHeight - 50 )
local userNameField = native.newTextField( display.contentWidth * 0.5 , 150, 250, 45)
userNameField.inputtype = "defualt"
local incrementor = 0
function showNameLetters()
userNameField:removeSelf( )
if incrementor <= string.len ("userNameField.text") then
incrementor = incrementor + 1
personLetters = string.sub (userNameField.text, incrementor,incrementor)
display_personLetters = display.newText (personLetters, 290,30*incrementor, native.systemFont, 30)
display_personLetters.alpha = 0
transition.to(display_personLetters,{time = 3000, alpha = 1, onComplete = showNameLetters})
end
end
Update:
I've found a solution to my problem, by adding userNameField.isVisible = false in my function.
I've also found something very weird, and wish to have someone explain why this happens. If I add greeting:removeSelf() and submitButton:removeSelf() (I've commented them out in my code below to show you where I put them for testing). I get weird result of only the first letter fading in. If i set greeting.isVisible = false and submitButton.isVisible = false. The code works fine.
I'm so confused why object:removeSelf() wouldn't work. Can someone please clear this up for me.
That is, if I replace the following line:
userNameField:removeSelf( )
with:
userNameField.isVisible = false
then the app works fine. Please suggest me why/ any solution for the question. THanks in advance...
It seems like you're calling the showNameLetters multiple times, this means that you're removing the native text field more than once. Nil it and check for nil before removing it like this:
if userNameField ~= nil then
userNameField:removeSelf()
userNameField = nil
end
It shows that you are using data from userNameField after removing it (in the line below):
personLetters = string.sub (userNameField.text, incrementor,incrementor)
As well as you are calling object:removeSelf() again and again without checking its existance (as mentioned by hades2510). So before removing userNameField, check for it's existance:
if userNameField ~= nil then
userNameField:removeSelf()
userNameField = nil
end
And you won't get userNameField.text while userNameField is nil. So use a temporary variable to hold the previous userNameField.text and get the saved data from that variable when needed.
Extra note: Are you sure, you have to check the length of the text "userNameField.text" in the following line, or the variable userNameField.text? If you have to use the data from the text field, then this will also matter.
if incrementor <= string.len ("userNameField.text") then
........
end
Keep coding....................... :)

How to reveal string one letter at a time?

I'm learning from a book, and this is the assignment question i'm working on:
Create an app that asks for the users name and then displays the name down the side of the screen, one letter at a time.
Clarify what i'm trying trying to do: Have users name fade in one at a time vertically. Example: Adam "A" would appear after 1 second , "d" would appear after 3 seconds under the displayed A, "a" would appear after 5 seconds under the displayed d, "m" would appear after 7 seconds under the displayed a. The visuals would have a sort of domino effect.When they appear they would stay displayed on screen.
So far i'm able to get the user's name and display it side ways. Have it fade it in within 2 seconds. I'm stuck on how to get the letters to fade in one letter at a time.
function submit ()
print( "connect" )
userName = userNameField.text
display_userName = display.newText( userName, display.contentWidth-20, display.contentHeight/2 )
display_userName.rotation = 90
display_userName.alpha = 0
userNameField: removeSelf( )
greeting:removeSelf( )
submitButton:removeSelf( )
transition.fadeIn( display_userName, {time = 2000} )
Please let me know if you need to see more of my code.
You can do it in a simple way as below:
local myString = "Adam" -- Create your string
local positionCount = 0 -- initialize a variable to determine letter position
local function displayData()
positionCount = positionCount + 1
if(positionCount<=string.len(myString))then
-- if positionCount is less than or equal to letters in 'myString'
local letter = string.sub(myString, positionCount, positionCount) -- get the current letter
local letterLabel = display.newText(letter,20,20*positionCount,nil,20) -- place the letter
letterLabel.alpha = 0;
-- display the label and update the function after the completion of transition
transition.to(letterLabel,{time=1000,alpha=1,onComplete=displayData})
end
end
displayData()
Keep Coding.................... :)
Here is the code snippet for storing every character in a table.
Initialise a variable:
check =0;
Here splitWord is an table to store each character of string. and variable "yourStringForOneLetter" is your string variable for splitting. "string.sub" will split string into words using for loop.
if(check==wordSize) then
check=1
end
local wordSize = string.len(yourStringForOneLetter)
splitWord = {}
for i=check, check do
splitWord[i] = string.sub(yourStringForOneLetter, i, i)
check= check +1;
end

How do I reference an image that I have created in an table array in Corona (Lua)?

Apologies for the incredibly noob question, but I'm new to Lua, very very rusty at any code, stuck and can't find the solution!
I'm creating a series of random images on screen using:
for count = 1, 6 do
r = math.random ( 1, 5 )
mpart[count] = display.newImage ("mpart" .. r .. ".png")
mpart[count].y = 680
mpart[count].x = x
mpart[count].spawnednew = false
x = x + 170
mpart[count]:addEventListener ("touch", onTouch)
end
How do I know which object is being touched/moved in the function "onTouch", and how do I add a property to it, e.g.
mpart[1].spawnednew == true
Your onTouch function should have an event parameter passed in. The touched image can then be found by in event.target.
Well first off, lins is spot on about how to reference the touched object: the 'event' parameter of the listener function includes the value 'event.target'
As for adding new data to the touched object, that's as simple as 'event.target.moved = true' and now the object has data at object.moved

Resources