UI button..change scene

I am using Ui button, but i cant change the scene using ui buttons, what is wrong?

main.lua

  
display.setStatusBar( display.HiddenStatusBar )  
  
   
\_W = display.contentWidth;  
\_H = display.contentHeight;  
  
local director = require("director")  
  
local mainGroup = display.newGroup()  
  
local function main()  
 mainGroup:insert(director.directorView)  
  
 director:changeScene("main\_menu\_home")  
  
 return true  
end  
  
main()  
  

main_menu_home.lua

  
module(..., package.seeall)  
function new()  
  
  
 local director = require("director"); -- if i dont inserted this line i cant see the button, why? i inserted this line on main.lua  
 local ui = require("ui");  
  
 local main\_menu\_play\_touch = function( event )  
 if event.phase == "release" then  
 director:changeScene("loadmainmenu")  
 end  
 end   
 local main\_menu\_play = ui.newButton{  
 defaultSrc = "main\_menu\_play\_off.png",  
 defaultX = 55,  
 defaultY = 55,  
 overSrc = "main\_menu\_play\_off.png",  
 overX = 45,  
 overY = 45,  
 onEvent = main\_menu\_play\_touch  
 }  
  
 main\_menu\_play.x = \_W\*0.50;  
 main\_menu\_play.y = \_H\* 0.80;  
 main\_menu\_play.isVisible = true;  
  
  
end  

loadmainmenu.lua

[code]

module(…, package.seeall)
function new()
local LoadingImage = display.newImage(“loading.png”)
LoadingImage.x = _W/2; LoadingImage.y = _H/2
end
[/code] [import]uid: 26056 topic_id: 18995 reply_id: 318995[/import]

Try changing it to this;

[lua]local main_menu_play_touch = function( event )
director:changeScene(“loadmainmenu”)
end

local main_menu_play = ui.newButton{
defaultSrc = “main_menu_play_off.png”,
defaultX = 55,
defaultY = 55,
overSrc = “main_menu_play_off.png”,
overX = 45,
overY = 45,
onRelease = main_menu_play_touch
}[/lua]

That should do it. UI buttons work fine with everything a normal button would.

Peach :slight_smile: [import]uid: 52491 topic_id: 18995 reply_id: 73151[/import]

thanks for the help peach, but dont work :confused: [import]uid: 26056 topic_id: 18995 reply_id: 73194[/import]

Did you enclose your loadmainmenu.lua code with
[lua]function new()
.
.
.
end[/lua] [import]uid: 64174 topic_id: 18995 reply_id: 73220[/import]

I just tested this (without using any of your other code) and it ran fine on the latest stable build.

Are you getting any errors? Does it work with a non UI button as is? [import]uid: 52491 topic_id: 18995 reply_id: 73224[/import]

i am testing with ui button and i can do anything, now will test with other button type, satheesh, i inserted this code but nothing happened.

the code is updated [import]uid: 26056 topic_id: 18995 reply_id: 73233[/import]

problem resoloved, the solution is… create local group, insert the button on the group and return the group :slight_smile: [import]uid: 26056 topic_id: 18995 reply_id: 73303[/import]

Peach, is there an example of using the director class, and the buttons actually change when you hover over them ?

The example that comes with director class doesn’t do this. Even the example below, from learingcorona.com website doesn’t change the buttons on hover:
http://www.youtube.com/watch?v=KudLE8h4kWw

Is the answer to use UI buttons instead, and if so, do you have a more complete example than the one above?

[import]uid: 97524 topic_id: 18995 reply_id: 74560[/import]

Yes you would use UI buttons and no, I don’t currently have an example.

However - if you look at the button events sample code (it’s in CoronaSDK > SampleCode > Interface) that will show you how to make one of these buttons - then you’d simply change scenes in the normal way.

Peach :slight_smile: [import]uid: 52491 topic_id: 18995 reply_id: 74616[/import]

Ok Peach, here’s an example of using a UI button with director class, that I cobbled together.

Yes, I know there’s a UI button example in the templates section but what I didn’t understand was how to combine the director code with it.

The following code works fine for me. This snippet is from my help screen and is saved as help.lua. I also have menu.lua and options.lua working in a similar way:

[code]
module(…,package.seeall);

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

– create the background
local bg = display.newImageRect(“images/green.png”,_W,_H);
bg:setReferencePoint(display.CenterReferencePoint);
bg.x = _W/2; bg.y = _H/2;

– create a simple label
local back_label = display.newImageRect(“images/label.png”, 320,44);
back_label:setReferencePoint(display.CenterReferencePoint);
back_label.x = _W/2; back_label.y = _H/2 - 220;

local back_btn_release = function( event )
director:changeScene(“menu”)
end

– create the ui button
local back_btn = ui.newButton{
default = “images/buttonBack.png”,
over = “images/buttonBackOver.png”,
onRelease = back_btn_release,
text = “”,
emboss = true
}
back_btn:setReferencePoint(display.CenterReferencePoint);
back_btn.x = _W/2 - 128; back_btn.y = _H/2 - 220;

– change scene to menu calls menu.lua, etc…
function changeScene(e)
if (e.phase == “ended”) then
director:changeScene(e.target.scene);
end
end

– insert screen items into the local group
localGroup:insert(bg);
localGroup:insert(back_label);
localGroup:insert(back_btn);

return localGroup;
end
[/code] [import]uid: 97524 topic_id: 18995 reply_id: 74677[/import]

Well done, I’m sure this will help others in the future :slight_smile: [import]uid: 52491 topic_id: 18995 reply_id: 74843[/import]