We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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
Perhaps the JS version doesn't support very well inheritance. I would try:
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
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
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.
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! [-(