Value change doesn't update in Draw

I have problem.
At TitleScreen class, I am creating a cursor which change position when up or down arrow button is pressed. Now, when I pressed the button, the cursor doesn’t move.

The button input itself works fine when I tested it with sys.exit.

So, I suspect that the problem is at the draw mechanism, but I am not so sure. Plus, I don’t know how to solve it.

Here is the code I use


from direct.gui.OnscreenImage import OnscreenImage
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from math import pi, sin, cos
from panda3d.core import TextNode
from pandac.PandaModules import WindowProperties
from pandac.PandaModules import TransparencyAttrib
from panda3d.core import loadPrcFileData

import sys

import direct.directbase.DirectStart
from pandac.PandaModules import *

#######################################

def drawText(x,y,text):
    return OnscreenText(text=str(text), style=1, fg=(1,1,1,1),
                        pos=(x,y), align=TextNode.ARight, scale = .07)

#######################################

class MenuAbstract(ShowBase):

	def __init__(self,cursorIndex,indexSize):
		self.titleText = [""]
		self.cursorIndex_ = cursorIndex
		self.indexSize_ = indexSize
		
	def ceilLimit(self):
		if self.cursorIndex_ == 0 :
			self.cursorIndex_ = self.indexSize_ - 1
		else:
			self.cursorIndex_ -= 1
			
	def floorLimit(self):
		if self.cursorIndex_ == self.indexSize_ -1 :
			self.cursorIndex_ = 0
		else:
			self.cursorIndex_ = 1
	
	def controlTitle(self):
		self.accept('arrow_up', self.ceilLimit)
		self.accept('arrow_down', self.floorLimit)
		
###########################################

class TitleScreen(MenuAbstract):

	def __init__(self):
		MenuAbstract.__init__(self,0,2)
		self.titleText_ = ["Begin","Continue"]

	def drawTitle(self):
		drawText(-0.5,0.5,self.titleText_[0])
		drawText(-0.5,0.4,self.titleText_[1])
		drawText(-0.8,0.5-(self.cursorIndex_*0.1),">")
		
		drawText(-0.8,0.2,self.cursorIndex_)
		drawText(-0.8,0.1,self.indexSize_)
		
		drawText(-0.8,0.3,self.titleText_[self.cursorIndex_])

###########################################

class Game(ShowBase):
	
	def __init__(self):
	
		ShowBase.__init__(self)
		self.accept('escape', sys.exit) 
		## Room: Title
		
		title = TitleScreen()
		title.drawTitle()
		title.controlTitle()
		title.drawTitle()

app = Game()
app.run()

Sorry for my bad grammar.
Thanks!

You are creating and returning a new OnscreenText object with each call to drawText(). These objects are never removed, and will remain onscreen forever. When you call drawText() again, you only add more objects to the screen, you’re not removing the objects you’ve already added.

To solve this, you will need to keep around a handle to the OnscreenText objects you are creating. You could call removeNode() on each of them before you create a new one. Or, you could attach all of them to a common node, and call removeAllChildren() on that common node.

Or, best yet, you could keep just one OnscreenText object around, and change its text by calling setText() on it, instead of creating a new one each time.

David

@drwr:
I have tried it with your hint.
It works very well =).
I destroy the OnscreenText object first when ceilLimit or floorLimit function is activated =).

Thank you very much =).

It is solved =).