Menu in Glowscript and VPython

时光总嘲笑我的痴心妄想 提交于 2019-12-11 15:36:56

问题


For a school project a friend of mine and I are currently trying to program a simulation of our solar system. The platform we use is Glowscript and we're working with VPython. We have programmed our whole solar system already with all its planets, the sun and the forces between them.

Now we're trying to add some extras and one of them is a menu in which the user can select a planet. In our code the selected object (a planet or the sun which are defined in the code) will be labeled as the 'currentobject'. After that it will be possible for the user to change the currentobject's mass using a slider.

Two problems occurred during the process of programming. The first one is that we were not able to define an object as the currentobject so that its mass can be changed later on. Sadly, we couldn't find the problem in our code.

The second problem is that we weren't able to add more than one extra to our simulation in Glowscript. We have already programmed a slider feature to change the simulations 'rate' (velocity) and after that the menu and the slider for changing the mass simply didn't show up.

def M(m):
    global col, currentobject
    currentobject.visible = True
    obj = m.selected
    if obj == 'Sun':
        currentobject = sun
    elif obj == "Mercury":
        currentobject = mercury
    elif obj == "Venus": 
        currentobject = venus
    elif obj == "Earth": 
        currentobject = earth
    elif obj == "Mars": 
        currentobject = mars
    elif obj == "Jupiter": 
        currentobject = jupiter
    elif obj == "Saturn": 
        currentobject = saturn
    elif obj == "Uranus": 
        currentobject = uranus
    elif obj == "Neptune": 
        currentobject = neptune
    currentobject=m.selected
    print(m.selected,m.index)


menu(choices=['Choose an object', 'Sun', 'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune'], bind=M)
scene.append_to_caption('\n\n')


def setmass(b):
    wt.text = '{:1.2f}'.format(b.value)
sl = slider(min=0.1, max=10, value=0.1, length = 1000, bind=setmass)  
wt = wtext(text='{:1.2f}'.format(sl.value))


#mainloop 

while (True):  


    rate(10)

    currentobject.mass=currentobject.mass*sl.value

In the end, the user of our simulation should be able to select a planet or the sun in a drop down menu. After that the user should be able to change the selected object's mass (using a slider) and see the effects in the simulation above (the other planets orbits change because they are more attracted to the currentobject).

Error message:

TypeError: Cannot read property 'mass' of undefined


回答1:


You code fragment isn't sufficiently complete to know exactly what the problem is, but I note that the first call to M has currentobject.visible = True, but currentobject doesn't yet exist, so you get an error. Similarly, in your loop you reference currentobject.mass, but currentobject doesn't yet exist.



来源:https://stackoverflow.com/questions/57572033/menu-in-glowscript-and-vpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!