Jump to content


Lagging


  • You cannot reply to this topic
2 replies to this topic

#1 Ben

    P2L Jedi Master

  • Publishing Betazoids
  • PipPipPipPip
  • 1,366 posts
  • Gender:Male
  • Location:VIC, Australia

Posted 16 October 2006 - 05:09 AM

I'm making this game that requires a lot of for loops, and movie attachments. And For some reason it lags like crazy after about 30 seconds. I dont know what I'm doing wrong, because I'm removing the balls when they go past the screen. I suspect it has something to do with the for loops, but I dont know what to do. If someone could help me that would be great.

Heres the code:
var depth:Number = 0;
var depth2:Number = 0;
var totalBalls:Number = 9
attachEnemy = setInterval(enemyFunc, 2000);
onMouseMove = function() {
	gunMc._rotation = Math.atan2((_root._ymouse-gunMc._y), (_root._xmouse-gunMc._x))*180/Math.PI;
}
onMouseDown = function() {
	shootBalls();
	shootInter = setInterval(shootBalls, 200);
}
onMouseUp = function() {
	clearInterval(shootInter);
}
onEnterFrame = function() {
	if(depth2 > 50) {
		clearInterval(attachEnemy);
	}
	for(var i2 = 0; i2 <= depth; i2++) {
		bullets = _root["ball"+i2];
		bulX = bullets._x;
		bulY = bullets._y;
		for(var i = 0; i < depth2; i++) {
			ball = enemyHold["enemy"+i];
			ballWidth = (ball._width/2);
			distx = bulX-ball._x;
			disty = bulY-ball._y;
			distance = Math.sqrt(distx*distx+disty*disty);
			if(distance < ((ball._width/2)-(bullets._width/2))) {
				mag1 = Math.sqrt(bullets._xMov*bullets._xMov+bullets._yMov*bullets._yMov);
				bullets._xMov = mag1*distx/distance;
				bullets._yMov = mag1*disty/distance;
				ball._xMov -= (mag1*distx/distance)/bullets._width;
				ball._yMov -= (mag1*disty/distance)/bullets._width;
			}
			if(bullets._x > (Stage.width+(bullets._width/2)) || bullets._x < -(bullets._width/2) || bullets._y > (Stage.height+(bullets._height/2)) || bullets._y < -(bullets._height/2)) {
				bullets.removeMovieClip();
			}
			if(ball._x > (Stage.width+(ball._width/2)) || ball._x < -(ball._width/2) || ball._y > (Stage.height+(ball._height/2)) || ball._y < -(ball._height/2)) {
				ball.removeMovieClip();
			}
		}
	}
}
function shootBalls() {
	newBall = "ball"+depth;
	angle = gunMc._rotation*Math.PI/180;
	_root.attachMovie("ball", newBall, depth);
	_root[newBall]._x = gunMc._x+30*Math.cos(angle);
	_root[newBall]._y = gunMc._y+30*Math.sin(angle);
	_root[newBall]._xMov = 10*Math.cos(angle);
	_root[newBall]._yMov = 10*Math.sin(angle);
	_root[newBall].onEnterFrame = function() {
		this._x += this._xMov;
		this._y += this._yMov;
	}
	depth++;
}
function enemyFunc() {
	newEnemy = "enemy"+depth2;
	enemyHold.attachMovie("enemy", newEnemy, depth2);
	enemyHold[newEnemy]._x = random(Stage.width);
	enemyHold[newEnemy]._y = random(Stage.height);
	enemyHold[newEnemy]._rotation = Math.atan2((gunMc._y-enemyHold[newEnemy]._y), (gunMc._x-enemyHold[newEnemy]._x))*180/Math.PI;
	angle2 = enemyHold[newEnemy]._rotation*Math.PI/180;
	enemyHold[newEnemy]._speed = (random(4)+1);
	enemyHold[newEnemy]._xMov = enemyHold[newEnemy]._speed*Math.cos(angle2);
	enemyHold[newEnemy]._yMov = enemyHold[newEnemy]._speed*Math.sin(angle2);
	enemyHold[newEnemy].onEnterFrame = function() {
		this._x += this._xMov;
		this._y += this._yMov;
	}
	depth2++;
}
Its for an upcoming game, please dont steal!

#2 PixelHiveDesign

    Young Padawan

  • Members
  • Pip
  • 83 posts
  • Gender:Male

Posted 16 October 2006 - 11:42 AM

When you remove a ball or a bullet you no longer need to check if they they are colliding.

So setup a variable for to keep track of how many have been removed and then have your for loops start at that number.

example:

var ballsRemoved = 0;
var bullsRemoved = 0;

//  Change your loops like.

for(var i2 = ballsRemoved; i2 < depth; i2++){

and 

for(var i = bullsRemoved; i < depth2; i++){

// Remeber to increment the variables when you remove the MC's.

I think you understand it completely but:
After 30 seconds you were still checking every bullet against every ball that ever existed and of course it just keep getting worse as time when on.

-Max (PHD)

Edited by PixelHiveDesign, 16 October 2006 - 11:44 AM.


#3 Ben

    P2L Jedi Master

  • Publishing Betazoids
  • PipPipPipPip
  • 1,366 posts
  • Gender:Male
  • Location:VIC, Australia

Posted 16 October 2006 - 04:29 PM

Wow, thanks, it works fine!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users