Game Development Community

onPositiontarget

by Jebaraj Daniel · in Torque Game Builder · 07/26/2011 (5:05 am) · 3 replies

in my dice game,i need move two or more dices in same direction and first dice value should be reduced when dice reach the particular position,i used onMouseDragged() to move sprites and use OnPositionTarget to reduce the dice value. My Prob is all Moved dices Value are reduced at a time when OnPositionTarget function call.,i need just one sprite value will be reduce when i move more sprites at a time.., help me


code:
function DiceClass :: onMouseDragged(%this,%mod,%worldPos)
{
if(!%this.firstX) %this.firstX = %worldpos.X;
if(%this.firstX+0.5 < %worldpos.X)
{
if(((Dice@($DiceNo+1)).getFrame()) == 0)
{
%this.MoveTo((Dice@($DiceNo+1)).getPositionX(),%this.getPositionY(),20,1,1,1,0.01);
%this.schedule(500,setName,Dice@($DiceNo+1));
(Dice@($DiceNo+1)).MoveTo(%this.getPositionX(),%this.getPositionY(),20,1,1,1,0.01);
(Dice@($DiceNo+1)).schedule(500,setName,Dice@($DiceNo));
}
else if(((Dice@($DiceNo+1)).getFrame()) != 0)
{
%this.MoveTo((Dice@($DiceNo+1)).getPositionX(),%this.getPositionY(),10,1,1,1,0.01);
%this.schedule(500,setName,Dice@($DiceNo+1)); (Dice@($DiceNo+1)).MoveTo((Dice@($DiceNo+2)).getPositionX(),%this.getPositionY(),10,1,1,1,0.01);
(Dice@($DiceNo+1)).schedule(500,setName,Dice@($DiceNo+2));
(Dice@($DiceNo+2)).schedule(500,setPosition,(Dice@($DiceNo)).getPositionX(),%this.getPositionY());
(Dice@($DiceNo+2)).schedule(500,setName,Dice@($DiceNo));
}
}
}

function DiceClass :: OnPositionTarget(%this)
{
$No=stripchars(%this.getName(),Dice);
echo("Target" SPC $DiceNo);
%frame=(Dice@($DiceNo)).getFrame();
if(%frame != 0)
{
//if($DiceNo == $No)
(Dice@($DiceNo)).setFrame(%frame-1);
for(%i=1;%i<=49;%i++)
{
if((Dice@%i).getFrame() == 0)
{
(Dice@%i).setUseMouseEvents(0);
}
else
{
(Dice@%i).setUseMouseEvents(1);
}
}
}
}

#1
07/26/2011 (5:57 am)
Hi there,

since you're moving multiple sprites at the same time, OnPositionTarget will get called multiple times (once for each object instance of the same DiceClass "class")

You need an early exit check in this method, to make sure it's only applying its logic on one of the instances.
Maybe you can identify which instance you want to modify (based on the name or current value, and then use these to check which one needs to be processed) ?
If not, maybe you can use a bool script global used as a flag, so that the logic is applied only the first time OnPositionTarget is called (the flag then gets reset every time you drag a new set of dice)

Let me know if that helps at all.

Regards
#2
07/27/2011 (5:02 am)
hi raphael,

your info really helpful to my script,thanks
#3
07/27/2011 (10:59 am)
Hi Jebaraj. Raphael explained that your DiceClass::OnPositionTarget is called once for each moving die and you have to discern which die it is called for.

If you want OnPositionTarget called for the *current* die, you could use a behavior. The "Behavior Playground" example is great for wrapping your head around this idea, and there is an entire section of the tutorial for behaviors.