Elements Name and Controller

Hello guys ,



i havea little problem that i’m sure you have the answer :smiley: , i have this



[xml]

<panel id=“huhu-2” childLayout=“vertical” width=“100%”>

<label id=“Attack” text=“Attack” style=“menu-item” >

</label>

<label id=“Shi” text=“Shi” style=“menu-item” >

</label>

<label id=“Combos” text=“Combos” style=“menu-item” >

</label>

<label id=“Items” text=“Items” style=“menu-item” >

</label>

</panel>

[/xml]



And that :



[java]

else if (inputEvent == NiftyInputEvent.Activate) {

System.out.println("Test ");

element.onClick();

return true;

[/java]



I would like to do something like this (pseudo code here ):





[java]

else if (inputEvent == NiftyInputEvent.Activate ) {



if (element.name=“Attack”){

Object.MyAttackFunction

}elseif(element.name=“Items”){

Object.MyItemFunction}

etc…

element.onClick();

return true;

[/java]



How do i link Labels id names to my program in Java ? Thanks a lot in advance !

You can resolve names to elements with a call to:



[java]Element element = screen.findElementByName("Attack");[/java]



BUT BUT BUT



it looks like you want something different here :slight_smile: Messing with the NiftyInputEvent would probably work but a somewhat easier way would be to simply add an onClick() event to the element:



[xml]<label id="Attack" text="Attack" style="menu-item" >

<interact onClick="attackClicked()" />

</label>[/xml]



which will directly call the method attackClicked() on your screencontroller:



[java]public class MyScreenController implements ScreenController



public void attackClicked() {

… do stuff in here

}

}[/java]



Or like this:



Use a parameter and a single onClick() method:



[xml]<label id="Attack" text="Attack" style="menu-item" >

<interact onClick="menuItemClicked(Attack)" />

</label>



<label id="Items" text="Items" style="menu-item" >

<interact onClick="menuItemClicked(Items)" />

</label>[/xml]



with this method on your ScreenController:



[java]public class MyScreenController implements ScreenController {



public void menuItemClicked(final String mode) {

if ("Attack".equals(mode)) {

} else if … // you get the idea

}

}[/java]

1 Like

You’re still the best thanks a lot void !