Items does not attach to mouse
by Georgina Benedetti · in Torque Game Builder · 01/30/2011 (2:51 pm) · 11 replies
Hey there,
I have these items I have created and i want to be able to pick them up with the mouse. Easy enough, so I programmed that in but, for some reason the item does not want to stay attached to the mouse for more than a few second so I keep "dropping" the item. Does anyone have any idea as to why this may be occurring?
I have these items I have created and i want to be able to pick them up with the mouse. Easy enough, so I programmed that in but, for some reason the item does not want to stay attached to the mouse for more than a few second so I keep "dropping" the item. Does anyone have any idea as to why this may be occurring?
#2
Will
01/30/2011 (7:11 pm)
GB: Without seeing your code, we can't tell from this end. Sounds like you programmed something wrong, why not just use a behav for what you need? There are plenty of them and sounds like you might be able to find what your looking for there... just a thought. Good luck.Will
#3
function itemClass::onMouseDown(%this, %modifier, %worldPosition, %clicks)
{
%this.setPosition(%worldPosition);
%this.setLinearVelocityY(0);
%this.setLayer(1);
}
function itemClass::onMouseDragged(%this, %modifier, %worldPosition, %clicks)
{
%this.setPosition(%worldPosition);
%this.setLinearVelocityY(0);
%this.setLayer(1);
}
01/31/2011 (3:48 pm)
Here's my code;function itemClass::onMouseDown(%this, %modifier, %worldPosition, %clicks)
{
%this.setPosition(%worldPosition);
%this.setLinearVelocityY(0);
%this.setLayer(1);
}
function itemClass::onMouseDragged(%this, %modifier, %worldPosition, %clicks)
{
%this.setPosition(%worldPosition);
%this.setLinearVelocityY(0);
%this.setLayer(1);
}
#4
First, to test that you had the same problem, try dragging very slowly and see if you hold it the whole time. And by slowly, I mean just a couple of pixels a second.
The fix the problem, I had to use window mouse events. The short of it was that I would get and store the object selected "onMouseDown" and then use "t2dSceneWindow::onMouseDragged" to move that object and then "t2dSceneWindow::onMouseUp" to release that object.
Check this thread out to get more specific code.
01/31/2011 (3:57 pm)
I've had this problem before and it dealt with moving the mouse a little "too" fast.First, to test that you had the same problem, try dragging very slowly and see if you hold it the whole time. And by slowly, I mean just a couple of pixels a second.
The fix the problem, I had to use window mouse events. The short of it was that I would get and store the object selected "onMouseDown" and then use "t2dSceneWindow::onMouseDragged" to move that object and then "t2dSceneWindow::onMouseUp" to release that object.
Check this thread out to get more specific code.
#5
Thanks a bunch
02/01/2011 (2:35 pm)
Yes that is exactly what is happening with mine. It is alright if you move the mouse slowly, but if I move too quickly the object detaches itself from the mouse.Thanks a bunch
#6
function itemClass::onLevelLoaded(%this, %scenegraph)
{
%this.spawner();
$heldItem = 0;
}
function itemClass::onMouseDown(%this, %modifier, %worldPosition, %clicks)
{
$heldItem = %this;
}
function sceneWindow2D::onMouseDragged(%this, %modifier, %worldPosition, %clicks)
{
if($heldItem == 0)
{
return;
$heldItem.moveTo(%worldPosition);
}
}
function sceneWindow2D::onMouseUp(%this, %modifier, %worldPosition, %clicks)
{
if($heldItem == 0)
{
return;
$heldItem.moveTo(%worldPosition);
$heldItem = 0;
}
}
function itemClass::onMouseUp(%this, %modifier, %worldPosition, %clicks)
{
%itemPositionX = %this.getPositionX();
%itemPositionY = %this.getPositionY();
if(%itemPositionX < 42 && %itemPositionX > -39)
{
%this.setLinearVelocityY(50);
}
else if(%itemPositionX < -138 && %itemPositionX > -190 && %itemPositionY < -5 && %itemPositionY > -48)
{
%this.setLinearVelocityY(50);
%this.fall();
%this.setLayer(2);
if(%this.frame < 4)
{
$score++;
scoreText.text = "Score =" SPC $score;
}
else if(%this.frame > 4)
{
$score--;
scoreText.text = "Score =" SPC $score;
}
}
else if(%itemPositionX < 184 && %itemPositionX > 140 && %itemPositionY < -5 && %itemPositionY > -42)
{
%this.setLinearVelocityY(50);
%this.fall();
%this.setLayer(2);
if(%this.frame > 4)
{
$score++;
scoreText.text = "Score =" SPC $score;
}
else if(%this.frame < 4)
{
$alive = false;
echo("DEAD!");
}
}
}
02/01/2011 (3:13 pm)
Alright, I've tried this out, but it doesn't want to pick up the items at all now...here's my code. I already had extra code in my onMouseUp function...function itemClass::onLevelLoaded(%this, %scenegraph)
{
%this.spawner();
$heldItem = 0;
}
function itemClass::onMouseDown(%this, %modifier, %worldPosition, %clicks)
{
$heldItem = %this;
}
function sceneWindow2D::onMouseDragged(%this, %modifier, %worldPosition, %clicks)
{
if($heldItem == 0)
{
return;
$heldItem.moveTo(%worldPosition);
}
}
function sceneWindow2D::onMouseUp(%this, %modifier, %worldPosition, %clicks)
{
if($heldItem == 0)
{
return;
$heldItem.moveTo(%worldPosition);
$heldItem = 0;
}
}
function itemClass::onMouseUp(%this, %modifier, %worldPosition, %clicks)
{
%itemPositionX = %this.getPositionX();
%itemPositionY = %this.getPositionY();
if(%itemPositionX < 42 && %itemPositionX > -39)
{
%this.setLinearVelocityY(50);
}
else if(%itemPositionX < -138 && %itemPositionX > -190 && %itemPositionY < -5 && %itemPositionY > -48)
{
%this.setLinearVelocityY(50);
%this.fall();
%this.setLayer(2);
if(%this.frame < 4)
{
$score++;
scoreText.text = "Score =" SPC $score;
}
else if(%this.frame > 4)
{
$score--;
scoreText.text = "Score =" SPC $score;
}
}
else if(%itemPositionX < 184 && %itemPositionX > 140 && %itemPositionY < -5 && %itemPositionY > -42)
{
%this.setLinearVelocityY(50);
%this.fall();
%this.setLayer(2);
if(%this.frame > 4)
{
$score++;
scoreText.text = "Score =" SPC $score;
}
else if(%this.frame < 4)
{
$alive = false;
echo("DEAD!");
}
}
}
#7
Change them to
02/01/2011 (7:12 pm)
Your problem is with these two functions:function sceneWindow2D::onMouseDragged(%this, %modifier, %worldPosition, %clicks)
{
if($heldItem == 0)
{
return;
$heldItem.moveTo(%worldPosition);
}
}
function sceneWindow2D::onMouseUp(%this, %modifier, %worldPosition, %clicks)
{
if($heldItem == 0)
{
return;
$heldItem.moveTo(%worldPosition);
$heldItem = 0;
}
}Change them to
function sceneWindow2D::onMouseDragged(%this, %modifier, %worldPosition, %clicks)
{
if($heldItem == 0)
return;
$heldItem.moveTo(%worldPosition);
}
function sceneWindow2D::onMouseUp(%this, %modifier, %worldPosition, %clicks)
{
if($heldItem == 0)
return;
$heldItem.moveTo(%worldPosition);
$heldItem = 0;
}
#8
game/gameScripts/naughtyORnice.cs (30): itemClass::moveTo - wrong number of arguments.
game/gameScripts/naughtyORnice.cs (30): usage: (t2dVector positionTarget, float linearSpeed, [bool autoStop = true], [bool callback = false], [bool snap = true], [float targetMargin = 0.1]) Sets a position target for the object and moves toward it.
The object is not actually forced to the target position, it is merely sent there. So, if a collision or some other outside force interferes with the object, it may not make it to the target position. However, the target is still set, so the object may still reach the target in the future.
@param positionTarget The x and y position of the position target.
@param linearSpeed The speed at which the object is setn to the target.
@param autoStop Whether or not to autoStop upon reaching the taret.
@param callback Whether or not to perform a script callback (onPositionTarget()) upon reaching the designated target.
@param snap Whether or not to snap the object to the target when it is within margin.
@param targetMargin The proximity to the target necessary to qualify as reaching the target.
@return No return value.
02/04/2011 (7:01 pm)
The only difference I could see there was the removal of the brackets and by doing that it still does not want to work and comes up with an error;game/gameScripts/naughtyORnice.cs (30): itemClass::moveTo - wrong number of arguments.
game/gameScripts/naughtyORnice.cs (30): usage: (t2dVector positionTarget, float linearSpeed, [bool autoStop = true], [bool callback = false], [bool snap = true], [float targetMargin = 0.1]) Sets a position target for the object and moves toward it.
The object is not actually forced to the target position, it is merely sent there. So, if a collision or some other outside force interferes with the object, it may not make it to the target position. However, the target is still set, so the object may still reach the target in the future.
@param positionTarget The x and y position of the position target.
@param linearSpeed The speed at which the object is setn to the target.
@param autoStop Whether or not to autoStop upon reaching the taret.
@param callback Whether or not to perform a script callback (onPositionTarget()) upon reaching the designated target.
@param snap Whether or not to snap the object to the target when it is within margin.
@param targetMargin The proximity to the target necessary to qualify as reaching the target.
@return No return value.
#9
Your code will never get to the line below the return because when the if($heldItem == 0) evaluates to true, the return; will kick it out of the method.
The code William shared is an "implied" if/else so when the if($heldItem == 0) evaluates to true, it returns. If it evaluates to false, it calls: $heldItem.moveTo(%worldPosition);
The error you're getting is because the if($heldItem == 0) is evaluating to false and the $heldItem.moveTo(%worldPosition); is being called but the %worldPosition is most likely not a valid position.
It should hold a value like : "23 64" or something similar. Try to add an echo right before the $heldItem.moveTo(%worldPosition); and see what %worldPosition's value is.
02/04/2011 (8:30 pm)
Removing those brackets makes a real difference in this case Georgina...Your code will never get to the line below the return because when the if($heldItem == 0) evaluates to true, the return; will kick it out of the method.
The code William shared is an "implied" if/else so when the if($heldItem == 0) evaluates to true, it returns. If it evaluates to false, it calls: $heldItem.moveTo(%worldPosition);
The error you're getting is because the if($heldItem == 0) is evaluating to false and the $heldItem.moveTo(%worldPosition); is being called but the %worldPosition is most likely not a valid position.
It should hold a value like : "23 64" or something similar. Try to add an echo right before the $heldItem.moveTo(%worldPosition); and see what %worldPosition's value is.
#10
02/05/2011 (10:43 am)
It also looks like moveTo needs two arguments... you might want to use setPosition instead of moveTo.
#11
Check the t2dSceneobject methods in the reference for the headers of commonly used functions.
Good to see you on the forums Georgina. By the way, when you're posting code wrap it with code tags. <code>...</code> but with square brackets []
02/06/2011 (6:44 am)
MoveTo takes a whole bunch of arguments:moveTo(t2dVector positionTarget, float linearSpeed,[bool autoStop=true],[bool callback=false],[bool snap=true],[float targetMargin=0.1])
Check the t2dSceneobject methods in the reference for the headers of commonly used functions.
Good to see you on the forums Georgina. By the way, when you're posting code wrap it with code tags. <code>...</code> but with square brackets []
Torque Owner David Helmer
The Kids
Can you provide some of your code for "picking up" objects. I'm sure we'll be able to help.