problems with function call in same function

Hi,

I created my first “game” color guess for my 2 year old kid. The 1st loop works well, but the function call doubles each further loop.

see the code ( it’s in German, i hope you can read it)

[lua]

local RectLinks = display.newRect( 0, 0, display.contentWidth / 2, display.contentHeight )
local RectRechts = display.newRect( display.contentWidth / 2, 0, display.contentWidth, display.contentHeight )

local S_Hallo = audio.loadSound( “halloSami.mp3” )
local S_Nein = audio.loadSound( “nein.mp3” )
local S_Nochmal = audio.loadSound( “nochmal.mp3” )
local S_Super = audio.loadSound( “super.mp3” )

local t = {
{farbe = “rot” , c = {255,0,0} , sound = {audio.loadSound( “rot.mp3” )}},
{farbe = “schwarz” , c = {0,0,0} , sound = {audio.loadSound( “schwarz.mp3” )}},
{farbe = “gelb” , c = {255,255,0} , sound = {audio.loadSound( “gelb.mp3” )}},
{farbe = “gruen” , c = {0,128,0} , sound = {audio.loadSound( “gruen.mp3” )}},
{farbe = “blau” , c = {0,0,255} , sound = {audio.loadSound( “blau.mp3” )}},
{farbe = “lila” , c = {128,0,128} , sound = {audio.loadSound( “lila.mp3” )}}
} --create a table

local richtig_falsch = { “links” , “rechts” }

– was machen wenn richtig
function R ()
print (“Richtig !!!”)
audio.play( S_Super )
timer.performWithDelay( audio.getDuration( S_Super ), function()
print (“Farben Reset”)
RectLinks:setFillColor(0, 0, 0)
RectRechts:setFillColor(0, 0, 0)
end, 1 )

timer.performWithDelay( audio.getDuration( S_Super ), function()
print (“Nochmal”)
audio.play( S_Nochmal )
end, 1 )
timer.performWithDelay( audio.getDuration( S_Super ) + audio.getDuration( S_Nochmal ) , function()
print ("---------------Neustart--------------------")
Raten()
end, 1 )
end

– was machen wenn falsch
function F ()
audio.play( S_Nein )
end
function Raten()
–print ( “raten start”)
– richtig , links oder rechts zuordnen
local richtig = richtig_falsch[math.random( 1,2 )]
print ("richtig ist: " … richtig)

– links und rechts Farben zuordnen
local links = math.random( 1, 6 )
local rechts = math.random( 1, 6 )

– sicherstellen, das sie verschieden sind
while ( rechts == links ) do
rechts = math.random( 1, 6 )
end

– wenn links richtig ist, dann die Funktionen so definieren
if richtig == “links” then

audio.play(t[links].sound[1])
print (" Ansage Farbe")

function RectLinks:touch( event )
if event.phase == “began” then
R()
– return true
end
end

function RectRechts:touch( event )
if event.phase == “began” then
F()
– return true
end
end
else
– wenn rechts richtig ist, dann die Funktionen so definieren
audio.play(t[rechts].sound[1])
function RectLinks:touch( event )
if event.phase == “began” then
F()
– return true
end
end

function RectRechts:touch( event )
if event.phase == “began” then
R()
– return true
end
end

end

– links
RectLinks:setFillColor(t[links].c[1], t[links].c[2], t[links].c[3])
RectLinks:addEventListener( “touch”, RectLinks )

– rechts
RectRechts:setFillColor(t[rechts].c[1], t[rechts].c[2], t[rechts].c[3])
RectRechts:addEventListener( “touch”, RectRechts )

– print ( “raten ende”)
– return true
end

– los

audio.play( S_Hallo, { onComplete=Raten } )
[/lua]
[import]uid: 236876 topic_id: 37585 reply_id: 67585[/import]

Well you repeat the RectLinks and RectRechts functions twice, but this isn’t causing your problem.  The later ones overwrite the earlier ones. If your rectangles overlap, the fact that you’re not returning true from  your event handler could cause the event handler for both rectangles to fire.  Try this:

 

 

 

[lua]

function RectRechts:touch( event )

    if event.phase == “began” then

        R()

    end

    return true – this needs to be on and outside the if statement

end  

[/lua]

 

Well you repeat the RectLinks and RectRechts functions twice, but this isn’t causing your problem.  The later ones overwrite the earlier ones. If your rectangles overlap, the fact that you’re not returning true from  your event handler could cause the event handler for both rectangles to fire.  Try this:

 

 

 

[lua]

function RectRechts:touch( event )

    if event.phase == “began” then

        R()

    end

    return true – this needs to be on and outside the if statement

end  

[/lua]