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.
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.