繁体   English   中英

如何根据ID识别对象

[英]How to identify an object off its id

我正在尝试制造战舰。

我有一个按钮,一旦单击,我希望被单击的按钮及其旁边的按钮都可以更改颜色。 (放在船上)

我为每个对象添加了一个ID,因此我有一个唯一的字符串来尝试识别按钮

您现在可以告诉我根据其ID标识一个对象,然后编辑该对象中的变量吗?

for(int x = 0; x < 10; x++) 
{

    for(int y = 0; y < 10; y++) {
        myrectangle r2 = new myrectangle();

        r2.id = Integer.toString(x)+Integer.toString(y);

        r2.PosX = x;
        r2.PosY = y;

        r2.setX(30 * x);
        r2.setY(30 * y);
        r2.setWidth(30);
        r2.setHeight(30);

        r2.setY((y*30)+400);

        r2.setFill(Color.GRAY);
        r2.setStroke(Color.WHITE);
        group.getChildren().add(r2);
        r2.setOnMousePressed(event ->{
            MouseButton Button = event.getButton();
            r2.pressed = true;
            if(Button == MouseButton.PRIMARY) { //left button
                //make the ship go horizontally
                int boatSize;
                if(r2.ships == 8) {
                    boatSize = 5;
                    if((boatSize + r2.PosX) < 11) 
                    {
                        // boat fits
                        r2.setFill(Color.RED);
                        r2.used = true;

                    }
                    //display aircraft horizontally(5 tiles)
                }
                else if(r2.ships == 7 || r2.ships == 6) 
                {
                    //display battleship horizontally(4 tiles)
                }
                else if(r2.ships == 5 || r2.ships == 4) 
                {
                    //display destoryer horizontally(3 tiles)
                }
                else if(r2.ships <= 3) 
                {
                    //display patrole boat horizontally(2 tiles)
                }
            }
            if(Button == MouseButton.SECONDARY) { //right button
                //make the ship go vertically
                if(r2.ships == 8) {
                    //display aircraft vertically(5 tiles)
                }
                else if(r2.ships == 7 || r2.ships == 6) 
                {
                    //display battleship vertically(4 tiles)
                }
                else if(r2.ships == 5 || r2.ships == 4) 
                {
                    //display destoryer vertically(3 tiles)
                }
                else if(r2.ships <= 3) 
                {
                    //display patrole boat vertically(2 tiles)
                }
            }
        });

        if(r2.pressed == true) {
            r2.setFill(Color.BLUE);
        }


    }
}

root.getChildren().add(group);
stage.show();

您可以使用事件对象的source属性来获得单击的节点:

r2.setOnMousePressed(event -> {
    mybutton clickedButton = (mybutton) event.getSource();
    ...
});

另一方面,您可以访问事件处理程序中的所有最终和有效的最终局部变量,如果您创建了它们的final副本,则可以访问r2甚至xy

for(int x = 0; x < 10; x++) {
    final finalX = x;
    for(int y = 0; y < 10; y++) {
        final finalY = y;
        ...
        r2.setOnMousePressed(event -> {
            System.out.println("X = " + r2.x);
            System.out.println("Y = " + finalY);
            ...
        });
        ...
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM