I’m creating an app using the andengine for Android. I’m autogenerating some of the screens and need to set specific OnClickListeners to specific buttons.
I’m using org.andengine.entity.sprite.ButtonSprite and org.andengine.entity.sprite.ButtonSprite.OnClickListener.
Each button has an id string so I had hoped to be able to create the listeners when I initialize the scene and store them in a HashMap. Like so:
buttonListeners = HashMap<String, Object>();
OnClickListener ocl = new OnClickListener() {
public void onClick( ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY ) {
//Do stuff
Log.d("Button Test", "Huzzah A");
}
};
buttonListeners.put("play_button", ocl);
And then when I create the button just do:
button.setOnClickListener((OnClickListener) buttonListeners.get(button.id));
This does not work. I suspected some problems in my HashMap so I created a class variable ocl2 identical to the one above, initialized it in the constructor and tried using it like this:
button.setOnClickListener(ocl2);
It did not work either. Intent on making sure I wasn’t just doing “it” wrong I tried this:
button.setOnClickListener( new OnClickListener()
{
public void onClick( ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY )
{
//Do stuff
Log.d("Button Test", "Huzzah C");
}
});
And this way works flawlessly. Problem being that since I autogenerate everything I can’t use this method (at least not the way that I’d want to…)
Is this some limitation on the way that this works? Am I just doing it wrong?
Maybe you can get around the problem by using the following: