0

I have been working on a processing.js demo in which bodies orbit a central point. I am attempting to add a listener for input that checks if the s key is pressed each update and if so calls a method to reduce the size of each bodies' orbit by a specified factor.

The example is here on jsfiddle.

Essentially I have placed code in the update() method to listen for a key press:

 void update()
    {
        //detect input
        if(keyPressed)
        {
            debugger; //debug

            if(key == 's' || key == 'S')
            {
                //shrink orbit
                ShrinkOrbit(planets,.9);
            }

        }    
    }

Which calls the ShrinkOrbit(ArrayList <OrbitingBody> orbs, float reductionFactor) method:

/*
* Reduces size of orbit for specified group of orbiting bodies
*/
void ShrinkOrbit(ArrayList<OrbitalBody> orbs, float reductionFactor)
{   
    for(OrbitalBody b:orbs)
    {
        b.x *= reductionFactor;
        b.y *= reductionFactor
    }
}

However, currently not only does the debug breakpoint not trigger, but I see some errors cropping up in the chrome developer tools:

Uncaught SyntaxError: Unexpected reserved word 
Uncaught TypeError: Cannot read property 'reason' of null Actions.js:333
(anonymous function) Actions.js:333
(anonymous function) moo-clientcide-1.3.js:212
Array.implement.each moo-clientcide-1.3.js:329
(anonymous function) moo-clientcide-1.3.js:212
Class.JSLintValidate Actions.js:330
wrapper.extend.$owner moo-clientcide-1.3.js:3798
Class.jsLint Actions.js:277
wrapper.extend.$owner moo-clientcide-1.3.js:3798
(anonymous function) moo-clientcide-1.3.js:1027
defn

Was I wrong to assume that update() is a global method, something equivalent of setup that can be called in any sketch? I had seen it used as such in another functioning jsFiddle, which is from where I drew my conclusion. I am targeting Processing.js 1.4.1 if that is any help.

Christian
  • 1,685
  • 9
  • 28
  • 48

1 Answers1

1

don't do that. 1) update is indeed not a global function, the only ones you get are the Processing API functions, and the two major calls are setup(), called once at the start, and draw(), called every frame (unless you issued noLoop(), then it's run only when you call redraw() manually), and 2) use event handlers, it's what they're for =)

boolean reduce = false;

void keyPressed() {
  if(str(key).equals("s")) reduce = true;
}

void keyReleased() {
  if(str(key).equals("s")) reduce = false;
}

void draw() {
  if(reduce) { doWhateverYouNeedToDo(); }
  drawStuff();
}
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153