Help with DirectButton


I am trying to get show a DirectButton on the screen to start a game when pressed. I have tried several different ways to do it but my code just does not work.

First of all I cant even see the button ! I also want to make a function so that when the button is pressed the game starts. I don’t even have a clue about how to do that.

This is the file in which the button is called, its seems to work fine since the word game is printed on the screen lol.

import direct.directbase.DirectStart
import Button
from pandac.PandaModules import *
from direct. gui import DirectGuiGlobals
from direct.gui. OnscreenText import OnscreenText


text=OnscreenText(text = ‘Game’, scale = 1, style=1, bg=(255,0,0,0))

I would love to set the background color to red but it just wont work.

The code for the button looks like this
from direct.gui.DirectGui import *
from direct. gui import DirectGuiGlobals

DirectObject.myDirectObject = Button(parent = aspect2d,
text = “Start Game”,
text_scale = (0,4, 0,4),
pos = (-1, 0 ,0),
relief = FLAT,
pressEffect = 1)
resetFrameSize = 10

             #comandfunc(self, event)


It would be quite helpful if somebody could give me some pointers in the right direction, since I am hopelessly lost in this coding stuff.

Have tried to look in the manual, but it just does not seem to work

The best source I have found for figuring Panda3D is to look at the examples.

For changing the background, use this:

base.setBackgroundColor(0, 90, 0)

this changes the background to a rather ugly bright neon green (the args are R,B,G or Red, Blue, Green). I’m not sure if there is a table of values to use for setting common colors - you may just have to play around with the values and see what you get.

For setting up a button use the following code:

self.button = DirectButton(pos = Vec3(.7,0,.7), text = "Open Box",
               scale = .1, pad = (.5, .5),
               rolloverSound = None, clickSound = None,
               command = self.runMyGame)

This little snippet game from the music box sample except I changed the command to be runMyGame.

runMyGame would be the function that controls your game.

This might help a bit - but don’t trust me too much because I am new at this also.

You might also want to take a look at the airblade demo available on the download pages.

The source looks like it does exactly what you are talking about.