Game Development Community

[Solved]Moving objects to other objects with 1 function?

by Vlad I · in Torque Game Builder · 10/10/2012 (8:51 am) · 11 replies

I'm trying to move 5 objects to other 5 objects using one function onMouseDown, just like in the mole tutorial.
I can do it all separately just copy and paste the same function 5 times , change the names etc. But I'd like to learn how to script it elegantly in one function using dynamic fields.

I have 5 circles I want to move to 5 boxes, each circle has moleColor Red, Blue, Green, Yellow, White dynamic field.
And I want to move each one at a time to the 5 boxes also corresponding to their moleColor.

So here it is:
function mole::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)
{
   if( %this.getName() $= (%this.moleColor) )
      %this.moveto("What do I put here?"); // Box1.Red, Box2.Blue, Box3.Green, Box4.Yellow, Box5.White ?
}

Is it at all possible?

#1
10/11/2012 (6:53 am)
Try something like this:

function mole::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)  
{ 
switch (%this.moleColor)
{
case 1:
   %this.moveTo(Box1.Red);
case 2:
   %this.moveTo(Box2.Blue);
Case 3: etc...
#2
10/11/2012 (8:14 am)
Nothing happens and no errors.
#3
10/11/2012 (8:16 am)
Does moveTo only use coordinates? like :
%this.moveTo(2,44,40, true);
#4
10/11/2012 (9:23 am)
It can take separate coordinates or a vector but it appears you never gave it either. Unless the .Red or .Blue variable IS a coordinate pair.
%this.moveSpeed can be whatever you want.

function mole::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)    
{   
switch (%this.moleColor)  
{  
case 1:  
   %this.moveTo(Box1.Red.getPosition(),%this.moveSpeed);  
case 2:  
   %this.moveTo(Box2.Blue.getPosition(),%this.moveSpeed);  
Case 3: etc...
#5
10/11/2012 (10:44 am)
I don't understand why it won't move.
I use:
function mole::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)      
{     
   switch (%this.moleColor)    
      {    
         case 1:    
          %this.moveTo(Box1.Red.getPosition(), 40);    
         case 2:    
          %this.moveTo(Box2.Blue.getPosition(),40);  
      }
}

also tried
function mole::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)      
{     
   switch (%this.moleColor)    
      {    
         case 1:    
          %this.moveTo(Box1.Red.getPosition(),%this.moveSpeed);    
         case 2:    
          %this.moveTo(Box2.Blue.getPosition(),%this.moveSpeed);  
      }
}
function moveSpeed()
{
  return (40);
}
#6
10/11/2012 (10:56 am)
The first parameter in the moveTo function should be the destination you want the circle or %this object to move to. The second parameter is the speed at which it travels.

%this.moveTo(Box1.getPosition(),40);
//or maybe...
%this.moveTo(%this.destinationBox,40);//It should hold a coordinate pair
//or maybe even...
%this.moveTo(%this.destinationBox.getPosition(),40);

You'll have to work with it to pick the best solution and make sure to get the variables right.
BTW, I'm going to be AFK for awhile.
#7
10/15/2012 (4:41 am)
Still can't make it work with one function, no errors.
It looks like switch doesn't pass dynamic fields.
function mole::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)        
    {       
       switch (%this.moleColor)      
          {      
             case 1:      
              %this.moveTo(Box1.Red.getPosition(), 40);      
             case 2:      
              %this.moveTo(Box2.Blue.getPosition(),40);    
          }  
    }
If I only put
function mole::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)        
    {       
              %this.moveTo(Box1.Red.getPosition(), 40);      
    }
then it works but all the circles move to the Box1, obviously, after clicking one by one.
Btw. I don't have to have image names ending with colors, right?
#8
10/15/2012 (10:30 am)
It looks pretty good to me but I have had problems with the finicky switch. Maybe make it a "switch$" and/or change the case numbers to strings?

EDIT: I have had problems sometimes putting functions in switch. Maybe you can find a way to take them out but still be sleek about it? You can still use the switch but make it change a variable or something.
#9
10/15/2012 (12:32 pm)
I made it work! :)
function mole::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)      
{     
   switch$ (%this.moleColor)    
      {    
         case "red":    
             %this.moveTo(Box1.getPosition(),40, true);      
         case "blue":    
              %this.moveTo(Box2.getPosition(),40, true);   
      }
}

I had tried to use string comparisons switch$ before without success, but you Alpha made me go back to it and recheck it again.

Eternal gratitude to you both guys! :)
#10
10/15/2012 (2:14 pm)
Oh good!
What's funny is your problems appear to be kinda like brain-teasers sometimes and there is a distinct challenge of trying to work them out. :P
#11
10/15/2012 (3:32 pm)
I almost felt a nerdorgazm :)