428

Writing program scripts with JavaScript
Building a toggle switch

  • A toggle script turns on at 1st touch, off at 2nd
  • A TouchSensor node can supply touch events
  • DEF Toggle Script {
        field    SFBool on TRUE
        eventIn  SFBool set_active
        eventOut SFBool on_changed
        url "vrmlscript:
            function set_active( b, ts ) {
                // ignore button releases
                if ( b  == FALSE ) return;
    
                // toggle on button presses
                if ( on == TRUE  ) on = FALSE;
                else               on = TRUE;
                on_changed = on;
            }"
    }