问题
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 disabled, but when I press Button1 I can now press Button2 and if I press Button2, Button3 and Button4 become enabled and can now be pressed. So far the way I'm doing it is as following: Declare a boolean:
boolean Enable = false;
Change Enable from false to true when a button is clicked:
if (type.equals(TAG_ENTITY_ATTRIBUTE_VALUE_GAMEBUTTON1)) {
levelObject = new ButtonSprite(x, y, resourcesManager.gamebutton1_region, resourcesManager.gamebuttonpressed_region, vbom, new OnClickListener() {
@Override
public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
Enable = true;
}
});
}
Disable all other button except for the first one, and seeing as Enable is still false before a button is pressed the other buttons should be disabled:
if (ButtonSpriteID != 1) {
levelObject.setEnabled(Enable);
levelObject.setColor(Color.BLACK);
}
And then final if the id of the child matches the child_id of the parent, the button should be enabled when a button is pressed because Enable should then be set to true:
for (int k = 0; k < ButtonSpriteChild.length; k++){
if (ButtonSpriteChild[k] == ButtonSpriteID){
levelObject.setEnabled(Enable);
levelObject.setColor(Color.WHITE);
}
}
I get not errors when running the code, but none of it seems to work as I intended it to. So any help would be great.
Update 1: So I've tried and removed Enable boolean and tried this:
if (ButtonSpriteID != 1) {
levelObject.setEnabled(false);
levelObject.setColor(Color.BLACK);
}
levelObject.setOnClickListener(new OnClickListener() {
@Override
public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
for (int k = 0; k < ButtonSpriteChild.length; k++){
if (ButtonSpriteChild[k] == ButtonSpriteID){
levelObject.setEnabled(true);
levelObject.setColor(Color.WHITE);
}
}
}
});
Still doesn't work. It was suggest that I override onTouchedArea but I'm not sure how I would implement that.
Update 2: I've now tried:
if (levelObjects.get(j).isPressed()) {
levelObjects.get(i).setEnabled(true);
levelObjects.get(i).setColor(Color.WHITE);
}
Still doesn't work. Still looking for help.
Update 3: I've now tried:
for (final ButtonSprite buttonSprite : levelObjects) {
if (ButtonSpriteID != 1) {
buttonSprite.setEnabled(false);
buttonSprite.setColor(Color.BLACK);
}
buttonSprite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX,
float pTouchAreaLocalY) {
for (int k = 0; k < ButtonSpriteChild.length; k++){
if (ButtonSpriteChild[k] == ButtonSpriteID){
buttonSprite.setEnabled(true);
buttonSprite.setColor(Color.WHITE);
}
}
}
});
}
Still not working, any help? Please?
Update 4: I've now tried:
levelObjects.get(j).setOnClickListener(new OnClickListener() {
@Override
public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX,
float pTouchAreaLocalY) {
for (int k = 0; k < ButtonSpriteChild.length; k++){
if (ButtonSpriteChild[k] == ButtonSpriteID){
levelObjects.get(i).setEnabled(true);
levelObjects.get(i).setColor(Color.WHITE);
}
}
}
});
That does not work as its giving me errors about setting "i" to final, but can't do that as "i" is in a for loop.
Update 5: So I've tried
if (ButtonSpriteChild[k] == ButtonSpriteID){
levelObject.setOnClickListener(new OnClickListener() {
@Override
public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX,
float pTouchAreaLocalY) {
levelObject.setEnabled(true);
levelObject.setColor(Color.WHITE);
}
});
}
Still doesn't work.
Update 6: So I've tried
if (ButtonSpriteChild[k] == ButtonSpriteID){
levelObject.setOnClickListener(new OnClickListener() {
@Override
public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX,
float pTouchAreaLocalY) {
levelObject.setEnabled(true);
levelObject.setColor(Color.WHITE);
}
});
}
Something weird happens only one button/levelobject is effected if press another button and some of the buttons that I press are not children or parent of that button.
回答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.
来源:https://stackoverflow.com/questions/15316933/andengine-enable-a-disabled-child-button-based-on-whether-the-parent-button-ha