PDA

View Full Version : Lagging



JapanMan
February 18th, 2009, 02:39 AM
Hey,

So I'm working on a new game - a Shooter, of sorts. Basically, you have a character (stick man with a pistol) who moves left and right with "A" and "D." His arms follow the mouse cursor, and when you click, a little bang animation plays from the gun.

I want enemies to fly/fall down at him or in from the sides. Making them move and chase him isn't the problem (yet, anyway) though. They are created through an array, and the whole .swf gets really laggy after about 10 enemies have been created.

I'm not sure why this is - I'm using the same script I have on several other games, just altered to chase the player, and it's never lagged before, even when I had over 100 MC's created. Why would this be so laggy?

I'm running at 40 fps, and the array MC's get removed once they reach the bottom of the stage anyway. I even changed the code so that only one is on-screen at a time, but it still lags once I hit 10-15 enemies created.

here's all the code but the part for moving the character ("bullets" are the enemies, FYI):

var bulletSpeed = 8;
var bulletReady = true;
var bulletDelay = 1000;
var bulletArray = [];
var bulletCount = 0;
function createBullets() {
var bulletMc = this.attachMovie("bullet", "bullet"+bulletCount, 1000+bulletCount);
bulletCount++;
bulletType = random(2);
if (bulletType == 0) {
bulletMc._x = -30;
bulletMc._y = 30;
} else {
bulletMc._x = 780;
bulletMc._y = 30;
}
bulletArray.push(bulletMc);
bulletNum++;
}
function moveBullets() {
if ((bulletReady == true) and (bulletNum<1)) {
trace(bulletArray.length);
bulletReady = false;
currentTime = getTimer();
createBullets();
} else {
if (currentTime+bulletDelay<=getTimer()) {
bulletReady = true;
}
}
for (var i = 0; i<bulletArray.length; i++) {
var b = bulletArray[i];
if (b._y<600) {
bulletArray[i]._y += bulletSpeed;
if (b._x>char._x) {
b._x -= (bulletSpeed-3);
} else {
b._x += (bulletSpeed-3);
}
} else {
removeMovieClip(b);
bulletArray.splice(i, l);
bulletNum--;
}
}
}
this.onEnterFrame = function() {
moveBullets();
};If it helps, I can post the .fla and a link to another game I made using very similar principles - just a lower frame-rate - that runs fine even after 20+ minutes of creating MC's.

Thanks.

therobot
February 18th, 2009, 10:59 AM
At a glance I don't see anything that would bog down the cpu here. What are you doing for your player movement code? Are the enemies Movieclips with overly elaborate animations?

I saw a couple minor things you could do to optimize:



bulletArray[i]._y += bulletSpeed;
can be
b._y += bulletSpeed;

and

if (b._x>char._x) {
b._x -= (bulletSpeed-3);
} else {
b._x += (bulletSpeed-3);
}

should be:
bulletSpeed = 5;

//....
if (b._x>char._x) {
b._x -= (bulletSpeed);
} else {
b._x += (bulletSpeed);
}




These are pretty minor things tho. You probably need to post more code or an fla even

JapanMan
February 19th, 2009, 03:19 AM
The reason I had

if (b._x>char._x) {
b._x -= (bulletSpeed-3);
} else {
b._x += (bulletSpeed-3);
}
as I did is because I don't want them to turn as fast as they fall.

The enemies are not elaborate at all, just little red circles.

I can't post the .fla yet, but I will.

JapanMan
February 22nd, 2009, 03:48 AM
Here's the .fla, hope it helps.

http://japanman1080.googlepages.com/2.17.09.fla

Joppe
February 22nd, 2009, 06:59 AM
After looking through your code, the only thing I *think* can be the cause would be the for loop.. It started going slow after ~3000 movieclips had been created, and the only thing still keeping any sort of reference to the first movieclips is the for loop. That one still starts at zero, that and the fact that the array keeps growing bigger while the first entries are gone so its ,,,,,,,bulletX instead of bulletX as the first entry. Honestly I am not the greatest with optimisation, and somehow it all ends up with the same problem you have. But my guess is the fact that both your loop and array grows bigger even thought you dont want it to.

DrRobot
February 22nd, 2009, 02:20 PM
Are you using "hitTest"? if so, i hope that you didn't put it in for loop inside of an enterframe...

therobot
February 22nd, 2009, 04:03 PM
Thanks for posting the fla. You have a typo that is causing your bullet array to continually grow, which will make your for loop grow every time a bullet is unsuccessfully spliced from the array.



//your code:
// you're using a lowercase L for the splice count.
// but you have not defined l as a variable, so it's not actually splicing anything from the array.
bulletArray.splice(i, l);

// it needs to be the number 1, not l.
bulletArray.splice(i, 1);


See if that works better for you.

EDIT: Had you been using AS3, I don't think your code would have compiled and you would have instantly known where the problem was :) AS2 dies silently too often.

JapanMan
February 23rd, 2009, 11:39 AM
It does :)

hourang
February 23rd, 2009, 11:49 AM
also if your bullets are vector i would throw in a bulletMc.cacheAsBitmap.