How can I Show and Hide and image with a button

I have a button. I created that button with the wdget.


local widget = require “widget”

buttonBee = widget.newButton{  – Create the button

        defaultFile=“buttonDo.png”, – I’m using this image .png
        onRelease = hideBee     – And onRelease it performs this function “hideBee”
    }
    buttonBee.x = 969 – this is just the location for the image
    buttonBee.y = 408 – location


Then I have the function, so when I release the mouse, it shows the image and plays the sound.


local function hideBee()
        local Bee = display.newImage (“bee.png”)
        doImage.x = 500
        doImage.y = 600
        audio.play(SoundReB5)
    return true
end

-------------------------------------------- everything works great -----------------------

-----This is what I need------------

Now what I need is that when I touch first the mouse, or tap the finger, or touches began or phase = began one of those… the image shows or isVisible or alpha=1

and when I release the mouse or let go my finger, then hide the image or isVisible = false or alpha=0 or just hide the image or maybe image=removeSelf, something, and play the sound.


In a simple way

onTouch = display image

onRelease = hide image and play sound

http://www.youtube.com/watch?v=UKY3GSZRZBU&feature=youtu.be

Thank you for your help

widget.newButton has two events you can take advantage of: onRelease (which you’re already using) and onPress which should happen as soon as the button is touched.

Between those two maybe you have what you need.

 Jay

Hi J.A. – Thank you very much, it works!!!, I was using onTouch, but it is onPress.

Now here is the problem:

I have a button in the ----------------------createScene------------------

buttonDo4 = widget.newButton{
        defaultFile=“whiteKey.png”,
        onPress = buttonDo4,
        onRelease = hideImage
    }
    buttonDo4.x = 141
    buttonDo4.y = 500

the functions are at the top of my program, outside the createScene

onPress I perform this action: – the “buttonDo4” function

local function buttonDo4()
        keyDo4.isVisible = true
        audio.play(soundDo4)
    return true
end

and onRelease I have this function “hideImage”

local function hideImage()
        keyDo4.isVisible = false
    return true
end

------------------------------------ at the very top of the program I have this

local keyDo4 = display.newImage( “keyDo4.png” )
        keyDo4.x = 141
        keyDo4.y = 498
        keyDo4.isVisible = false

When I run the program for the first time the image is not visible.

So when I press the button, I see an Image and hear a sound, good!

when I release the image not showing, so .isVisible = false, good! and I do that as much as I want. great!

------------------------ THE PROBLEM ---------------------------

When i go to another scene, and then I go back to the same scene where I have the button showing and hiding the image;

as soon as I press the button

File: …/Desktop/Music Theory Game - CORONA/hotCrossBuns.lua
Line: 85

Attempt to index upvalue ‘keyDo4’ (a nil value)
 

stack traceback:
    [C]: ?
    …/Desktop/Music Theory Game - CORONA/hotCrossBuns.lua:85: in function ‘_onPress’
    ?: in function <?:135>
    ?: in function <?:218>

Please I need Help

Thanks

Victor

widget.newButton has two events you can take advantage of: onRelease (which you’re already using) and onPress which should happen as soon as the button is touched.

Between those two maybe you have what you need.

 Jay

Hi J.A. – Thank you very much, it works!!!, I was using onTouch, but it is onPress.

Now here is the problem:

I have a button in the ----------------------createScene------------------

buttonDo4 = widget.newButton{
        defaultFile=“whiteKey.png”,
        onPress = buttonDo4,
        onRelease = hideImage
    }
    buttonDo4.x = 141
    buttonDo4.y = 500

the functions are at the top of my program, outside the createScene

onPress I perform this action: – the “buttonDo4” function

local function buttonDo4()
        keyDo4.isVisible = true
        audio.play(soundDo4)
    return true
end

and onRelease I have this function “hideImage”

local function hideImage()
        keyDo4.isVisible = false
    return true
end

------------------------------------ at the very top of the program I have this

local keyDo4 = display.newImage( “keyDo4.png” )
        keyDo4.x = 141
        keyDo4.y = 498
        keyDo4.isVisible = false

When I run the program for the first time the image is not visible.

So when I press the button, I see an Image and hear a sound, good!

when I release the image not showing, so .isVisible = false, good! and I do that as much as I want. great!

------------------------ THE PROBLEM ---------------------------

When i go to another scene, and then I go back to the same scene where I have the button showing and hiding the image;

as soon as I press the button

File: …/Desktop/Music Theory Game - CORONA/hotCrossBuns.lua
Line: 85

Attempt to index upvalue ‘keyDo4’ (a nil value)
 

stack traceback:
    [C]: ?
    …/Desktop/Music Theory Game - CORONA/hotCrossBuns.lua:85: in function ‘_onPress’
    ?: in function <?:135>
    ?: in function <?:218>

Please I need Help

Thanks

Victor