0
101 Jan 10, 2008 at 08:53

I still not familiar with action script, so I am not sure whether it is coding problem or logic problem.

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\~\~\~

#### 5 Replies

0
139 Jan 10, 2008 at 12:08

For future reference, please use the …[/code[b][/b]] forum tag around code samples, as this will preserve the indentation and add syntax highlighting. I would add them for you but the indentation of your code has already been removed… [code]…[/code**] forum tag around code samples, as this will preserve the indentation and add syntax highlighting. I would add them for you but the indentation of your code has already been removed…

0
101 Jan 10, 2008 at 15:57

@Reedbeta

For future reference, please use the …[/code[b][/b]] forum tag around code samples, as this will preserve the indentation and add syntax highlighting. I would add them for you but the indentation of your code has already been removed…[/QUOTE] Sorry, this is the first time I post. Thanks for remind. [code]…[/code**] forum tag around code samples, as this will preserve the indentation and add syntax highlighting. I would add them for you but the indentation of your code has already been removed…

Sorry, this is the first time I post. Thanks for remind.

0
102 Jan 10, 2008 at 16:18

I’m having trouble making since of your code. Is this the whole thing? In what function is the “prioritisation” section? If you just need the closest sheep, then is a bubble sort necessary?

Select the first sheep and then iterate over the rest and replace it with any that are closer. That should be a lot quicker & simpler and give the same result.

0
101 Jan 11, 2008 at 08:39

@monjardin

I’m having trouble making since of your code. Is this the whole thing? In what function is the “prioritisation” section? If you just need the closest sheep, then is a bubble sort necessary? Select the first sheep and then iterate over the rest and replace it with any that are closer. That should be a lot quicker & simpler and give the same result.

No, this is not my whole code. My whole code is too messy and I think is not necessary. My prioritisation is just buble sort if-else statement that put inside onEnterFrame function.

0
101 Jan 16, 2008 at 20:28