PDA

View Full Version : AS 2.0 - Cannon game problems


ScionOfWar
09-06-2008, 10:28 PM
I'm working on a cannon game, works great so far with the exception of lag problems. I don't know what's causing it to lag, since all the movie clips get deleted, could anyone help me out? The lag really ruins the game after only about one minute, it's obnoxious.

Also, feel free to use and manipulate this code for your own purposes! Consider it a gift? >.>

The objects on the screen is a dynamic text box (score_txt), a red hexagon (restart), and two bars which scroll across the screen and block incoming enemies (bar1 and bar2).

The game looks something like this so far, (though the bullets in the below script are stopped by the bars and the score resets when you hit the restart button): http://www.mediafire.com/download.php?nmwaqabahps


Edit: I'm almost positive the cause of the lag is in the last part of the code, the restart.onRelease function. I tried surviving for as long as possible without having to restart and never experienced lag until I died and restarted.


All of the code is on frame one:



stop();
score_txt.text = score.toString();
// begin 'evil' unit with cannon rotation
var start_enemy_y = 0;
enemies = [];
evilBreak = setInterval(evilSpawn, 1000);
function evilSpawn() {
start_enemy_x = Math.round(Math.random()*650);
speedy = Math.round(Math.random()*5) + 1;
speedx = Math.round(Math.random()*5) - 2;
evilguy = attachMovie("evil", "evil"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:start_enemy_x, _y:start_enemy_y});
enemies.push(evilguy._name);
evilguy.onEnterFrame = function() {
this._y += speedy;
this._x += speedx;
for(x in enemies){
if (this.hitTest(_root[turret1[x]])) {
_root[turret1[x]].removeMovieClip();
this.removeMovieClip();
turret1.splice(x, 1);
}
if (this.hitTest(_root.bar)) {
this.removeMovieClip();
}
if (this.hitTest(_root.bar2)) {
this.removeMovieClip();
}
}
mousex = turret._x;
mousey = (turret._y)*-1;
angle = Math.atan(mousey/mousex)/(Math.PI/180);
if (mousex<0) {
angle += 180;
}
if (mousex>=0 && mousey<0) {
angle += 360;
}
this.evilcannon._rotation = (angle*-1)+115;
if (this._y > 650) {
this.removeMovieClip();
}
};
}

// begin faster, 'bad' unit
var start_bad_y = 0;
enemiesbad = [];
badBreak = setInterval(badSpawn, 2500);
function badSpawn() {
start_bad_x = Math.round(Math.random()*650);
speedbady = Math.round(Math.random()*10) + 1;
speedbadx = Math.round(Math.random()*5);
badguy = attachMovie("bad", "bad"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:start_bad_x, _y:start_bad_y});
enemiesbad.push(badguy._name);
badguy.onEnterFrame = function() {
for(x in turret1){
if (this.hitTest(_root[turret1[x]])) {
_root[turret1[x]].removeMovieClip();
this.removeMovieClip();
turret1.splice(x, 1);
}
if (this.hitTest(_root.bar)) {
this.removeMovieClip();
}
if (this.hitTest(_root.bar2)) {
this.removeMovieClip();
}
}
this._y += speedbady;
this._x += speedbadx;
if (this._y > 650) {
this.removeMovieClip();
}
};
}

// begin good turret with mouse activated cannon rotation
attachMovie("turret","turret",1,{_x:350, _y:650});
turret.onEnterFrame = function() {
turret1 = [];
turret1.push(turret._name);

//motion
var speed = 3
if(Key.isDown(Key.RIGHT)) {
if(this._x <= 700) {
this._x += speed
}
}

if(Key.isDown(Key.UP)) {
if(this._y >= 0) {
this._y -= speed
}
}


if(Key.isDown(Key.DOWN)) {
if(this._y <= 675) {
this._y += speed
}
}

if(Key.isDown(Key.LEFT)) {
if(this._x >= 0) {
this._x -= speed
}
}

//math for cannon rotation
mousex = _xmouse-this._x;
mousey = (_ymouse-this._y)*-1;
angle = Math.atan(mousey/mousex)/(Math.PI/180);
if (mousex<0) {
angle += 180;
}
if (mousex>=0 && mousey<0) {
angle += 360;
}
this.cannon._rotation = (angle*-1)+90;
};

//cannon fired
function onMouseDown() {
angle = turret.cannon._rotation-90;
start_ball_x = turret._x+60*Math.cos(angle*Math.PI/180);
start_ball_y = turret._y+60*Math.sin(angle*Math.PI/180);
cannonball_fired = attachMovie("cannonball", "cannonball"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:start_ball_x, _y:start_ball_y});
cannonball_fired.dirx = Math.cos(angle*Math.PI/180)*15;
cannonball_fired.diry = Math.sin(angle*Math.PI/180)*15;
cannonball_fired.onEnterFrame = function() {
this._x += this.dirx;
this._y += this.diry;
for(x in enemies){
if (this.hitTest(_root[enemies[x]])) {
_root[enemies[x]].removeMovieClip();
this.removeMovieClip();
enemies.splice(x, 1);
_root.score++;
score_txt.text = score.toString();
}
if (this.hitTest(_root.bar)) {
this.removeMovieClip();
}
if (this.hitTest(_root.bar2)) {
this.removeMovieClip();
}
}
for(x in enemiesbad){
if (this.hitTest(_root[enemiesbad[x]])) {
_root[enemiesbad[x]].removeMovieClip();
this.removeMovieClip();
enemiesbad.splice(x, 1);
_root.score+=3
score_txt.text = score.toString();
}
}
//anti bullets
// bottom
if (this._y > 700) {
this.removeMovieClip();
}
// top
if (this._y < 0) {
this.removeMovieClip();
}
//left
if (this._x < 0) {
this.removeMovieClip();
}
//right
if (this._x > 700) {
this.removeMovieClip();
}
};
}



// turret restart: Needs fixing somehow
restart.onRelease = function (){
_root.score = 0;
score_txt.text = score.toString();
attachMovie("turret","turret",1,{_x:650, _y:650});
turret.onEnterFrame = function() {
turret1 = [];
turret1.push(turret._name);

//motion
var speed = 3
if(Key.isDown(Key.RIGHT)) {
if(this._x <= 700) {
this._x += speed
}
}

if(Key.isDown(Key.UP)) {
if(this._y >= 0) {
this._y -= speed
}
}


if(Key.isDown(Key.DOWN)) {
if(this._y <= 675) {
this._y += speed
}
}

if(Key.isDown(Key.LEFT)) {
if(this._x >= 0) {
this._x -= speed
}
}
mousex = _xmouse-this._x;
mousey = (_ymouse-this._y)*-1;
angle = Math.atan(mousey/mousex)/(Math.PI/180);
if (mousex<0) {
angle += 180;
}
if (mousex>=0 && mousey<0) {
angle += 360;
}
this.cannon._rotation = (angle*-1)+90;
};
}