Button question

I am still new to lua so here is my question. It is possible to add code for rollover image to this code below without ui.lua file ?

Thanks

Here is my code:

local beep = display.newImage (“image.png”)
beep.x = 100
beep.y = 100
localGroup:insert(beep)

local beepsound = media.newEventSound( “ch1.mp3” )

local beepsound = function( event )
if event.phase == “ended” then
media.playEventSound( beepsound )
end
end
beep:addEventListener(“touch”, beepsound) [import]uid: 22037 topic_id: 11584 reply_id: 311584[/import]

Please post your code within lua tags, it’s just good manners :wink:

As to doing it without ui, sure, it’d be possible - but it would also be rather a lot of work.

Is there a reason you do not want to use ui.lua?

Peach :slight_smile: [import]uid: 52491 topic_id: 11584 reply_id: 42087[/import]

Something like this should work:
[lua]local beep = display.newImage (“image.png”)
local beepRoll = display.newImage (“rollover.png”)

beep.x = 100
beep.y = 100

local beepX = beep.x
local beepY = beep.y

beepRoll.x = beepX
beepRoll.y = beepY
beepRoll.isVisible = false

localGroup:insert(beep)
localGroup:insert(beepRoll)

local beepsound = media.newEventSound( “ch1.mp3” )

local beepsound = function( event )
if event.phase == “began” then
beep.isVisible = false
beepRoll.isVisible = true
elseif event.phase == “ended” then
beep.isVisible = true
beepRoll.isVisible = false
media.playEventSound( beepsound )
end
end
beep:addEventListener(“touch”, beepsound)[/lua] [import]uid: 27965 topic_id: 11584 reply_id: 42106[/import]

Thank You calebr2048 for that code.

I also tried with ui file but wasn’t sure where code for audio files should go?

Here is my code.

local button1 = ui.newButton{
default = “buttonFire.png”,
over = “buttonFireOver.png”,
onEvent = buttonHandler,
id = “button1”,
text = “Fire”,
font = “Trebuchet-BoldItalic”,
textColor = { 51, 51, 51, 255 },
size = 30,
emboss = true
}

local button2 = ui.newButton{
default = “buttonWind.png”,
over = “buttonWind.png”,
onEvent = buttonHandler,
id = “button2”,
text = “Wind”,
font = “Trebuchet-BoldItalic”,
size = 30,
emboss = true

}
button1.x = 95; button2.y = 55
button2.x = 95; button3.y = 228

[import]uid: 22037 topic_id: 11584 reply_id: 42215[/import]

Don’t forget to put your code in lua tags [text][lua]past code between lua tags like this[/lua][/text]. It’s makes code infinitely easier to read.

ui.lua allows you to call a function using onPress and onRelease.
[lua]local beepsound = media.newEventSound( “ch1.mp3” )

local button1Release = function( event )
media.playEventSound( beepsound )
– You can put whatever code you want to execute when the button
– is released within this function
end

local button1 = ui.newButton{
default = “buttonFire.png”,
over = “buttonFireOver.png”,
onRelease = button1Release, – This line calls the function button1Release
onEvent = buttonHandler,
id = “button1”,
text = “Fire”,
font = “Trebuchet-BoldItalic”,
textColor = { 51, 51, 51, 255 },
size = 30,
emboss = true
}

local button2 = ui.newButton{
default = “buttonWind.png”,
over = “buttonWind.png”,
onEvent = buttonHandler,
id = “button2”,
text = “Wind”,
font = “Trebuchet-BoldItalic”,
size = 30,
emboss = true

}

button1.x = 95; button2.y = 55
button2.x = 95; button3.y = 228[/lua] [import]uid: 27965 topic_id: 11584 reply_id: 42224[/import]

Thank you calebr2048 again for explanation and code.
Sorry for lua tags. [import]uid: 22037 topic_id: 11584 reply_id: 42259[/import]

I wrote a tutorial article + video called Better Buttons in Corona SDK that shows how to do roll-over type stuff without the ui.lua library – you have to join Game Dev Nation to get the tutorial, but it’s free.

http://GameDevNation.com

Jay
[import]uid: 9440 topic_id: 11584 reply_id: 42260[/import]

Yee, yee I know its me again, but that forum is so helpful that can’t resist to ask again for help :slight_smile:

…after my button is pressed I would like to change scene using director.lua and make that button invisible on scene1.
I did that on project without ui, but I prefer with ui.
Here is my code everything works (sound, rollover) but no scene change action.

Thanks for help.

[lua]module(…, package.seeall)
local ui = require(“ui”)
local director = require (“director”)
display.newGroup()
function new()
local localGroup = display.newGroup()
local beepsound = media.newEventSound( “ch1.mp3” )
local button1Release = function( event )
media.playEventSound( beepsound )

end
local background = display.newImage (“background.png”)
localGroup:insert(background)

local button1 = ui.newButton{
default = “fire.png”,
over = “fireroll.png”,
onRelease = button1Release, button1Release
onEvent = buttonHandler,
id = “button1”,
text = “Fire”,
font = “Trebuchet-BoldItalic”,
textColor = { 51, 51, 51, 255 },
size = 20,
emboss = true
}

button1.x = 95; button1.y = 55

local function touchstart (event)
if event.phase == “ended” then
director:changeScene (“scene2”, “overFromRight”)
end
end[/lua] [import]uid: 22037 topic_id: 11584 reply_id: 42649[/import]

You are a gentleman and a scholar, thanks for using lua tags! :slight_smile:

There are a few different ways to accomplish changing a scene with director when using ui.lua. What I would recommend (although I wouldn’t claim it’s the best way but it seems appropriate for your needs) is to use a timer in the button1Release function that will call the touchstart function. The timer should be set to the length of time of the beepsound (ch1.mp3) so that the sound will finish playing before the scene changes. As is you can’t call the touchstart function from the button1Release function unless you use the glorius and wonderful thing known as “forward referencing”. I won’t go in to detail about forward referencing since Jon Beebe has a wonderful article on the topic already here:
http://jonbeebe.net/post/3789692325/forward-referencing-will-save-you
You don’t actually need the if statement in the touchstart function because the button has already bean tested as released in the ui.lua. [import]uid: 27965 topic_id: 11584 reply_id: 42653[/import]

Thanks for help and interesting article. I removed sound from my button, won’t play with timers yet. I’m still trying change scene after button had been released with no success I cant figure out what.

[lua]local button1 = ui.newButton{
default = “button1.png”,
over = “button1over.png”,
onRelease = button1Release,
id = “button1”,
text = “”,
font = “Trebuchet-BoldItalic”,
textColor = { 1, 1, 1 , 100 },
size = 30,
emboss = true
}

button1.x = 70; button1.y = 420

localGroup:insert(button1)
localGroup.button = button1

function loadGame(event) screen
if event.phase == “ended” then
director:changeScene(“scene2”,“moveFromLeft”)
end
end
button:addEventListener(“touch”, loadGame)

localGroup:insert(button1)
button1:addEventListener(“touch”,button1)[/lua] [import]uid: 22037 topic_id: 11584 reply_id: 42841[/import]

Do you still have the button1Release function somewhere (I don’t see it in your posted code)? The function loadGame has to be before the onRelease function in the code (unless you use forward referencing) otherwise the onRelease function will not be able to call it and the console will throw up an error. [import]uid: 27965 topic_id: 11584 reply_id: 42849[/import]

Here is my code I have rollover on my button, but not sure where to place code for scene change (director:changeScene)

[lua]module(…, package.seeall)
local ui = require(“ui”)
local director = require(“director”)

function new()
local localGroup = display.newGroup()

local background = display.newImage (“menubackground.png”)
localGroup:insert(background)

local button1Release = function( event )
end

local button1 = ui.newButton{
default = “button1.png”,
over = “button1over.png”,
onRelease = button1Release,
onEvent = buttonHandler,
id = “button1”,
text = “”,
font = “Trebuchet-BoldItalic”,
textColor = { 1, 1, 1 , 100 },
size = 30,
emboss = true
}

button1.x = 70; button1.y = 420

return localGroup
end[/lua] [import]uid: 22037 topic_id: 11584 reply_id: 42926[/import]

[lua]module(…, package.seeall)
local ui = require(“ui”)
local director = require(“director”)

function new()
local localGroup = display.newGroup()

local background = display.newImage (“menubackground.png”)
localGroup:insert(background)

local button1Release = function( event )
– You should be able to call the change scene function here
director:changeScene(“scene2”,“moveFromLeft”)
end

local button1 = ui.newButton{
default = “button1.png”,
over = “button1over.png”,
onRelease = button1Release,
onEvent = buttonHandler,
id = “button1”,
text = “”,
font = “Trebuchet-BoldItalic”,
textColor = { 1, 1, 1 , 100 },
size = 30,
emboss = true
}

button1.x = 70; button1.y = 420

return localGroup
end[/lua] [import]uid: 27965 topic_id: 11584 reply_id: 42984[/import]

When I click that button is still visible on scene2. How to disable that button after click? and enable after return.

thx [import]uid: 22037 topic_id: 11584 reply_id: 44986[/import]

When using director you have to insert all display objects in to the local display group so that director can control them. Insert button1 in to the localGroup like this:
[lua]localGroup:insert(button1)[/lua] [import]uid: 27965 topic_id: 11584 reply_id: 44989[/import]

Everyday I’m learning something new from Corona forum.

Got it! working perfect thanks for your patience calebr2048 :slight_smile:
[import]uid: 22037 topic_id: 11584 reply_id: 45281[/import]