I have it half working but only a single one in the hit Tests. Anyway here is the code that I have for the controller of the hit tests.
onClipEvent (load) {
damage = random(10)+1;
}
onClipEvent (enterFrame) {
for (i=0; i<=40; i++) {
for (j=41; j<=82; j++) {
if (_root["square_blue"+i].hitTest(_root["square_red"+j])) {
_root["square_blue"+i].speed = 0;
_root["square_red"+j].speed = 0;
_root["square_red"+j].health = _root["square_red"+j].health- damage;
damage = random(10)+1;
_root["square_blue"+i].health = _root["square_blue"+i].health - damage;
damage = random(10)+1;
if (_root["square_red"+j].health<=0) {
_root["square_red"+j].removeMovieClip();
_root["square_blue"+i].speed = 5;
}
if(_root["square_blue"+i].health <=0){
_root["square_blue"+i].removeMovieClip();
_root["square_red"+j].speed = 5;
}
}
}
}
}
Alright, the problem is that you're only resetting the speed variable for the square that deals the killing blow, but there may be multiple squares attacking one another at any given time. The quick fix is to just loop through all squares quickly in the seperate loop run before that one and reset all their speed's to 5. Then, the loop you have now will run and set the speed of any squares that are "in combat" back to zero. Here's an example:
onClipEvent (enterFrame) {
for (i=0; i<=40; i++) {_root["square_blue"+i].speed = 5;}
for (j=41; j<=82; j++) {_root["square_red"+j].speed = 5;}
for (i=0; i<=40; i++) {
for (j=41; j<=82; j++) {
if (_root["square_blue"+i].hitTest(_root["square_red"+j])) {
//... the rest of your original loop
A few other suggestions I can make:
Use the += and -= operators, they will save you time and make your ActionScript easier to read.
So, "_root["square_red"+j].health = _root["square_red"+j].health- damage;" becomes just "_root["square_red"+j].health -= damage;"
The "onClipEvent(load)" part seems rather pointless. In fact, the entire "damage" variable is unnecessary unless you intend to add individual classes, and even then it would need to be under the squares' vars and not the controller's. Otherwise, you can just use "... .health -= random(10)+1;"
Overall, it's a pretty solid script. The use of i,j, and names to reference the squares seems a bit overcomplicated, but I think your method is actually more efficient than mine.
Edit: Yes I know it is a rip off of DarkCampaigner's sig, but that is how I learn by mimicing things >_>
Why don't you just PM darkcampaigner?
Man, I need to get my name changed, people keep spelling it right!