flash actionscript:
Hey, i'm working on a game right now where I want around 40 or so animated enemies on screen at a time. Is this even possible with flash? Right now i'm starting to get slowdown when I have 15 or so on screen and they're not even animated yet. This is how i'm doing it at the moment. function createEnemy(enemyName, enemyPicture, xvar, yvar, eSpeed, range) { this.createEmptyMovieClip(enemyName, this.getNextHighestDepth()); _root[enemyName].attachMovie(enemyPicture, "enemy"+enemyCounter, this.getNextHighestDepth()); _root[enemyName]._x = xvar; _root[enemyName]._y = yvar; enemyCounter++; _root[enemyName].onEnterFrame = function() { if (_root.player._x<_root[enemyName]._x-range) { _root[enemyName]._x -= eSpeed; } if (_root.player._x>_root[enemyName]._x+range) { _root[enemyName]._x += eSpeed; } if (_root.player._y<_root[enemyName]._y-range) { _root[enemyName]._y -= eSpeed; } if (_root.player._y>_root[enemyName]._y+range) { _root[enemyName]._y += eSpeed; } } }
thanks, i never knew about that property. It sped it up a bit. I think what the reason is though, is that the computers at school, the ones I was testing it on are just really really horrible, and they don't even have flash player 9 which i've heard does a much better job managing resources? Anyways, i ran the same code on my home computer and I could get up to 400 enemies on screen at once with minimal slowdown. But I just wanted to make sure I'm using this right as well as deleting my onEnterFrame properly. I'm still fairly new at actionscript and even though i'm not getting any errors, I dont really know of any way to check if the last 2 lines of code in my ewalk() function are doing anything. Heres my updated code. Also, I found this method of adding a sprite onto the screen that seems to work and does everything I want it to, but i'm not quite sure how it works. Can anyone explain it to me? It was this line... enemy = attachMovie(esprite, "enemyMan", _root.getNextHighestDepth()); Thanks! function createEnemy(cspeed, csprite, crange, clocx, clocy) { espeed = cspeed; esprite = csprite; erange = crange; elocx = clocx; elocy = clocy; enemy = attachMovie(esprite, "enemyMan", _root.getNextHighestDepth()); enemy._x = elocx; enemy._y = elocy; ewalk(); } function ewalk() { enemy.onEnterFrame = function(){ if(_root[pname]._x<this._x-erange) this._x-=espeed; if(_root[pname]._x>this._x+erange) this._x+=espeed; if(_root[pname]._y<this._y-erange) this._y-=espeed; if(_root[pname]._y>this._y+erange) this._y+=espeed; } enemy.cacheAsBitmap = true; delete enemy.onEnterFrame(); }
the first block of code will be a better way to do what you're trying. the 2nd block is much more efficient (only one loop), and is really what you should be using: // first block function createEnemy(cspeed, csprite, crange, clocx, clocy) { espeed = cspeed; esprite = csprite; erange = crange; elocx = clocx; elocy = clocy; enemy = attachMovie(esprite, "enemyMan", _root.getNextHighestDepth()); enemy._x = elocx; enemy._y = elocy; enemy.cacheAsBitmap = true; enemy.onEnterFrame = function() { if (_root[pname]._x<this._x-erange) { this._x -= espeed; } if (_root[pname]._x>this._x+erange) { this._x += espeed; } if (_root[pname]._y<this._y-erange) { this._y -= espeed; } if (_root[pname]._y>this._y+erange) { this._y += espeed; } }; } // 2nd block enemyA = []; function createEnemy(cspeed, csprite, crange, clocx, clocy) { enemy = attachMovie(esprite, "enemyMan", _root.getNextHighestDepth()); enemy.espeed = cspeed; enemy.erange = crange; enemy._x = clocx; enemy._y = clocy; enemy.cacheAsBitmap = true; enemyA.push(enemy); } this.onEnterFrame = function() { for (var i = 0; i<enemyA.length; i++) { if (_root[pname]._x<enemyA[i]._x-enemyA[i].erange) { enemyA[i]._x -= enemyA[i].espeed; } if (_root[pname]._x>enemyA[i]._x+enemyA[i].erange) { enemyA[i]._x += enemyA[i].espeed; } if (_root[pname]._y<enemyA[i]._y-enemyA[i].erange) { enemyA[i]._y -= enemyA[i].espeed; } if (_root[pname]._y>enemyA[i]._y+enemyA[i].erange) { enemyA[i]._y += enemyA[i].espeed; } } };
i'm not sure what you're trying to do with that delete enemy.onEnterFrame, but that surely can't be what you want to do. you should use the 2nd block of code and you should remove enemy movieclip references from enemyA when the movieclip's are no longer needed.
awesome! Thanks so much. I've got one more question though... I want my player to face the mouse, but for my game i've chosen a camera angle similar to starcraft, where it's a birds eye view, but on an angle. I've created a seperate movie clip for each angle that you would see the character on, but what I was wondering is would it be faster to create one movie clip with the different angles inside of it and instead of using attachMovieClip() to change the image when he's turning, use gotoAndPlay() instead? char.onEnterFrame = function(){ mousex = _xmouse-_root[pname]._x; mousey = (_ymouse-_root[pname]._y)*-1; angle = Math.atan(mousey/mousex)/(Math.PI/180); if (mousex<0) angle += 180; if (mousex>=0 && mousey<0) angle += 360; if(angle >= 337.5 || angle <= 22.5){ char._xscale = 100 char.attachMovie("charSide"+parmor, "playerMan", 500); } if(angle >= 22.5 && angle <= 67.5){ char._xscale = 100 char.attachMovie("charDiagBack"+parmor, "playerMan", 500); } if(angle >= 67.5 && angle <= 112.5){ char._xscale = 100 char.attachMovie("charBack"+parmor, "playerMan", 500); } if(angle >= 112.5 && angle <= 157.5){ char._xscale = -100 char.attachMovie("charDiagBack"+parmor, "playerMan", 500); } if(angle >= 157.5 && angle <= 202.5){ char._xscale = -100 char.attachMovie("charSide"+parmor, "playerMan", 500); } if(angle >= 202.5 && angle <= 247.5){ char._xscale = -100 char.attachMovie("charDiagFront"+parmor, "playerMan", 500); } if(angle >= 247.5 && angle <= 292.5){ char._xscale = 100 char.attachMovie("charFront"+parmor, "playerMan", 500); } if(angle >= 292.5 && angle <= 337.5){ char._xscale = 100 char.attachMovie("charDiagFront"+parmor, "playerMan", 500); } }
Don't see what you're looking for? Try a search.
|