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

2017/8/19

Toggle

Venbha Venbha

2017/8/19

#
I am Making a scenario in which you have a Fidget spinner and a switch which has text on two sides; manual and automatic. when it is in automatic, the spinner will turn around and when in manual, when you click it will spin. What is the code for it? Please reply below | REPLY>COM
Super_Hippo Super_Hippo

2017/8/19

#
Venbha wrote...
I am Making a scenario...
If you are making the scenario, don't ask to get the complete code of the scenario.
Venbha Venbha

2017/8/20

#
Ok but I just want the toggling code
Super_Hippo Super_Hippo

2017/8/20

#
You can either: - save a reference to the fidget spinner in the switch - switch checks for clicks on itself: if switch is clicked, let the fidget spinner know that it was clicked (call a method on the fidget spinner object) - in this method, change a variable of the fidget spinner - the fidget spinner moves depending on this variable or: - save a reference to the switch in the fidget spinner - the fidget spinner checks for clicks on the switch - ... (rest like above)
Venbha Venbha

2017/8/21

#
I will try it in my project thank you(even if it doesn't)
Venbha Venbha

2017/8/22

#

IT DOESN'T WORK!!!

can you write the code ONLY for detecting if the switch has been clicked it will spin (Or else stop spinning)
Yehuda Yehuda

2017/8/22

#
Venbha wrote...
can you write the code ONLY for detecting if the switch has been clicked it will spin (Or else stop spinning)
That's what's above. The code deals with the actions of two classes, these actions are described by Hippo.
danpost danpost

2017/8/23

#
Venbha wrote...
can you write the code ONLY for detecting if the switch has been clicked it will spin (Or else stop spinning)
The following should be sufficient with empty Spinner and Switch classes:
import greenfoot.*;

public class MyWorld extends World
{
    Actor spinner;
    Actor toggle;
    boolean auto;
    
    public MyWorld()
    {
        super(600, 400, 1);
        spinner = new Spinner();
        addObject(spinner, 300, 200);
        toggle= new Switch();
        toggle.setImage("toggleOff.png");
        addObject(toggle, 80, 200);
    }
    
    public void act()
    {
        if (Greenfoot.mouseClicked(toggle))
        {
            auto = ! auto;
            toggle.setImage("toggle"+(auto ? "On" : "Off")+".png");
        }
        if (auto) spinner.turn(5);
    }
}
You can adjust the image filenames for the toggle as needed.
Venbha Venbha

2017/8/23

#
should I use 'On.png' and 'Off.png' for this following line in line 24?
            toggle.setImage("toggle"+(auto ? "On" : "Off")+".png");
Venbha Venbha

2017/8/23

#
I will for now... :() <:)
Venbha Venbha

2017/8/23

#
k thanks all :))) :D
Venbha Venbha

2017/8/23

#
See link of my scenario Fidget Spinner
Venbha Venbha

2017/8/25

#
And I also wrote the Code ONLY on the WORLD
You need to login to post a reply.