 # Reduce Lag... Help!

Currently, I am creating a flash document where the main character can shoot a shotgun that has 7 bullets come out of it. This is a picture of where the bullets go and what they look like: These are all inside the instance of “man” (the stick man in the picture). I needed to find a way to check if they hit the enemy, so I came up with the following code:

``````onClipEvent(enterFrame){
xc = _root.man._x;
yc = _root.man._y;
r = 1500;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
if(_root.man.arms.shotgun_shoot._currentframe == 7){
//the "bullet_zone" is a zone around the bullets. This is for reducing the amount of enemies I have to check
if(_root.man.arms.shotgun_shoot.bullet_zone.hitTest(this)){
a += 0.0872;
while(xnp != xc){
while(ynp != yc){
if(r < 0){
xnp = xc;
ynp = yc;
}if(this.hitTest(xnp, ynp, true)){
this.play();
xnp = xc;
ynp = yc;
}else{
r -= 5;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
}
}
a -= 0.0872;
r = 1500;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
while(xnp != xc){
while(ynp != yc){
if(r < 0){
xnp = xc;
ynp = yc;
}if(this.hitTest(xnp, ynp, true)){
this.play();
xnp = xc;
ynp = yc;
}else{
r -= 5;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
}
}
a += 0.1745;
r = 1500;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
while(xnp != xc){
while(ynp != yc){
if(r < 0){
xnp = xc;
ynp = yc;
}if(this.hitTest(xnp, ynp, true)){
this.play();
xnp = xc;
ynp = yc;
}else{
r -= 5;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
}
}
a -= 0.1745;
r = 1500;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
while(xnp != xc){
while(ynp != yc){
if(r < 0){
xnp = xc;
ynp = yc;
}if(this.hitTest(xnp, ynp, true)){
this.play();
xnp = xc;
ynp = yc;
}else{
r -= 5;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
}
}
a += 0.2617;
r = 1500;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
while(xnp != xc){
while(ynp != yc){
if(r < 0){
xnp = xc;
ynp = yc;
}if(this.hitTest(xnp, ynp, true)){
this.play();
xnp = xc;
ynp = yc;
}else{
r -= 5;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
}
}
a -= 0.2617;
r = 1500;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
while(xnp != xc){
while(ynp != yc){
if(r < 0){
xnp = xc;
ynp = yc;
}if(this.hitTest(xnp, ynp, true)){
this.play();
xnp = xc;
ynp = yc;
}else{
r -= 5;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
}
}
r = 1500;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
while(xnp != xc){
while(ynp != yc){
if(r < 0){
xnp = xc;
ynp = yc;
}if(this.hitTest(xnp, ynp, true)){
this.play();
xnp = xc;
ynp = yc;
}else{
r -= 5;
xp = xc + r * Math.cos(a);
yp = yc + r * Math.sin(a);
xnp = xp;
ynp = yp;
}
}
}
}
}
}
``````

This is on the enemy. Basically, if the “bullet_zone” hits the enemy, then Flash finds the outermost point of one of the bullet lines, using the bullet line’s length, “man”'s rotation, and “man”'s _x and _y coords. Then, Flash checks every point (separated by 5 pixels) on that line to see if the bullet is hitting the enemy. Finally, the process repeats its self 6 more times for all the bullets.

It’s basically a way around the fact that using a hitTest for this wouldn’t work because the boundary box for my bullets would be huge, and they would be hitting all the enemies on the screen.

Flash can easily do all that without much lag…
But what if there were 50 enemies?
Well, then changing the code to reduce lag would be a good thing to do!

Unfortunately, I’m not that good at reducing lag on my projects, so I was hoping for some help!

Please ask if there is something you don’t understand in the script, or if there are any other problems.

Thanks!