A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: Flash Optimization Tips

  1. #1
    Member
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    61

    Flash Optimization Tips

    I am very new at making flash games, infact starting my first one only a month ago. However as I have gotten further and further along, I am having some FPS drops, especially in slower machines. Basically, my 2.4 Ghz Computer with 1 GB Memory and a 64 MB Graphics Accelerator is topping out at 38 FPS. A 450 Mhz Computer with 312 MB Memory is topping out at 4 FPS. I notice people have talked about optimizing code for a faster FPS. How do you do that. Below are the game and .fla

    Game = http://www.sfhq.net/sfhq/flashgame.swf
    Game FPS Test = http://www.sfhq.net/flashtutorials/fpstest3.swf
    Game FLA = http://www.sfhq.net/sfhq/flashgame.fla

    Also could a few people run Game FPS Test and post the framerate it displays along with there machine specs. Note the limit is currently 40 FPS. Thanks.

    Below Is The Code For The Moving Objects
    Last edited by Admiral_Ames; 03-27-2003 at 10:55 AM.

  2. #2
    Member
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    61
    onClipEvent(enterFrame){
    if(_root.primary <100)
    _root.primary += 0.01;
    }
    onClipEvent(enterFrame){
    _root.sshield.moveMovieClip (sshield);
    _root.sshield._x = _root.non._x;
    _root.sshield._y = _root.non._y;
    _root.sshield._rotation = _root.non._rotation;
    }
    onClipEvent(enterFrame) {
    for(i = 0; i <100; ++i) {
    if(this.hitTest(_root["disruptor"+i])) {
    shipVel=0
    removeMovieClip (_root["disruptor"+i]);
    if (_root.shield2>0) {
    _root.sshield.moveMovieClip (sshield);
    _root.sshield._x = this._x;
    _root.sshield._y = this._y;
    _root.sshield._rotation = this._rotation;
    _root.shield2 -= 5;
    }if (_root.shield2<1) {
    _root.life2 -= 10;
    _root.sshield.moveMovieClip (sshield);
    _root.sshield._x = _root.non._x;
    _root.sshield._y = _root.non._y;
    _root.sshield._rotation = _root.non._rotation;
    }
    if (_root.life2 < 1 && _root.sovereign._currentframe == 1) {
    _root.sovereign.gotoAndPlay( 2 );
    _root.impulseh = 0;
    _root.primary = 0;
    _root.secondary = 0;
    }

    }}}
    onClipEvent(enterFrame) {
    for(i = 0; i <80; ++i) {
    if(this.hitTest(_root["rtorpedo"+i])) {
    shipVel=0
    removeMovieClip (_root["rtorpedo"+i]);
    if (_root.shield2>0) {
    _root.sshield.moveMovieClip (sshield);
    _root.sshield._x = this._x;
    _root.sshield._y = this._y;
    _root.sshield._rotation = this._rotation;
    _root.shield2 -= 10;
    }if (_root.shield2<1) {
    _root.life2 -= 20;
    _root.sshield.moveMovieClip (sshield);
    _root.sshield._x = _root.non._x;
    _root.sshield._y = _root.non._y;
    _root.sshield._rotation = _root.non._rotation;
    }
    if (_root.life2 < 1 && _root.sovereign._currentframe == 1) {
    _root.sovereign.gotoAndPlay( 2 );
    _root.impulseh = 0;
    _root.primary = 0;
    _root.secondary = 0;}

    }}}
    onClipEvent (enterFrame) {
    if (this.hitTest(_root.valdore)) {
    shipVel=0
    if (_root.shield2>0) {
    _root.sshield.moveMovieClip (sshield);
    _root.sshield._x = this._x;
    _root.sshield._y = this._y;
    _root.sshield._rotation = this._rotation;
    _root.shield2 -= 1;
    }if (_root.shield2<1) {
    _root.life2 -= 1;
    _root.sshield.moveMovieClip (sshield);
    _root.sshield._x = _root.non._x;
    _root.sshield._y = _root.non._y;
    _root.sshield._rotation = _root.non._rotation;
    }
    if (_root.life2 < 1 && _root.sovereign._currentframe == 1) {
    _root.sovereign.gotoAndPlay( 2 );
    _root.impulseh = 0;
    _root.primary = 0;
    _root.secondary = 0;
    }
    }}
    onClipEvent (load) {
    _root.shipVel = shipVel;
    _root.mph = Number(shipVel)*2;
    _root.mph -= _root.mph%1;
    if (_root.mph<1) {
    _root.mph = 0;

    }
    if (_root.mph>25) {
    _root.mph = 100;

    }}
    onClipEvent (load) {
    this._rotation = 0;
    // velocity of the Phaser when shot
    vel = 5;
    // translates between angle units
    dtr = Math.PI / 180;
    // ship's velocity
    shipVel = 0;
    incVel = .20;
    incVel2 = .45;
    stick = .975;
    stick2 = 0;
    }

    onClipEvent (keyDown) {
    // for rotating the ship
    if (Key.isDown (Key.LEFT)) {
    if (_root.impulseh > 0) {
    rot = "left";
    }} if (Key.isDown (Key.RIGHT)) {
    if (_root.impulseh > 0) {
    rot = "right";
    } }
    // for firing a Phaser
    if (Key.isDown (Key.DELETEKEY)) {
    shootPhaser ();
    }
    // for firing a Torpedo
    if (Key.isDown (Key.END)) {
    shootstorpedo ();
    }
    // for ship movement
    if (Key.isDown (Key.UP)) {
    if (_root.impulseh > 0) {
    shipStatus = "forward";}
    }else if (Key.isDown (Key.PGDN)) {
    if (_root.impulseh > 0) {
    shipStatus = "warp";}
    } else if (Key.isDown (Key.DOWN)) {
    if (_root.impulseh > 0) {
    shipStatus = "backward";}

    } else {
    shipStatus = "nowhere";
    }function shootPhaser () {
    if (_root.primary>0) {
    count++;
    _root.Phaser.duplicateMovieClip ("Phaser"+1);
    _root["Phaser"+1]._rotation = this._rotation;
    _root["Phaser"+1].velx = vel * Math.cos (this._rotation * dtr);
    _root["Phaser"+1].vely = vel * Math.sin (this._rotation * dtr);
    _root["Phaser"+1]._x = this._x;
    _root["Phaser"+1]._y = this._y;
    _root.primary -= 1;
    }}
    function shootstorpedo () {
    if (_root.secondary>0) {
    count++;
    _root.storpedo.duplicateMovieClip ("storpedo"+count, count);
    _root["storpedo"+count]._rotation = this._rotation;
    _root["storpedo"+count].velx = vel * Math.cos (this._rotation * dtr);
    _root["storpedo"+count].vely = vel * Math.sin (this._rotation * dtr);
    _root["storpedo"+count]._x = this._x;
    _root["storpedo"+count]._y = this._y;
    _root.secondary -= 1;
    }}}

    onClipEvent (keyUp) {
    // ship rotation
    if (Key.getCode () == Key.LEFT || Key.getCode () == Key.RIGHT) {
    rot = "neither";
    }
    // ship movement
    if (Key.getCode () == Key.DOWN || Key.getCode () == Key.UP) {
    shipStatus = "nowhere";
    }
    }

    onClipEvent (enterFrame) {
    if (rot == "left") {
    this._rotation -= 1.45;
    } if (rot == "right") {
    this._rotation += 1.45;
    }
    if (shipStatus == "forward") {
    shipVel += incVel;

    } if (shipStatus == "warp") {
    shipVel += incVel2;

    }else if (shipStatus == "backward") {
    shipVel -= incVel;
    }
    vx = shipVel * Math.cos (this._rotation * dtr);
    vy = shipVel * Math.sin (this._rotation * dtr);
    this._x += vx;
    this._y += vy;
    if (this._x > 1000) {
    this._x = 0;
    } else if (this._x < 0) {
    this._x = 1000;
    }
    if (this._y > 680) {
    this._y = 0;
    } else if (this._y < 0) {
    this._y = 680;
    }
    shipVel *= stick;
    }
    Last edited by Admiral_Ames; 03-27-2003 at 10:47 AM.

  3. #3
    Senior Member devnull_2k's Avatar
    Join Date
    Oct 2001
    Location
    Limehouse, Ontario - Never heard of it? Not surprised.
    Posts
    785
    PIII 750
    128 meg ram
    TNT2 32 meg

    Average FPS of about 21 - 22 based on past 50 frames.
    The future belongs to those who prepare for it today.

  4. #4
    Vox adityadennis's Avatar
    Join Date
    Apr 2001
    Posts
    751
    i'd started a thread called "Optimization tips" Search for it

  5. #5
    Member
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    61
    I have read the thread and I am wondering what "Flip Flop" means. I think it refers to having an action called every second frame.

  6. #6
    Flash Developer on a break
    Join Date
    Jun 2001
    Location
    The Netherlands
    Posts
    1,370
    That code..

    Let me count your amount of hitTests().

    181 every enterFrame!!! Try to or get that number down, or use an alternative hittesting method.

    | "Keep Flashing!"-Jeroen
    PHP multiplayer? Check this thread: PHPgateway

  7. #7
    Member
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    61
    Thats why I am wondering if you can have code called every second frame instead of every frame.

  8. #8
    Member
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    61
    Originally posted by jeroen84
    181 every enterFrame!!!
    Don't forget there is another object although its hittest count is only 82

  9. #9
    Member
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    61
    Okay, I have removed the 40FPS "Speed Limit" so it now tops out at 120 although with the current code, nobody will ever get it to go that fast.

    I have also determined the problem is with my hittest code which is currently checking way to many hittests.

    Does anyone know of a better way to run hittest if the most distruptor bolts ever on screen at the same time will be aprox 10 or even if there is a way to hittest with a variable.

  10. #10
    Vox adityadennis's Avatar
    Join Date
    Apr 2001
    Posts
    751
    You can also use an array. Suppose you have baddie1, baddie2, baddie3, baddie4 etc...
    so:

    for (x;x<no_of_baddies;x++){
    if (goodguy.hittest( _root["baddie" + x]){
    other code here}}

  11. #11
    Member
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    61
    Okay, I will try that.

    Also, is there anyway to duplicate a movieclip with the code. What I was going to try is to have the hittest code in the disruptor so it would only have to hittest 1 object. Problem is that as soon as a movie is duplicated, it looses its actionscript.

  12. #12
    Member
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    61
    Is the array not similar to what I have in my existing code

    for(i = 0; i <100; ++i) {
    if(this.hitTest(_root["disruptor"+i])) {

  13. #13
    Vox adityadennis's Avatar
    Join Date
    Apr 2001
    Posts
    751
    It probably loses its actionscript because the duplicate has the same instance name. Sorry I cant open your FLA, I have MX on my other comp. but Flash 5 on this one

  14. #14
    Member
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    61
    Here is a flash5 copy although it slightly predates the above, it is mostly the same.

    http://www.sfhq.net/sfhq/flashgame5.fla

  15. #15
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,928
    A couple of posts late ( Busy day ) but yeah a flip flop is just a way of running code every other frame.

    Just set up a var and if it's true set it false and run function a, if it's false change it to true and run function b.

    Hope that's some help.

    Squize.

  16. #16
    Member
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    61
    OKAY, with much editing I have cut down on the code and sped up the game a lot. But for some reason, the code removeMovieClip (this);
    only works with the duplicated movie clips and not the original. Basically the problem is that as soon as the game stars, the torpedo that is to be duplicated from moves towards the ship it is supposed to track. I don't want it to do this until its been duplicated but its not responding to anything including hittest, removemovieclip, etc. until after it has been duplicated.

    onClipEvent (load) {
    if (this._name == "storpedo") {
    this._visible = false;
    }
    }

    onClipEvent (enterframe) {
    //x movement
    mx=_root.sovereign._x;
    if (mx<_x) {
    dx=_x-mx;
    }
    else {
    dx=mx-_x;
    }
    moveSpeedx=dx/48;
    if (mx<_x) {
    _x=_x-moveSpeedx;
    }
    else {
    _x=_x+moveSpeedx;
    }
    //y movement
    my=_root.sovereign._y;
    if (my<_y) {
    dx=_y-my;
    }
    else {
    dx=my-_y;
    }
    moveSpeedx=dx/48;
    if (my<_y) {
    _y=_y-moveSpeedx;
    }
    else {
    _y=_y+moveSpeedx;
    }
    }

    onClipEvent(enterFrame){
    if (this.hitTest(_root.sovereign)) {
    removeMovieClip (this);
    if(_root.shield2>0){
    _root.shield2 -= 5;
    _root.sovereign.gotoAndPlay(83);
    }if (_root.shield2<1) {
    _root.life2 -= 10;
    }
    if (_root.life2 < 1 && _root.sovereign._currentframe == 1) {
    _root.sovereign.gotoAndPlay( 2 );
    _root.impulseh = 0;
    _root.primary = 0;
    _root.secondary = 0;
    }

    }}
    Last edited by Admiral_Ames; 03-27-2003 at 10:02 PM.

  17. #17
    Vox adityadennis's Avatar
    Join Date
    Apr 2001
    Posts
    751
    Removemovieclip() NEVER works in the original (not for me, at least). The best you can do is move the original movieclip far, far away:
    movieclip._y = 100000

  18. #18
    Member
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    61
    Its okay, I figured out a better solution:

    onClipEvent (enterframe) {
    if (this._name == "storpedo") {
    this._visible = false;
    }
    else {//x movement
    mx=_root.valdore._x;
    if (mx<_x) {
    dx=_x-mx;
    }
    else {
    dx=mx-_x;
    }
    moveSpeedx=dx/48;
    if (mx<_x) {
    _x=_x-moveSpeedx;
    }
    else {
    _x=_x+moveSpeedx;
    }
    //y movement
    my=_root.valdore._y;
    if (my<_y) {
    dx=_y-my;
    }
    else {
    dx=my-_y;
    }
    moveSpeedx=dx/48;
    if (my<_y) {
    _y=_y-moveSpeedx;
    }
    else {
    _y=_y+moveSpeedx;
    }

    }}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center