AndEngine - 启用基于父按钮是否已经pressed残疾儿童按钮 [英] AndEngine - Enable a disabled child button based on whether the parent button has been pressed
问题描述
随着目前的设置,我有感谢的答案<一个href=\"http://stackoverflow.com/questions/15116158/andengine-drawing-a-line-between-level-objects-entites-using-matching-ids\">here.我怎么残疾儿童按钮基于父按钮是否已经pressed?例如将Button2当前已禁用,但是当我preSS Button1的我现在可以preSS Button2的,如果我preSS Button2的,将Button3和将Button4变得启用,现在可以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 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;
从false更改为true来启用一个按钮被点击时:
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;
}
});
}
禁用所有其他的按钮,除了第一个,看到是启用还是假前的按钮是pressed其他按钮应禁用:
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);
}
然后最后如果孩子的id父的child_id匹配,按钮应该被启用时,按钮pssed因为启用然后应设置为true $ P $:
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);
}
}
我运行code,当拿到并非错误,但没有它似乎工作,因为我想要它。因此,任何帮助将是巨大的。
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.
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);
}
}
}
});
还不行。这是表明我重写onTouchedArea,但我不知道我将如何实现这一点。
Still doesn't work. It was suggest that I override onTouchedArea but I'm not sure how I would implement that.
if (levelObjects.get(j).isPressed()) {
levelObjects.get(i).setEnabled(true);
levelObjects.get(i).setColor(Color.WHITE);
}
还不行。还在寻求帮助。
Still doesn't work. Still looking for help.
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?
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);
}
}
}
});
这不工作作为给我有关设置我到最后的错误,但不能这样做,因为我是一个for循环。
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.
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);
}
});
}
还不行。
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);
}
});
}
奇怪的事情发生了只有preSS另一个按钮和一些我preSS的按钮都没有孩子,或者认为按钮的父一键/ levelobject的影响。
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.
推荐答案
在你的XML解析,当你正在创建的按钮,我们将借此机会仅启用GAMEBUTTON1,禁止其他所有和调用一个函数,使所有孩子当按钮pressed。即,
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)
的子过程EnableChildButtons之前,我做了ammendment你levelObjectUserData,我换成孩子的ID你的int数组,
Before the sub routine EnableChildButtons, I made an ammendment to your levelObjectUserData, I replaced your int array of child IDs,
public int[] ChildID = {-1};
与列表,
public List<Integer> ChildIDs = new ArrayList<Integer>();
当调用code存储你的孩子的ID简单地使用你的code以下,
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);
}
}
}
}
});
}
希望这有助于。
这篇关于AndEngine - 启用基于父按钮是否已经pressed残疾儿童按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!