Restarting a object when no longer in a hit test

Started by: Steyene | Replies: 5 | Views: 688

Steyene

Posts: 2,060
Joined: Apr 2006
Rep: 10

View Profile
Aug 25, 2008 1:20 AM #232917
All right, once more fellow SPPians I need your help. I have several object colliding, and I have told them to stop when they collide. But once one or the other has "died" I want the remaining one to move on. But I can't work it out.

I have it half working but only a single one in the hit Tests. Anyway here is the code that I have for the controler 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;
}
}
}
}
}

Here is the over all result with that code and the rest of the stuff.

[swf="http://fwup.net/file/0ueyf4km1p.swf"]height=200 width=600[/swf]

Edit: Yes I know it is a rip off of DarkCampaigner's sig, but that is how I learn by mimicing things >_>
Steyene

Posts: 2,060
Joined: Apr 2006
Rep: 10

View Profile
Aug 25, 2008 9:55 AM #233238
Bump .
Bonk
2

Posts: 2,778
Joined: Mar 2008
Rep: 10

View Profile
Aug 25, 2008 1:47 PM #233335
Why don't you just PM darkcampaigner?
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Aug 25, 2008 10:40 PM #233917
Quote from Steyene
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.

Quote from Steyene

Edit: Yes I know it is a rip off of DarkCampaigner's sig, but that is how I learn by mimicing things >_>


Quote from Bonk
Why don't you just PM darkcampaigner?


Man, I need to get my name changed, people keep spelling it right!
Steyene

Posts: 2,060
Joined: Apr 2006
Rep: 10

View Profile
Aug 25, 2008 11:11 PM #233966
Lawl thanks >_>

I remember the good old times when no one would spell your name right >_>.

Thanks I will try that :D

Yours still kicks ass with automatic resize >_>
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Aug 26, 2008 12:49 AM #234141
Quote from Steyene
Lawl thanks >_>

I remember the good old times when no one would spell your name right >_>.

Thanks I will try that :D

Yours still kicks ass with automatic resize >_>


If you want, I can show you how to set up resizing.

Also, your signature seems to be eating up a bit more CPU than usual. You should look into optimizing it a bit. Since your controller is running that loop only once per frame, it shouldn't be the problem. Do you have any similar loops running anywhere else?