How to Loop function in a function

Hello guys,

I’m a newbie to corona sdk trying to create a android game app ,i need some help from you guys

I’m trying to create a small game app where i need to toggle between images on touch

for e.g i have two images image1.png,image2.png

Both the images are displayed in the same position alternatively.

When game starts only image1.png will be displayed and image2.png will be invisible.

Image2.png will be displayed when image1.png is clicked, once this click happens … image2.png is displayed and image1.png will become invisible.

This cycle continues, its like a toggle effect where a user can toggle between two images for ‘n’ number of times

i’m struck at a phase where when i can display both the images only for the 1st time but I’m unable to display it for the next time…Dont know how to code further…i’m very new to corona sdk so please help me by altering my code or sending a new code or any reference links

Here is my lua code

object = display.newImage( "dice.png",150,150 ) object.xScale = 1 object.yScale = 1 object.id = "dice1" object.isVisible = true function object:touch( event ) if event.phase == "began" then print "Dice one rolled on " object.isVisible = false objectt = display.newImage( "dice2.png",345,150 ) objectt.xScale = 1 objectt.yScale = 1 function objectt:touch( event ) if event.phase == "began" then objectt.isVisible = false objectt = display.newImage("dice.png",150,150) objectt.xScale = 1 objectt.yScale = 1 print "Dice two rolled on " end return true end objectt:addEventListener( "touch", objectt ) end return true end object:addEventListener( "touch", object )

Waiting for quick reply

Regards

saideep

Unfortunately in Lua you can’t change an image once you’ve declared it to a variable. You would have to remove it and re-do it. But for your case you don’t have to do such a thing.

dice1 = display.newImage("dice1.png", 150, 150 ) dice1.id = 1   dice2 = display.newImage("dice2.png", 150, 150 ) dice2. id = 2 dice2.isVisible = false   function changeDice(event)      if event.phase == "ended" then         local focus = event.target          if focus.id == 1 then dice1.isVisible = false dice2.isVisible = true          elseif focus.id == 2 then dice1.isVisible = true dice2.isVisible = false         end      end end   dice1:addEventListener( "touch", changeDice ) dice2:addEventListener( "touch", changeDice )

Haven’t tested it out but should work

Can I suggest you attach the listener to a group instead?

local dieGroup = display.newGroup() display.newImage( dieGroup  "dice1.png", 150, 150 ) display.newImage( dieGroup  "dice2.png", 150, 150 )  dieGroup[2].isVisible = false local function onTouchDie( self, event )    local phase = event.phase    if(phase == "ended") then -- Toggle both dice       self[1].isVisible = not self[1].isVisible       self[2].isVisible = not self[2].isVisible    end    return true end dieGroup.touch = onTouchDie dieGroup:addEventListener( "touch", dieGroup )  

Thanks @roaminggamer and @hatethinkingofnames for the prompt replies…we’ll get back to you…

Here is what I do in my apps:

[lua]

local soundFxOnSwitch, soundFxOffSwitch

local function soundToggle(event)
    if event.phase == “ended” then
        if gamesettings.soundOn then
            gamesettings.soundOn = false
            soundFxOnSwitch.isVisible = false
            soundFxOffSwitch.isVisible = true
        else
            gamesettings.soundOn = true
            soundFxOnSwitch.isVisible = true
            soundFxOffSwitch.isVisible = false
        end
    end
    return true
end

    soundFxOnSwitch = display.newImageRect(“images/switch_on.png”, 48, 36)
    soundFxOnSwitch.x = display.contentCenterX + 100
    soundFxOnSwitch.y = display.contentCenterY
    
    soundFxOffSwitch = display.newImageRect(“images/switch_off.png”, 48, 36)
    soundFxOffSwitch.x = display.contentCenterX + 100
    soundFxOffSwitch.y = display.contentCenterY

    soundFxOnSwitch:addEventListener(“touch”, soundToggle)
    soundFxOffSwitch:addEventListener(“touch”, soundToggle)

    if gamesettings.soundOn then
        soundFxOnSwitch.isVisible = true
        soundFxOffSwitch.isVisible = false
    else
        soundFxOnSwitch.isVisible = false
        soundFxOffSwitch.isVisible = true
    end

[/lua]

Unfortunately in Lua you can’t change an image once you’ve declared it to a variable. You would have to remove it and re-do it. But for your case you don’t have to do such a thing.

dice1 = display.newImage("dice1.png", 150, 150 ) dice1.id = 1   dice2 = display.newImage("dice2.png", 150, 150 ) dice2. id = 2 dice2.isVisible = false   function changeDice(event)      if event.phase == "ended" then         local focus = event.target          if focus.id == 1 then dice1.isVisible = false dice2.isVisible = true          elseif focus.id == 2 then dice1.isVisible = true dice2.isVisible = false         end      end end   dice1:addEventListener( "touch", changeDice ) dice2:addEventListener( "touch", changeDice )

Haven’t tested it out but should work

Can I suggest you attach the listener to a group instead?

local dieGroup = display.newGroup() display.newImage( dieGroup  "dice1.png", 150, 150 ) display.newImage( dieGroup  "dice2.png", 150, 150 )  dieGroup[2].isVisible = false local function onTouchDie( self, event )    local phase = event.phase    if(phase == "ended") then -- Toggle both dice       self[1].isVisible = not self[1].isVisible       self[2].isVisible = not self[2].isVisible    end    return true end dieGroup.touch = onTouchDie dieGroup:addEventListener( "touch", dieGroup )  

Thanks @roaminggamer and @hatethinkingofnames for the prompt replies…we’ll get back to you…

Here is what I do in my apps:

[lua]

local soundFxOnSwitch, soundFxOffSwitch

local function soundToggle(event)
    if event.phase == “ended” then
        if gamesettings.soundOn then
            gamesettings.soundOn = false
            soundFxOnSwitch.isVisible = false
            soundFxOffSwitch.isVisible = true
        else
            gamesettings.soundOn = true
            soundFxOnSwitch.isVisible = true
            soundFxOffSwitch.isVisible = false
        end
    end
    return true
end

    soundFxOnSwitch = display.newImageRect(“images/switch_on.png”, 48, 36)
    soundFxOnSwitch.x = display.contentCenterX + 100
    soundFxOnSwitch.y = display.contentCenterY
    
    soundFxOffSwitch = display.newImageRect(“images/switch_off.png”, 48, 36)
    soundFxOffSwitch.x = display.contentCenterX + 100
    soundFxOffSwitch.y = display.contentCenterY

    soundFxOnSwitch:addEventListener(“touch”, soundToggle)
    soundFxOffSwitch:addEventListener(“touch”, soundToggle)

    if gamesettings.soundOn then
        soundFxOnSwitch.isVisible = true
        soundFxOffSwitch.isVisible = false
    else
        soundFxOnSwitch.isVisible = false
        soundFxOffSwitch.isVisible = true
    end

[/lua]