Jump to content


Need help in prioritisation AI ( action script )


5 replies to this topic

#1 Khadgar

    New Member

  • Members
  • PipPip
  • 14 posts

Posted 10 January 2008 - 08:53 AM

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

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 5305 posts
  • LocationBellevue, WA

Posted 10 January 2008 - 12:08 PM

For future reference, please use the [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...
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 Khadgar

    New Member

  • Members
  • PipPip
  • 14 posts

Posted 10 January 2008 - 03:57 PM

Reedbeta said:

For future reference, please use the [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.

#4 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 10 January 2008 - 04:18 PM

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.
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#5 Khadgar

    New Member

  • Members
  • PipPip
  • 14 posts

Posted 11 January 2008 - 08:39 AM

monjardin said:

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.

#6 Nodlehs

    Valued Member

  • Members
  • PipPipPip
  • 152 posts

Posted 16 January 2008 - 08:28 PM

Khadgar said:

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.

And if the error is in your bubble sort? I would suggest looking at all your code again, cleaning it up as you go along. If that doesn't fix your problem, then post the entire code here.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users