Table of Contents Chapter Index Previous Slide Next Slide 360
Building a toggle switch

Writing program scripts with JavaScript
Building a toggle switch
  • A toggle switch script turns on at the first touch, and off at the second
  • A TouchSensor node can supply the touch events
  • DEF Toggle Script {
        field    SFBool on TRUE
        eventIn  SFBool set_active
        eventOut SFBool on_changed
        url "vrmlscript:
            function set_active( b, tm ) {
                if ( b == FALSE ) return;
                if ( on == TRUE ) on = FALSE;
                else              on = TRUE;
                on_changed = on;
            }"
    }