0

这是我的代码,在 main 方法中,只有一行:

game.initShowBase()

那么这个函数的代码是:

def initShowBase():

   global myShowBase   
   myShowBase = ShowBase(False)
   startwindowThread = threading.Thread(target = starters.startfunction)
   startwindowThread.start()
   myShowBase.run()

首发然后:

def startfunction():
   from modules.logic import game
   global h
   h = 0
   print h
   # g1 = open ("game1", "a")
   #g1.close()
   #g2 = open ("game2", "a")
   #g2.close()
   #g3 = open ("game3", "a")
   #g3.close()
   game.myShowBase.setBackgroundColor(1.0,0.627,0.894, 1)     
   textObject = OnscreenText(text = "Beginn", pos = (0.95,-0.95), 
                         scale = 0.07,fg=(1,0.5,0.5,1),align=TextNode.ACenter,mayChange=1)


   buttons = [
           DirectRadioButton(text = 'Game 1 ', scale=0.05, indicatorValue = 0,  pos=(-0.4,0,0), command=load, extraArgs =[1]),
           DirectRadioButton(text = 'Game 2 ',  scale=0.05, indicatorValue = 0, pos=(0,0,0), command=load, extraArgs = [2]),
           DirectRadioButton(text = 'Game 3 ',  scale=0.05, indicatorValue = 0, pos=(0.4,0,0), command=load, extraArgs = [3])
         ]
   print "button"
   for button in buttons:
       button.setOthers(buttons)
       print "true"


def load(gamenumber):
   global h
   if (gamenumber==1):
       h += 1
       if (h>0):
           loadGame("game1")
   elif (gamenumber==2):
       loadGame("game2")
   else:
       loadGame("game3")


def loadGame (gamename): 
    from modules.logic import game
    global data
    data = open (gamename, "r")
    print gamename

如果不对代码进行任何更改,有时我会得到一个没有任何按钮的黑色窗口,一个没有任何按钮的粉红色窗口(正确颜色)或一个带有所有三个按钮的粉红色窗口。

没有堆栈跟踪,因为编译器似乎从来没有看到错误,所以我想我在某个地方遇到了死锁。我只是没有找到它,我的意思是,它没有那么多代码。

谁能帮忙?

也许我还应该提到,indicatorValue 似乎不起作用 - 第一个按钮,如果它出现,总是设置。

4

1 回答 1

1

我猜这个问题与你对线程的使用有关。首先,如果您还没有使用 Panda3D 1.8,我建议您使用它,它改进了对线程的支持。也就是说,我仍然可以在 Panda3D 1.8 中重现您的代码的一些问题。

其次,我强烈建议您在主线程中进行设置,或添加额外的同步。例如,如果在主线程中正确启动窗口之前调用 setBackgroundColor,则窗口可能会显示为黑色。使用线程时有许多潜在的陷阱需要考虑,尤其是同时从两个线程访问相同的结构时。我会为繁重的加载操作保留线程,例如加载模型(Panda3D 有特殊的结构可以异步执行此操作)或与图形管道几乎没有交互的游戏逻辑。

至于indicatorValue,需要实际使用valueandvariable字段来改变默认值,如下:

var = [1]
buttons = [
    DirectRadioButton(text='Game 1 ', variable=var, value=[1], scale=0.05, pos=(-0.4,0,0), command=load, extraArgs=[1]),
    DirectRadioButton(text='Game 2 ', variable=var, value=[2], scale=0.05, pos=(0,0,0), command=load, extraArgs=[2]),
    DirectRadioButton(text='Game 3 ', variable=var, value=[3], scale=0.05, pos=(0.4,0,0), command=load, extraArgs=[3])
]

var默认情况下,将选择与该设置匹配的任何按钮的值。(使用列表而不是 int 的原因是因为列表是按引用传递的,而不是按值传递的。

于 2013-03-16T13:50:13.950 回答