welcome to my space

03/15/2010 (10:54 am)

Need help optimizing my code.

Filed under: nappedeptrole.com edit
  • What I wanted to do was draw multiple lines comming from the same point to a random point that moves around randomly. I then wanted to apply a blur filter. The effect produces a ray of light that resembles what you would see if you where underwater. It's pretty cool, but after a while I found that the lag got worse (I think it was because of the huge "for" loop) anyway... I decided to convert all the lines to a bitmapdata and THEN apply a filter to it... This did take care of the growth in lag... But instead... It is now always in a constant lag that doesn't change much.

    Can anyone give me any tips on how I could optimize my code further or have I reached flash 8's max potential in terms of performance?

    K thx, here's the code:

    import flash.filters.BlurFilter;
    import flash.display.BitmapData;
    import flash.geom.Rectangle;
    import flash.geom.Point;
    center = new Object();
    center.x = Stage.width/2;
    center.y = Stage.height/2;
    firstRun=true
    num=100
    damp = .95;
    this.createEmptyMovieClip("star", this.getNextHighestDepth());
    var prev=new Array()
    var place=new Array()
    blur=new BlurFilter(20,20,1)
    this.createEmptyMovieClip("bmpHolder", this.getNextHighestDepth());
    this.onEnterFrame = function () {
    star.clear()
    for (var i=0; i if(firstRun){
    star.lineStyle(1, 0xFFFFFF, 100);
    star.moveTo(0, 100);
    prev[i]=new Object()
    prev[i].cx=0
    prev[i].cy=0
    prev[i].x=0
    prev[i].y=0
    place[i]=new Object()
    place[i].cx=Math.random()*Stage.width/4+100
    place[i].cy=Math.random()*Stage.height/4+100
    place[i].x=Math.random()*Stage.width/4+100
    place[i].y=Math.random()*Stage.height/4+100
    }
    prev[i].cx+=Math.random()*2-1
    prev[i].cy+=Math.random()*2-1
    prev[i].x+=Math.random()*2-1
    prev[i].y+=Math.random()*2-1
    prev[i].cx*=damp
    prev[i].cy*=damp
    prev[i].x*=damp
    prev[i].y*=damp
    place[i].cx+=prev[i].cx
    place[i].cy+=prev[i].cy
    place[i].x+=prev[i].x
    place[i].y+=prev[i].y
    star.lineStyle(1, 0xFFFFFF, 100);
    star.moveTo(0, 100);
    star.lineTo(place[i].x,place[i].y)
    //star.curveTo(place[i].cx, place[i].cy, place[i].x, place[i].y);
    }
    var bmp = new BitmapData(Stage.width,Stage.height,true,0x00FFFFF F)
    bmp.draw(star)
    star.clear()
    //bmp.applyFilter(bmp,new Rectangle(0,0,Stage.width,Stage.height),new Point(0,0),blur)
    bmp.applyFilter(bmp,new Rectangle(0,0,Stage.width,Stage.height),new Point(0,0),blur)
    bmpHolder.attachBitmap(bmp,1)
    if(firstRun){
    firstRun=false
    }
    };
    turtle_mc.swapDepths(this.getNextHighestDepth());


    P.S. You will need a dark background to see the light rays.


  • Thats awesome :P Did you care to put your fps up alot? it lagged for me untill I put it to 120.

    Not really, I set it to 30, but teh lag is not really obvious... The problem is when, for example you have a draggable movieclip on the stage; when you move it around it had trouble keeping up to the mouse because of the lag.


  • Thats awesome :P Did you care to put your fps up alot? it lagged for me untill I put it to 120.







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about Need help optimizing my code. , Please add it free.