Andengine - Enable A Disabled Child Button Based On Whether The Parent Button Has Been Pressed
With the current set-up that I have thanks to the answer here. How do I disabled child button based on whether the parent button has been pressed? for example Button2 is currently
Solution 1:
During your XML parsing when you are creating the buttons, we will take the opportunity to enable only GAMEBUTTON1, disable all of the others and call a function to enable all children when the button is pressed. I.e.,
if (type.equals(TAG_ENTITY_ATTRIBUTE_VALUE_GAMEBUTTON1))
{
final ButtonSprite levelObject = new ButtonSprite(x, y, resourcesManager.gamebutton1_region, vbom, new OnClickListener()
{
@Override
public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY)
{
//This button sprite has been clicked so lets enable any child sprites
EnableChildButtons(this); //See sub routine later
}
});
//For game button 1, we will enable it,
levelObject.setEnabled(true);
levelObject.setColor(Color.WHITE);
//Hereafter for further buttons, although the rest of the code will be the same
//will be disabled after creation with the following two lines in place of the latter
// levelObject.setEnabled(false);
// levelObject.setColor(Color.BLACK);
}
//...(rest of your code in previous post)
Before the sub routine EnableChildButtons, I made an ammendment to your levelObjectUserData, I replaced your int array of child IDs,
public int[] ChildID = {-1};
With a List,
public List<Integer> ChildIDs = new ArrayList<Integer>();
When calling the code which stores your child IDs simply use the following with your code,
for (int i = 0;i<childString_id.length;i++)
{
MyData.ChildIDs.add(Integer.parseInt(childString_id[i]));
}
We then just need to write the function to enable the children,
private void EnableChildButtons(final ButtonSprite mButtonSprite)
{
//To cut down on syntax length get a pointer to the button user data
final levelObjectUserData mUserData = ((levelObjectUserData) (mButtonSprite.getUserData()));
//We will be careful and run this on the update so we do not alter states
//while they are currently being processed by the update thread!
mActivity.runOnUpdateThread(new Runnable()
{
@Override
public void run()
{
//Go through all of the buttons child ids
for (int i = 0;i<mUserData.ChildIDs.size();i++)
{
//Locate the button with that ID as will be refernced in our levelObjects
//linked list
for (int j = 0;j<levelObjects.size();j++)
{
final int ButtonSpriteID = ((levelObjectUserData) (levelObjects.get(j).getUserData())).ID;
if (mUserData.ChildIDs.get(i) == ButtonSpriteID)
{
//We have found a child button, so enable it!
((ButtonSprite) levelObjects.get(j)).setEnabled(true);
((ButtonSprite) levelObjects.get(j)).setColor(Color.WHITE);
}
}
}
}
});
}
Hope this helps.
Post a Comment for "Andengine - Enable A Disabled Child Button Based On Whether The Parent Button Has Been Pressed"