Buttons made with LD

Hi there

i was trying around some days and searching in the forum but i cant get buttons made with ld to work

Ho do i set up an onevent for an button made with ld that should delete current lvl and load new one

pls help

wfw cloud

To interact with the buttons you first need to obtain the object;

myBtn = myLevel:getLayerObject("Controls","button\_1")

Now you associate a function to one of the button events (onPress, onCancel or onRelease);

myBtn.onPress = btnPressed

The function;

local function btnPressed(event) print ("button pressed") end

Does this help, also the scroller demo has examples of using buttons?

Also, be careful if you are unloading and reloading a level in the same scene, as your button object for example is a reference from the old level which no longer exists.

Ideally, you would use composer and the button would change scene, which unloads the level and the new scene loads the next level.

If you need to reuse the code for all levels, have a ‘switch’ scene that you call that allows the unload and the it switches back to your original scene.

thank u very much this helped me much :slight_smile:

but i have another question

i cant load 2 levels at same time

i want to load lvl 1 and controls lvl but it always says i am trying to assume loadlevel as nil value

and how can i get reference to an buttonobject if i use two lvls at same time?

tyvm for ure fast and good support :slight_smile:

To load two levels you could try something like;

local myLevel = LD\_Loader:new() myLevel:loadLevel("Level01") local myControls = LD\_Loader:new() myControls:loadLevel("Controls") myBtn = myControls:getLayerObject("btnLayer","button\_1")

ou yeah thank u really much 

i was in despair

i really love director x will buy it in the next time :smiley:

To interact with the buttons you first need to obtain the object;

myBtn = myLevel:getLayerObject("Controls","button\_1")

Now you associate a function to one of the button events (onPress, onCancel or onRelease);

myBtn.onPress = btnPressed

The function;

local function btnPressed(event) print ("button pressed") end

Does this help, also the scroller demo has examples of using buttons?

Also, be careful if you are unloading and reloading a level in the same scene, as your button object for example is a reference from the old level which no longer exists.

Ideally, you would use composer and the button would change scene, which unloads the level and the new scene loads the next level.

If you need to reuse the code for all levels, have a ‘switch’ scene that you call that allows the unload and the it switches back to your original scene.

thank u very much this helped me much :slight_smile:

but i have another question

i cant load 2 levels at same time

i want to load lvl 1 and controls lvl but it always says i am trying to assume loadlevel as nil value

and how can i get reference to an buttonobject if i use two lvls at same time?

tyvm for ure fast and good support :slight_smile:

To load two levels you could try something like;

local myLevel = LD\_Loader:new() myLevel:loadLevel("Level01") local myControls = LD\_Loader:new() myControls:loadLevel("Controls") myBtn = myControls:getLayerObject("btnLayer","button\_1")

ou yeah thank u really much 

i was in despair

i really love director x will buy it in the next time :smiley: