jeudi 25 août 2016

Unable to use the same key press to connect/disconnect two squares

I want to make it so that when the square in my current class is adjacent (on the right) of the redSquare, I can press SPACE to connect the two squares and this square will follow the red one around whilst constantly being next to it. Then pressing SPACE again would "disconnect" the two squares, meaning this square wont follow the redSquare around any more. However, since SPACE is also used to connect the squares together, I think the code just keeps repeating, thus the squares never connect.

How do I use SPACE to connect and disconnect?

Boolean isConnected = false;
    public void update() {
        if (position.x - redSquare.position.x == 40 && position.y == redSquare.position.y) {
            if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
                isConnected = true;
            }
        }

        if (isConnected) {
            position.x = redSquare.position.x + 40;
            position.y = redSquare.position.y;
            if(Gdx.input.isKeyJustPressed(Input.Keys.SPACE)){
                isConnected = false;
            }
        }
    }

Also, as a side note, is there any resources you recommend to learn about these type of "logic" problems?

Aucun commentaire:

Enregistrer un commentaire