Currently I doing a prioritisation AI where my character ( movie clip name: tiger ) will eat the unit ( movie clip name: sheep ) which is nearest to it.
The movie clip "sheep" is an icon which can be drag. When click on it, it will duplicate new movie clip instance and I can drag and drop it into the field I define. When the new sheep instance drop onto the field , the tiger will move toward it, however when I drag another sheep instance and put it relatively nearer to the tiger, the tiger will change target and move to it first.
Here are my codes:
tigerymvspd = 5;
tigerxmvspd = 5;
sheepnum = 0;
D = 0;
_root.onEnterFrame = function()
{
_root.sheep.onMouseDown = function()
{
_root.attachMovie( "sheep", "sheepy_"+sheepnum, ++D );
_root["sheepy_"+sheepnum].startDrag(true);
}
_root.onMouseUp = function()
{
_root["sheepy_"+sheepnum].stopDrag();
sheepnum += 1;
}
if(
_root.tiger.hunger > 0 && sheepnum > 0 &&
_root.tiger._x + 20 < _root.field._x + 200 &&
_root.tiger._x - 20 > _root.field._x - 200 &&
_root.tiger._y + 20 < _root.field._y + 200 &&
_root.tiger._y - 20 > _root.field._y - 200
)
{
//Prioritisation
if( sheepnum > 0 )
{
for( k = 1; k <= sheepnum+1; k++)
{
sheep[k] = _root["sheepy" + k]
distance[k] = Math.sqrt( Math.pow( (
_root["sheepy_" + k]._x -
_root.tiger._x ), 2 ) + Math.pow( ( _root["sheepy_" + k]._y -
_root.tiger._y ), 2 ) );
}
//bubble swap technique
for( j = 0; j < sheepnum; j++ )
{
for( k = 0; k < sheepnum; k++ )
{
if( distance[k] > distance[k+1] )
{
mindistance = distance[k]
distance[k] = distance[k+1]
distance[k+1] = mindistance
mindistance = sheep[k];
sheep[k] = sheep[k+1];
sheep[k+1] = mindistance
}
}
}
return sheep[0];
}
//Tiger Approach Prey
if( _root.tiger._x < _root["sheepy_" + k]._x &&
pathfinding == 0 )
{
_root.tiger._x += tigerxmvspd;
}
if( _root.tiger._y < _root["sheepy_" + k]._y &&
pathfinding == 0 )
{
_root.tiger._y += tigerymvspd;
}
if( _root.tiger._x > _root["sheepy_" + k]._x &&
pathfinding == 0 )
{
_root.tiger._x -= tigerxmvspd;
}
if( _root.tiger._y > _root["sheepy_" + k]._y &&
pathfinding == 0 )
{
_root.tiger._y -= tigerymvspd;
}
}
The result is, the tiger does not move at at =0=
Can any one tell me what is the problem? I squeeze my brain for two week still cannot figure it by myself, so I hope someone can help me on this~~~












