Hi everybody
I've been using vpython for a while to create visualisations for my electromagnetism class. Everything works fine when using vpython, but when I try to run code in Glowscript things stop working. I've been able to narrow it down to the following: It seems that calling functions during initialisation only runs the statement following the function call, then exits the method (see code example below). This seems to happen regardless of scope- I've tried making the function a member of the class, or even the init method itself, with the same result. As I'm no expert on Javascript I don't have a clue why this code works fine in vpython but does not compile to Javascript (or not as expected). Anyone know the reason for this behaviour, and possibly a solution?
Thanks,
Peter
def red():
return vec(1,0,0)
class Body:
def __init__(self, a_pos, a_color):
#self.my_color=red() # works as expected in vpython, but will only run next statement, then exit __init__() in Web VPython 3.2
self.my_color = a_color
self.my_pos = a_pos
#self.my_color= red() # will not cause error, but will not update self.my_color
def draw(self):
arrow(pos=self.my_pos, axis=vec(-1, 0, 0), color=self.my_color)
my_body = Body(a_pos = vec(0, 0, 0), a_color=vec(0,1,0))
my_body.draw()