Trigger button actions without touch event?

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

[import]uid: 29983 topic_id: 7336 reply_id: 307336[/import]

have you tried eg [lua]mybutton:touch()[/lua]?

[import]uid: 6645 topic_id: 7336 reply_id: 25836[/import]

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? [import]uid: 12108 topic_id: 7336 reply_id: 26003[/import]

You could possibly do something like this:

  
local event = {}  
  
event.name = "touch"  
event.phase = "ended"  
event.x = button.x  
event.y = button.y  
event.target = button  
  
button:dispatchEvent( event )  
  

This assumes that “button” is an already created button object.

Or if you just want to send it as a global event then fire it from the Runtime object:

  
Runtime:dispatchEvent( event )  
  

However I have no idea why you’d want this, I just figured I would post and ask questions later :slight_smile:

P.S. This may not actually work, it’s just a guess. [import]uid: 5833 topic_id: 7336 reply_id: 26017[/import]

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:

[code]
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
[/code] [import]uid: 29983 topic_id: 7336 reply_id: 26145[/import]

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 classfunction newButton( params ) local button, default, over, size, font, textColor, offset -- snip function button:showOver( ) over.isVisible = true endend[/code]Then in your main.lua file you can "touch" a button like this:button3:showOver()[/code]You probably need a hideOver() function as well to return to the default button state.Tim [import]uid: 8196 topic_id: 7336 reply_id: 26157[/import]

Thanks Tim. It works! [import]uid: 29983 topic_id: 7336 reply_id: 26175[/import]