Calling function within a function ? Getting string from group

Hello

I have 2 questions;

  1. If I have a simple function being called by a “tap” eventlistener such as below:

function myTapListener1( event )
    print( "object tapped = "…tostring(event.target) )
    print(currentscore)
–now kick off another Function

end

But then after that functions complete, I want to execute another function , how do I do it? For example, I have another function triggered when I tap an object which kicks off startGame function:

GO:addEventListener( “tap”, startGame )
   

Essentially I want it to start everytime function myTapListener1 ends.

  1. How do I get the string value from one object in grouped objects, say I have:

num1 = display.newText( RandNum, x, y, native.systemFont, 50 )

num2 = display.newText( RandNum, x, y, native.systemFont, 50 )

num3 = display.newText( RandNum, x, y, native.systemFont, 50 )

Then add it to a group:

numGroup:insert(num1)

numGroup:insert(num2)

numGroup:insert(num3)

From here if I click num1 for example and want the value from num1 which is part of numGroup, I have tried

print(numGroup) but no luck. Also tried numGroup.text.

Obviously print(num1) shows me the value.

Although using an event listener does work for all the objects within the group

numGroup:addEventListener( “tap”, counter)

It just doesn’t get the value from within it, even if I do tonumber(numGroup)

Thanks in advance!

Hi @never3den,

  1. You should just call the new function at the end of the tap listener function. No need to start another event listener (at least not from what I sense you’re trying to do).

  2. One method is to assign a property to each object which you can use to identify it in the tap listener function. For example:

[lua]

local num1 = display.newText( RandNum, x, y, native.systemFont, 50 )

num1.thisItemNumber = 1

[/lua]

Then in the listener function, you can read (and use as necessary):

[lua]

event.target.thisItemNumber

[/lua]

Hope this helps,

Brent

Thanks Brent, to be specific, whats the code I use to call the function at the end of the existing function?

function myTapListener1( event )
    print( "object tapped = "…tostring(event.target) )
    print(currentscore)
–now kick off another Function

end

What would I put in the code above if I want to call a function called counter?

Your suggestion for question 2 helps greatly !

Just add…

[lua]

counter( [args] )

[/lua]

…and make sure that function is properly scoped above and outside (or forward referenced) so Lua sees it.

Brent

Awesome, that worked, thx for your patience

Hi @never3den,

  1. You should just call the new function at the end of the tap listener function. No need to start another event listener (at least not from what I sense you’re trying to do).

  2. One method is to assign a property to each object which you can use to identify it in the tap listener function. For example:

[lua]

local num1 = display.newText( RandNum, x, y, native.systemFont, 50 )

num1.thisItemNumber = 1

[/lua]

Then in the listener function, you can read (and use as necessary):

[lua]

event.target.thisItemNumber

[/lua]

Hope this helps,

Brent

Thanks Brent, to be specific, whats the code I use to call the function at the end of the existing function?

function myTapListener1( event )
    print( "object tapped = "…tostring(event.target) )
    print(currentscore)
–now kick off another Function

end

What would I put in the code above if I want to call a function called counter?

Your suggestion for question 2 helps greatly !

Just add…

[lua]

counter( [args] )

[/lua]

…and make sure that function is properly scoped above and outside (or forward referenced) so Lua sees it.

Brent

Awesome, that worked, thx for your patience