Ok i have designed a cool way of controlling my ship but i will need some output (text on the screen) to help me tweak it to make it realistic. So i need to first of all get my mouse coordnintes on the screen. I have put this line of code into my code:
okay… and the aspect2d coordinate space is -1 to 1 on both axes? If so, I’m not seeing anything flagrantly wrong with that line in particular.
You might try a simple print statement to verify that execution is even reaching the textObject declaration (and if that’s in a loop, you’re going to want to reconsider how you set it up, otherwise you’re going to have a zillion rogue textNodes building up in the scene over time).
Well, if you can verify that it’s getting to the point right before the if statement, and it’s not executing anything in the if statement, than the if condition is failing. If that’s the case, I’ve never used mouseWatcherNode much, but I’d guess there’s something you need to do to prime/initialize/enable it if it’s not detecting your mouse by default.
Even if you take away the if statement it still gives you a crazy error:
AssertionError: _has_mouse at line 65 of built/include/mouseWatcher.I
I didnt do any changes to the config files, in fact, i dont even know how to yet
so am i supposed to change something in it to fix my problem?
Thanks for any help
The text is never created because hasMouse() returns False, i.e. the mouse is not found within the frame.
What you should be doing is listening for mouse movement, and then destroy() and create a new text overlay every time the mouse X position changes.
EDIT: Here’s some code that might help…
import direct.directbase.DirectStart
from pandac.PandaModules import *
import sys
from direct.gui.OnscreenText import OnscreenText
from direct.task import Task
hasMouse = False
lastX = 0
lastY = 0
text = None
font = loader.loadFont("cmss12")
def mouseMoved(x, y):
updateText('Mouse X position: %g' % x)
def mouseLost():
updateText('Mouse lost')
def updateText(msg):
global text
if text is not None:
text.destroy()
text = OnscreenText(text=msg, style=1, font=font,
fg=(0,0,0,1), align=TextNode.ALeft, pos=(-1.15, .8), scale=.07)
#Task to move the camera
def MouseMotion(task):
global lastX, lastY, hasMouse
if base.mouseWatcherNode.hasMouse():
hasMouse = True
x = base.mouseWatcherNode.getMouseX()
y = base.mouseWatcherNode.getMouseY()
if x!=lastX or y!=lastY:
mouseMoved(x, y)
lastX = x
lastY = y
elif hasMouse:
hasMouse = False
mouseLost()
return Task.cont
taskMgr.add(MouseMotion, 'MouseMotion')
updateText('Mouse lost')
run()