This site requires JavaScript, please enable it in your browser!
Greenfoot back
Tot
Tot wrote ...

2021/6/6

How can I bring a variable over from my world to my actors

Tot Tot

2021/6/6

#
I'm trying to find out if all three buttons are pressed, using three booleans, button1, button2 and button3. I don't know how to bring them all into the same code, though. I get that this is probably pretty basic stuff for some of you anyway please help
public void checkPressed()
    {
        Actor pressed = getOneObjectAtOffset​(0, 0, Actor.class);
        if (pressed != null) 
        {
            button1 = true;
        } else
        {
            button1 = false;
        }
    }
danpost danpost

2021/6/6

#
Tot wrote...
I'm trying to find out if all three buttons are pressed, using three booleans, button1, button2 and button3. I don't know how to bring them all into the same code, though. I get that this is probably pretty basic stuff for some of you anyway please help << Code Omitted >>
Your code is faulty to begin with (unless you have 3 actors -- one for each button). As coded, with only one actor, as soon as it leaves a button, the boolean will turn back to false. Remove else part. Best is probably to maintain references to the buttons in your world and check the booleans in act of world.
// in world
private Button1 b1 = new Button1();
private Button2 b2 = new Button2();
private Button3 b3 = new Button3();

// in act of world
if (b1.button1 && b2.button2 && b3.button3) ; // do something
with public boolean fields in button classes.
Tot Tot

2021/6/6

#
I'm going to have three actors, and the idea is that the buttons only stay down if they are pressed, so they all have to be simultaneously pushed. That's why the else is there I tried putting this code into the constructor of my world, and it said I had an illegal start to the expression, so I probably put it in the wrong place.
// in world
private Button1 b1 = new Button1();
private Button2 b2 = new Button2();
private Button3 b3 = new Button3();
Also I don't know what maintain references to the buttons or public boolean fields in button classes means
Tot Tot

2021/6/6

#
ok update I got the code in and its good and all that but I still don't know how to keep the booleans the same between the world and the actors
danpost danpost

2021/6/7

#
Tot wrote...
ok update I got the code in and its good and all that but I still don't know how to keep the booleans the same between the world and the actors
You should not have booleans in your world -- not to keep same as actors.
You need to login to post a reply.