Trigger button actions without touch event?

Is it possible to trigger button functions(rollover and sounds) without a touch event?

have you tried eg mybutton:touch()?

Is it possible to trigger button functions(rollover and sounds) without a touch event?

Why?

As in, if you don't want a touch event, then why are you making a button?

You could possibly do something like this:

1
2
3
4
5
6
7
8
9
local event = {}
 
event.name = "touch"
event.phase = "ended"
event.x = button.x
event.y = button.y
event.target = button
 
button:dispatchEvent( event )

Thanks everyone for the feedback. I'm trying to make a simple Simon Says type game where the app plays a sequence of sound / rollovers and the user tries to repeat the pattern. I added sounds to the basic button demo file and I'm trying to play an initial pattern using the timer that the user can mimic. I can get the initial sounds to play right now, dispatch event is firing, but I'm still trying to figure out how to trigger the rollover states with it's associated sound. I tried using button3:touch() and a few other things. Maybe it's a scoping issue? Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
local ui = require("ui")
 
local background = display.newImage("bg.jpg", true)
background.x = display.contentWidth / 2
background.y = display.contentHeight / 2
 
local roundedRect = display.newRoundedRect( 10, 50, 300, 40, 8 )
roundedRect:setFillColor( 0, 0, 0, 170 )
 
local t = ui.newLabel{
        bounds = { 10, 55, 300, 40 },
        text = "Waiting for button event...",
        font = "AmericanTypewriter-Bold",
        textColor = { 255, 204, 102, 255 },
        size = 18,
        align = "center"
}
 
--load sounds
local beep = audio.loadSound("sound1.wav")
local poof = audio.loadSound("sound2.wav")
local impact = audio.loadSound("sound3.wav")
 
--button functions setup
local button1Press = function(event) 
        audio.play(sound1)
end
 
local buttonHandler = function( event )
        if event.phase == "press" then
        if event.id == "button2" then audio.play(sound2) else audio.play(sound3) end
        end
        t:setText( "id = " .. event.id .. ", phase = " .. event.phase )
end
 
local bt1 = ui.newButton {
        default = "buttonRed.png",
        over = "buttonRedOver.png",
        onPress = button1Press
}
 
local button2 = ui.newButton{
        default = "buttonYellow.png",
        over = "buttonYellowOver.png",
        onEvent = buttonHandler,
        id = "button2"
}
 
local button3 = ui.newButton{
        default = "buttonGray.png",
        over = "buttonBlue.png",
        onEvent = buttonHandler,
        id = "button3"
}
 
-- Setup listener
local myListener = function( event )
                -- --------------------------trigger rollover state
                --button3:buttonHandler( "press" )
                --self:touch()
end
 
--initial sound logic
local soundID = media.newEventSound( "sound3.wav" )
 
local playBeep = function()
    media.playEventSound( soundID )
        local event = { name="touch", phase="ended", target=button3 }
        button3:dispatchEvent( event )
        --button3:touch()
end
 
timer.performWithDelay( 1000, playBeep, 3 )
 
bt1.x = 160; bt1.y = 160
button2.x = 160; button2.y = 240
button3.x = 160; button3.y = 320

Since you're using the UI button library you can modify ui.lua to include a function that toggles the visibility of the button's "over" graphic:

---------------
-- Button class
 
function newButton( params )
        local button, default, over, size, font, textColor, offset
         
        -- snip
         
        function button:showOver(  )
               over.isVisible = true
        end
 
end

Thanks Tim. It works!

views:1940 update:2011/10/6 9:28:12
corona forums © 2003-2011