Sprite following mouse with a delay
by Steve Miles · in Torque Game Builder · 09/28/2005 (7:20 am) · 12 replies
I'm developing a zuma like game where the "gun" at the bottom of the screen is following the mouse left/right on a straight line. I've added the code to detect the mouse position and have the sprite follow, but as I move back and forth things look a bit choppy. Looking at other games, it seems like the sprite is following the mouse action with a very slight delay which sort of smooths things out. Is there a way to do this directly?
I was thinking that I would have to first have the mouse move a small invisible sprite which then I'd mount the gun to and have the delay set there. I still have to try this and test it, but it seems if there was a "mouse follow delay" command that would be more straighforward.
Steve
I was thinking that I would have to first have the mouse move a small invisible sprite which then I'd mount the gun to and have the delay set there. I still have to try this and test it, but it seems if there was a "mouse follow delay" command that would be more straighforward.
Steve
About the author
#3
09/28/2005 (8:36 am)
Matt's would kind of work, but for what you need it to do, the solution Hans pointed out would probably suit your situation a little better.$mouse = new fxSceneobject2D() { sceneGraph = t2dSceneGraph; };
$cursor = new fxStaticSprite2D() { sceneGraph = t2dSceneGraph; };
// ...setup $cursor sprite stuff
$cursor.mount($mouse, "0 0", 5); // 5 denotes the mount force. Adjust as necessary.
function t2dSceneGraph::onUpdateScene()
{
%mouse = sceneWindow2D.getMousePosition();
$mouse.setPosition(%mouse);
}
#4
09/28/2005 (8:40 am)
@Teck: Good example on how to do that... in my pack I cover doing the setPosition method and making an object "follow" it :)
#5
09/28/2005 (8:42 am)
Add: the C++ tutorial I have in there shows a more efficient way than through script if you wanted to go to the source, though Teck's snippet is a great solution for using script though :)
#6
09/28/2005 (8:43 am)
Matt, good point, I completely forgot about that one. :p
#7
09/28/2005 (8:43 am)
=p I figured, lol jk... actually your snippet is quite good :) Probably cleaner than doing what I suggested
#8
This doesn't seem to be working - the image just shutters and appears to be stuck. Am I getting the X coordinate appropriately here?
09/28/2005 (10:37 am)
Thanks for your input and examples....I think getMousePosition is one of the functions that's obscured because of the bug in the PDF reference file. I'm limiting the movement to the X axis, and my code change is like so:function t2dSceneGraph::onUpdateScene()
{
%mouse = sceneWindow2D.getMousePosition();
%mxpos = getWord(%mouse,0);
$mouse.setPositionX(%mxpos);
}This doesn't seem to be working - the image just shutters and appears to be stuck. Am I getting the X coordinate appropriately here?
#9
09/28/2005 (10:53 am)
Strange. That looks like it should work (tried it on one of my objects just to double check). Maybe there's a bug somewhere else in the code?
#10
it's a bit jumbled, but basically this is in a player.cs file that I exec from client.cs, and the call the createPlayer function from client.cs. It seems like it should work, but the player ship just sits in the middle of the screen and doesn't move.
09/28/2005 (12:04 pm)
Well, now I opened a can of worms - I tried to simplify things, but still can't get the image to follow the cursor. function createPlayer()
{
$mouse = new fxSceneObject2D() { sceneGraph = t2dSceneGraph; };
$player = new fxStaticSprite2D() { sceneGraph = t2dSceneGraph; };
$player.setSize( "20 10" );
$player.setImageMap( playerShip );
$player.isSelectable = true;
$player.mount($mouse, "0 0", 5); // 5 denotes the mount force. Adjust as necessary.
}
function t2dSceneGraph::onUpdateScene()
{
%mouse = sceneWindow2D.getMousePosition();
$mouse.setPosition(%mouse);
}it's a bit jumbled, but basically this is in a player.cs file that I exec from client.cs, and the call the createPlayer function from client.cs. It seems like it should work, but the player ship just sits in the middle of the screen and doesn't move.
#11
edit: Have you possibly used different naming conventions for your scenewindow and scenegraph? (ie, deviated from t2dsceneGraph)
09/28/2005 (12:36 pm)
There may be something else going on. I just tried setting everything up, and the mounted object flies over to the dummy sceneObject without a hitch.edit: Have you possibly used different naming conventions for your scenewindow and scenegraph? (ie, deviated from t2dsceneGraph)
#12
10/28/2005 (10:11 am)
Isn't it the way that you have to use fxSceneGraph2D::onUpdateScene(%this) instead of t2dSceneGraph? (ran into that as well, didn't call the function ... because actually its still the fx naming not t2d as in the upcomming T2D 1.1)
Torque Owner Hans Sjunnesson
--
Hans