Some function doesn't work

edited May 2015 in JavaScript Mode

Hi, I understand, that the title of the post is not too informative, unfortunately I don't know where the problem lies.

Some background, a student of mine was playing with Processing and wanted to post his results. However, even though his program runs nicely in Java mode, it stops working properly in JavaScript.

Here is the link to her project: http://www.openprocessing.org/sketch/200423

I guess the problem has something to do with syntax or functions that work differently in JS as opposed to J.

Any help would be most welcome, Alex

Tagged:

Answers

  • edited June 2015 Answer ✓
    • Although Java & JS are very much alike, sharing almost the same set for keywords, operators and their precedence, name conventions, etc., inside they're worlds apart! 8-X
    • "JS Mode" can transpile Java to JS code as long as it adheres to Processing's API:
    • In her case, hitting F12 and selecting the console tab, I've got:
      "Uncaught TypeError: number is not a function".
    • It means she tried to use a variable as a function, even though that variable doesn't hold a reference for such anymore.
    • Much probably somewhere in her sketch, she re-assigned that variable to a number, therefore losing the function's reference it once had.
    • In Java we can have the same label for a variable and a function at the same time.
    • Like for example variable mousePressed & function mousePressed():
    • B/c they're in different categories and Java knows how to differentiate them due to ().
    • However in JS, functions are assigned to variables, just like everything else. @-)
    • And () is an operator which tries to use the reference stored in a variable as a function call.
    • If it's not a function, an Error is thrown, the "Uncaught TypeError: xxx is not a function". >-)
    • Much probably she accidentally re-assigned some Processing function to a number.
    • Then later on, tried to invoke it as a function. Even though it's a number now! ;;)
  • Thanks a lot - I will show the post, and we will try to figure it out)

  • The variables (fields) named size are first suspect. Try to rename them and see if there is an improvement. Hint: the syntax coloring can help in finding the culprits.

  • Hi, thank you for help, after 10+ times rerunning the code we have found the problem. And it was, indeed, related to a number variable being used as a function.

    However, we are stuck with yet another glitch. For some strange reason the code restarts itself after a period of time.

    The first thing the program does - it sets the coordinates for the asteroids in the "setup" part. This means that if the asteroids change their place over time, and they do, the program somehow runs the "setup" part again, which is something I can't understand.

    This is the link: http://www.openprocessing.org/sketch/201071

Sign In or Register to comment.