# AI System in Actionscript - Sorting Arrays

This topic is 4241 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey I'm Patrick. I'm new here... and I have a problem. I'm now working on the battle system for my game and I'm now officially stuck with the enemy AI. To begin with the system I want to be able to have all information about the enemy ready for use. So my step now is finding the enemy's strongest to weakest attacks from an Array. Here's what I have. Code: enemyAttacks = new Array(); for(kP=0;kP<4;kP++){ enemyAttacks[kP] = new Array(); enemyAttacks[kP]['name'] = _root['enemyAttack'+(kP+1)]; enemyAttacks[kP]['ap'] = _root['enemyAttack'+(kP+1)+'_AP']; enemyAttacks[kP]['damage'] = _root['attack_'+enemyAttacks[kP]['name']+'Damage']; } That all works nicely. But sorting it is kind of tricky, considering that it's a pretty intricate array. I need each of these to contain all the info of the attack (name, ap and damage), except in a way that it's ordered from strongest to weakest (or other way around.. doesn't really matter). Could someone give me a headstart here. I'm kinda stuck.

##### Share on other sites
Well, this is more of an idea than an answer, but hopefully it'll give you an idea.

Put this in some old frame in a new movie and observe:

a = new Array();
for( i = 0; i < 10; i++ ){
a = new Array();
a['nifty'] = random( 10 );
}

for( i = 0; i < 10; i++ ){
for( k = 0; k < 10; k++ ){
if( a[k]['nifty'] < a[k-1]['nifty'] ){
temp = a[k];
a[k] = a[k-1];
a[k-1] = temp;
}
}
}

for( i = 0; i < 10; i++ ){
trace( a['nifty'] );
}

Hope this helps!

##### Share on other sites
You should be able to do a sortOn() of the array. If you do a search of the actionscript dictionary or the Macromedia LiveDocs it should tell you how to use it.

##### Share on other sites
Thanks guys - I got some help on another forum and yeah, it did involve the sortOn thing. It's pretty complicated.. o_o' But thanks anyway.. It's working fantastically. Although writing an AI system that works effectively isn't the easiest thing in the world... I'm worried. :P

##### Share on other sites
That's probably the most difficult part of game programming. Especially if you want your computer opponents to not spend days thinking about their move!

##### Share on other sites
Agreed. I think I'm going to make it a bit simple... but.. yeah. How many 14 year olds make fully functional AI scripts?

Basically all he does as of now is, depending on how intelligent he is (a set number between 0 and 100), makes either a random move, or a systematic one. If it's random he just takes any attack out of four, but if he goes for systematic.. then he checks for.. user health/initial health/is the user better off and such and such.

It's not movement either so that makes the whole thing a lot simpler for me. It's just one of them battle engines similar to the one from them Pokémon games. XD

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631708
• Total Posts
3001839
×