質問

This is my code, in the main method, there is just one line:

game.initShowBase()

The code of THIS function is then:

def initShowBase():

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

starters then:

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

Without ANY changes on the code, I sometimes get a black window without any buttons, a pink window (right color) without any buttons or a pink window with all three buttons.

There is no stacktrace, as the compiler never seems to see an error, so I guess I have a deadlock somewhere. I just do not find it, I mean, it is not this much code.

can anyone help?

Maybe I should also mention that indicatorValue does not seem to work - the first button, IF it appears, is always set.

役に立ちましたか?

解決

I'm guessing that the issue has to do with your use of threading. Firstly, I would suggest that you use Panda3D 1.8 if you aren't already using it, it has improved support for threading. That said, I can still reproduce some issues with your code in Panda3D 1.8.

Secondly, I would strongly advise you to do your setup in the main thread, or add additional synchronisation. The window may appear black if setBackgroundColor is called before the window has been properly started in the main thread, for instance. There are many potential pitfalls to consider when using threading, especially when accessing the same constructs from two threads at once. I would reserve threading for heavy loading operations such as loading models (for which Panda3D has special constructs to do this asynchronously) or for game logic that has little interaction with the graphics pipeline.

As for indicatorValue, you need to actually use the value and variable fields to change the default value, as follows:

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])
]

Whichever button's value matches up with that set in var will be selected by default. (The reason a list is used instead of an int is because lists are passed by reference, and not by value.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top