libGDX : Paint And Remove Color From Actor with touchDragged

心已入冬 提交于 2019-12-25 03:51:24

问题


I created two-dimensional array of Button actor, then I added new ClickListener() { touchDragged } as the following code:

buttons = new Button[3][3];

for (int row = 0; row < buttons.length; row++) {
    for (int col = 0; col < buttons[0].length; col++) {

buttons[row][col] = new Button(drawable);

buttons[row][col].addListener(new ClickListener() {

                @Override
                public void touchDragged(InputEvent event, float x, float y, int pointer) {
                    for (int row = 0; row < buttons.length; row++) {
                        for (int col = 0; col < buttons[0].length; col++) {
                            if (buttons[row][col].isOver()) {
                                buttons[row][col].setColor(Color.GREEN);
                            }
                        }
                    }
                }
   }
}

the code inside touchDragged method if the buttons isOver the buttons colored GREEN (it works fine) as shown in image

Now, How can I remove Color.GREEN i.e. (Color.WHITE) from buttons in the same calling touchDragged method, I mean undo GREEN to WHITE if the isOver() still true??

this image clear my question :

like Alphabetty Game from king company, if you know it :).

Sorry, For bad English


回答1:


You could use an if statement to check which colour the square is. If it is white, colour it green and vice versa.

I have something similar and for some reason you can't directly compare colours in an if statement, however changing them to an rgb int value solves this. You can choose from the various rgb options such as rgba8888 or argb8888 etc, choose one which will suit your needs. The simplest one would just be rgb888. It is a static method in the colour class, pass it a colour it will return an int.

if(Color.rgb888(button[row][col].getColor()) == Color.rgb888(Color.Green()))
    {
        button[row][col].setColor(Color.White());
    }



回答2:


1- Create List :

private List<Integer> list;

// In create OR show Method 
list = new ArrayList<Integer>();

2- Create this line code (as ID for buttons[row][col]):

buttons[row][col].setName("" + id++);

3- Write this code inside your touchDragged .. after your loop:

if (buttons[row][col].isOver()) {
    try {
        if (list.contains(Integer.parseInt(buttons[row][col].getName()))) {
            if(Integer.parseInt(buttons[row][col].getName()) == list.get(list.size() - 2)) {
                stage.getRoot().findActor("" + list.get(list.size() - 1)).setColor(Color.WHITE);
                list.remove(list.size() - 1);
            }
        } else {
            buttons[row][col].setColor(Color.GREEN);
            list.add(Integer.parseInt(buttons[row][col].getName()));
        }
      } catch (Exception e) {
           System.out.println(e.getClass());
      }
}

4- See the answer in this video



来源:https://stackoverflow.com/questions/32878595/libgdx-paint-and-remove-color-from-actor-with-touchdragged

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!