from processing to javascript: unexplainable bug

edited February 2014 in JavaScript Mode

Hi

I am trying to pass my processing program on to javascript. Of course it didn't work on the first time, so I'm chasing the bugs one by one. I came to a bug I cannot explain and I cannot correct. To simplify the problem I wrote an example code that reproduces the same problem, I put it at the end of my post.

Let me sum up the problem: I use a class (stone) that extends PVector. If I want to use the attribute 'x' of my class, javascript breaks ('x is undefined'), while java works well. If I use in the exact same way two classes I invented ("Truc" and "Machin"), it works perfectly well...

(the following code works if i delete the line "println(s);")

EDIT: The code is in the next post, cleaned by GotoLoop (thanks!) (but the problem remains)

Answers

  • edited February 2014
    // forum.processing.org/two/discussion/2961/
    // from-processing-to-javascript-unexplainable-bug
    
    void setup() { 
      final Machin m = new Machin(10, -20, 5.43);
      final Stone  s = new Stone(30, 50, -65.827);
    
      println(m);
      println(s);
    
      exit();
    }
    
    class Truc {
      float x, y, z;
    
      Truc(float xx, float yy) {
        x = xx;
        y = yy;
      }
    
      Truc(float xx, float yy, float zz) {
        x = xx;
        y = yy;
        z = zz;
      }
    
      String toString() {
        return "[" + x + ", " + y + ", " + z + "]";
      }
    }
    
    class Machin extends Truc {
      float a;
    
      Machin(float xx, float yy, float aa) { 
        super(xx, yy); 
        a = aa;
      }
    
      Machin(float xx, float yy) {
        super(xx, yy);
      }
    
      String toString() {
        return "[ " + x + ", " + y + ", " + a + " ]";
      }
    }
    
    class Stone extends PVector { 
      float a;
    
      Stone(float xx, float yy, float aa) { 
        //super(xx, yy);
        super(xx, yy, 0);
        a = aa;
      }
    
      Stone(float xx, float yy) {
        //super(xx, yy);
        super(xx, yy, 0);
      }
    
      String toString() {
        return "[ " + x + ", " + y + ", " + a + " ]";
      }
    }
    
  • One problem that I see right away: In Javascript mode, you can't overload constructors (or methods) - for the same reason that you can't have a method and a variable share the same name. So each of your classes can have only one constructor...

  • Ok thanks, so here is a new simplified version of the code that does not work

    void setup() {
      final Stone  s = new Stone(30, 50);
      println(s); 
    
      exit();
    }
    
    class Stone extends PVector {
    
      Stone(float xx, float yy) {
        super(xx, yy);
      }
    
      String toString() {
        return "x:"+x;
      }
    }
    
  • Answer ✓

    Perhaps the JS version doesn't support very well inheritance. I would try:

      String toString() {
        return "[ " + this.x + ", " + this.y + ", " + a + " ]";
      }
    

    for example.

    Or avoid using inheritance here, PVector (particularly the JS one) might not be designed for that. Why not use composition instead?

  • I wonder whether any1 bothered to check my code on JS Mode after all?! :-w

  • Answer ✓

    Nah, according to the OP, it was supposed to be the original code, just reformatted... So if you changed it, it is hard to tell what was wrong in the original code.

  • Changed or not, it shows how to overcome the error! The original bugged line is commented out! Easy to spot!

  • Indeed, "this.x" works! Thanks PhilHo

  • edited February 2014

    Indeed, "this.x" works!

    In my solution, there's no need of keyword this, b/c all of the 3 PVector's fields (x, y, z)
    were perfectly inherited by the sub-class Stone! :-w

  • Well actually, your solution does not work with javascript (only the first output line is displayed). I didn't try to fix it i thought it was just a cleaned version of my code.

  • edited February 2014

    ... your solution does not work with javascript...

    You're right! Browser's JS has deceived me!!! :(
    I was using OpenProcessing site to debug it:

    http://www.openprocessing.org/sketch/create

    But apparently, once fields like x, y are defined inside a class, they continue to work even though they're removed from it later!!!
    So when I kept re-running the code w/ super(xx, yy, 0);, it still recognized the existing of both x & y,
    even though that super call has actually failed! :-/

    After debugging it more, it's pretty clear to me that inheritance fails in JS mode when the parent class comes from a library! :-q
    Parent classes gotta be included in-text in order for the inheritance conversion to succeed! [-(

Sign In or Register to comment.