remove text

i want to display a new number each time the key “w” is pressed:

def EventKeyW(self):

   text = TextNode('node name')
   text.setText(str(self.x))
   textNodePath = aspect2d.attachNewNode(text)
   textNodePath.setScale(0.07)
   self.x+=1 

it’s working, but the new numbers are just displayed over the old numbers/you can’t really read them.
-> how can i remove the old text/rerender the background? thx

Each time you call TextNode(), you are creating a new instance of a node, and then you drop the handle to it so there’s no way to remove it later.

Keep the handle to your TextNode around, and then you can remove it when you create a new one. Or, better yet, don’t even bother to create a new one; once you have the handle to the old one, you can just change the text.

Do something like this instead:

def __init__(self):
    self.text = TextNode('node name')
    textNodePath = aspect2d.attachNewNode(self.text)
    textNodePath.setScale(0.07)

def EventKeyW(self): 
    self.text.setText(str(self.x))

David