Game Development Community

Drag and Drop

by Kevin James · in Torque Game Builder · 04/08/2005 (8:13 pm) · 30 replies

I have characters on one side in the scenegraph and a window on the other side containing sprites. I want to drag and drop these sprites onto the characters using the mouse. Will this work from a gui screen (drag) to the scenegraph(drop)?

Does anyone have example script of drag and drop?

About the author

Computer security, digital forensics, and platform jumper enthusiast. shells.myw3b.net/~syreal/

Page «Previous 1 2
#2
04/08/2005 (9:06 pm)
Thanks King Bob!
#3
04/08/2005 (9:29 pm)
Sure :) btw the first tutorial will probably be all you need... the second one goes over a more advanced method... the third one goes over debuging info... I very much recommend them, but the simple selection 'should' work fine for what your doing
#4
04/09/2005 (1:19 pm)
I'm following the tutorial and this is what is happening to me:
in onMouseDown I have this (along with everything else above it):
if(%selected)   
   { //we then store that object as the selectedObj      
        %this.selectedObj = %obj;
        %this.objectSelected = true;
    echo("objectselected=" SPC %this.objectSelected);   
   }

and the echo shows true.

but... in onMouseDragged it echos false:
//lets store the mouses position   
   %this.mousePos = %worldPos;
   %this.objectFollowCheck();
    echo("objectselected=" SPC %this.objectseleted);

However, if I echo this in OnMouseDragged:
echo("You have dragged:" SPC %this.selectedObj.objectname);

It echos "Test Box" as expected.

As a result of all of this, my tile does not move:
function fxSceneWindow2D::objectFollowCheck()
{
    if(%this.objectSelected){   
        %obj = %this.selectedObj;      
        %mousePos = %this.mousePos;         
        %obj.setPosition(%mousePos);        
    }
}

because %this.objectSelected is not keeping its value. I'm gonna try and rewrite this to work, but I wanted you to be aware of my experience. I assume this only acts this way for me.
#5
04/09/2005 (1:44 pm)
Doh! I left out %this from objectFollowCheck()

So now it works. Very cool.
#6
04/09/2005 (1:49 pm)
Now it wont let me selet anything else. is there something special that needs done for animated sprites or mounted sprites?
#7
04/09/2005 (2:23 pm)
Remember set a property to the object you want selected... ".isSelectable = true;"

so say you have $object

$object.isSelectable = true;

$player.isSelectable = true;

or you can comment out my loop that checks if an object is selectable, though it will only pick one out of a stack of objects if they're layered :O)
#8
04/09/2005 (2:39 pm)
Yes, I did that.

For some reason, in here:
for(%i=0;%i<%objCount;%i++)   {      
       //grabing the entry corresponding to the loop      
           %obj = getWord(%objList, %i);            
           echo("You have selected:" SPC %obj);

%obj returns me the same number, no matter what guy I click. I have several rows and columns of characters in multiple layers, but its not finding the right one for some reason.
#9
04/09/2005 (3:05 pm)
I stuck in %selected = true; right after the loop and the object that gets moved is the background.... which is 1 in the list, 0 is not an object. This problem goes way beyond having my IsSelecable turned on- its not seeing my sprites at all.
#10
04/09/2005 (3:25 pm)
Right now its returning the first object in the list, which is the background... to make it select the topmost object do this

%objCount = getWordCount(%objList) - 1;
%obj = getWord(%objList, %objCount);                       
echo("You have selected:" SPC %obj);

that way it picks the last item in the list...
#11
04/09/2005 (3:29 pm)
By specifying isSelectable tells it to pick the first one with that option toggled to true...

objList = t2dSceneGraph.pickPoint(%worldPos);

can be changed to use group and layer masks

objList = t2dSceneGraph.pickPoint(%worldPos, BIT(1), BIT(1));

so you can fine tune it even more
#12
04/09/2005 (3:30 pm)
Thru tedious trial and error I narrowed it down to this:
$Character[%n].setCollisionPolyCustom(4,"-0.5 1 0.5 1 0.5 -0.7 -0.5 -0.7");

when i take that out, I can pick him up with the mouse. Ummm... I really do need to have a custom poly. I'm just grasping at straws here, but does the order of setting these properties matter?
setPosition( "0 0" );
setGroup(1 ); 
setLayer(1); 
setCollisionActive(true,true ); 
setCollisionPolyCustom(4,"-0.5 1 0.5 1 0.5 -0.7 -0.5 -0.7"); 
setCollisionMasks(BIT(2),BIT(2)); 
setCollisionCallback(true );
playAnimation($Character[%n].Taunt);
#13
04/09/2005 (3:31 pm)
One thing to keep in mind, I think the pick function works off of collision polys or bounding boxes, so if you set a custom poly you will probably have to click within that collision poly (I'm not completely sure though

your set up seems fine
#14
04/09/2005 (3:39 pm)
Quote:
Right now its returning the first object in the list, which is the background... to make it select the topmost object do this
With custom poly turned on, there's only 1 object in the list, the background.

Quote:
objList = t2dSceneGraph.pickPoint(%worldPos, BIT(1), BIT(1));

i tried that, but it still doesn't work (when using poly)
#15
04/09/2005 (3:42 pm)
If collision is active you must select the object within the collision poly
#16
04/09/2005 (3:42 pm)
My poly narrows the square around the sprite for better precision. Its not hard to click within it. I'll try adjusting the size and see what happens.
#17
04/09/2005 (3:43 pm)
Ok now I'm really confused... when i set the collision poly to this

$test.setCollisionPolyCustom( 5, "-0.9 0 0 -0.6 1 -0.3 1 0.3 0 0.5" );

it works selecting within the collision poly

when i set it to yours it doesnt

$test.setCollisionPolyCustom(4,"-0.5 1 0.5 1 0.5 -0.7 -0.5 -0.7");
#18
04/09/2005 (3:48 pm)
Very interesting...
#19
04/09/2005 (3:49 pm)
Hmm, yeah, even realtime... wonder if its some sort of bug
#20
04/09/2005 (3:58 pm)
Setting collsion poly to one from tutorial (for enemies)
www.razedskyz.com/games/torque/pickPointProb/pickPoint1.JPGclick and moving
www.razedskyz.com/games/torque/pickPointProb/pickPoint2.JPGmoving works
www.razedskyz.com/games/torque/pickPointProb/pickPoint3.JPGset collision to the new poly, its accepted and set
www.razedskyz.com/games/torque/pickPointProb/pickPoint4.JPGclick and moving doesnt work
www.razedskyz.com/games/torque/pickPointProb/pickPoint5.JPG
Page «Previous 1 2