A Flash Developer Resource Site

Page 13 of 21 FirstFirst ... 391011121314151617 ... LastLast
Results 241 to 260 of 411

Thread: Optimization Tips

  1. #241
    flash junk extravagante
    Join Date
    Oct 2003
    Location
    netherlands
    Posts
    105
    Man, you really are one bunch of codef*ckers! But honestly: word up for this topic. I've actually been reading all of the posts up to now and I've really learned a lot. I'm already planning on learning some stuff about ASBroadcaster and FLASM (way to go Squize!) after I finish my current game-engine (two days left and counting....)

    I really better get some sleep now

    BTW: I'm quite new to the board too (been lurking for over a year but finally decided to actually post something), so err... Hi (too bad I was too late to contribute to the 'sexy'-contest)
    signatures after the show, thank you.

  2. #242
    Here's my tip:
    Imagine you have a bullet that is pointing right, but you want to have it point in the direction it is going. If xspeed is positive then the bullet is going right, so its rotation should be 0, else xspeed is negative, so the bullet is going left, and its rotation should be 180.
    You could use the following if statements:
    Code:
    if(this.xspeed>0){
        this._rotation=0;
    if(this.xspeed<0){
        this._rotation=180;
    }
    but you could make an equation that gives you the correct rotation based on xspeed directly. The following:
    Code:
    xs=this.xspeed
    this._rotation = (((xs/Math.abs(xs))*-180)+180)/2
    ...would do the same, though it shortens your code, and is, i imagine, faster.

  3. #243
    Untitled-1.fla strille's Avatar
    Join Date
    Mar 2001
    Location
    Sweden
    Posts
    1,626
    taxman, I think just a normal if-else would be the best solution to achieve what you described, much like your first code:
    code:

    if (this.xspeed > 0){
    this._rotation = 0;
    } else {
    this._rotation = 180;
    }


    The second code snippet would alomst certainly be slower. I haven't actually compared them, but I cannot imagine it running faster, because you have a Math.abs() call, 2 divisions etc.

    And I think a normal if-else is much more readable, you can instantly see what is happening. Creating very readable code is an important rule in eXtreme Programming (but that's another topic ).

  4. #244
    I was thinking about that while i was at the dentist earlier... It's true that the best choice would probably be to use:
    this._rotation = xspeed<0 ? 180 : 0

    I tried to find a way to avoid using Math.abs, but i could only get the same result by using Math.sqrt(xs*xs) :P
    About addition/multiplication though, how much performance impact can they have?
    In examples with more variables though it really reduces your code. Imagine you had you wanted to check xspeed, yspeed, xcoord, ycoord values you'd need alot more if statements versus a single line.

  5. #245
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    Although it would reduce your code it would still run slower ( With more variables ), because the flash player would still have to get all those values before performing a single operation on them.

    If you seperated up your conditionals ( No "and" or "or" etc. ) and put them in the most likely to run through order then it would be quicker.

    Maths functions aren't incredibly bad in the grand scheme of things. You can use little speed-ups such as

    var Mabs=Math.abs

    or the old classic

    a+=2;
    instead of
    a*=2;

    etc.

    Squize.

  6. #246
    Code:
    function move(move, xmxm, ymxm, rest, updtx, updty, axis, delay, postprev, oupdt, chFace, chObj, l, r, u, d)
    {
        ();
        ();
        ();
        _root;
        delay;
        move;
        if (chFace != _root.currentFace)
        {
            _root.currentFace = chFace;
            _level1[chObj + "MC"].gotoAndPlay(chFace);
        } // end if
        iniT = {r:_level1[chObj].r, s:_level1[chObj].s, t:_level1[chObj].t};
        temp = new Object();
        temp.r = (iniT.r + Math.floor((iniT.t + 1) / 21)) * d + (iniT.r - Math.floor(Math.abs((iniT.t - 26) / 22))) * u + iniT.r * r + iniT.r * l;
        temp.s = iniT.s * d + iniT.s * u + ((iniT.t + 1) % 5 == 0 ? iniT.s + 1 : iniT.s) * r + ((iniT.t + 5) % 5 == 0 ? iniT.s - 1 : iniT.s) * l;
        temp.t = (iniT.t - 20 * Math.floor((iniT.t + 1) / 21) + (5 - 5 * Math.floor((iniT.t + 1) / 21))) * d + (iniT.t + 20 * Math.floor(Math.abs((iniT.t - 26) / 22)) - (5 - 5 * Math.floor(Math.abs((iniT.t - 26) / 22)))) * u + ((iniT.t + 1) % 5 == 0 ? iniT.t - 4 : iniT.t + 1) * r + ((iniT.t + 5) % 5 == 0 ? iniT.t + 4 : iniT.t - 1) * l;
        chF = _root.calcField(temp);
        eval(_level50.walkNova[chF.d.r][chF.d.s][chF.d.t] == "w" && _level50.walkNova[chF.lr.r][chF.lr.s][chF.lr.t] == "w") = _level50.walkNova[chF.ll.r][chF.ll.s][chF.ll.t] == "w";
        eval("up" && _level50.walkNova[chF.u.r][chF.u.s][chF.u.t] == "w" && _level50.walkNova[chF.ur.r][chF.ur.s][chF.ur.t] == "w") = _level50.walkNova[chF.ul.r][chF.ul.s][chF.ul.t] == "w";
        eval("right" && _level50.walkNova[chF.r.r][chF.r.s][chF.r.t] == "w" && _level50.walkNova[chF.ur.r][chF.ur.s][chF.ur.t] == "w") = _level50.walkNova[chF.lr.r][chF.lr.s][chF.lr.t] == "w";
        eval("left" && _level50.walkNova[chF.l.r][chF.l.s][chF.l.t] == "w" && _level50.walkNova[chF.ul.r][chF.ul.s][chF.ul.t] == "w") = _level50.walkNova[chF.ll.r][chF.ll.s][chF.ll.t] == "w";
        if (((("down" && down || d == 1) && up || u == 1) && left || l == 1) && right && r == 1)
        {
            if ((((_level1[chObj + "MC"]._x >= 128 && axis == "x" || move < 0) && _level1[chObj + "MC"]._x >= _root.xSize - 128 && axis == "x" || move < 0) && _level1[chObj + "MC"]._y >= 128 && axis == "y" || move < 0) && _level1[chObj + "MC"]._y >= _root.ySize - 128 && axis == "y" && move < 0)
            {
                if ((((axis == "x" && _root.xorigin >= _root.xmax - (_root.xres - 1) && move < 0 || _root.xspec < (_root.xmax - (_root.xinit + (_root.xres - 1))) * 5 + 10) && axis == "x" && move < 0 || _root.xspec < 2) && axis == "y" && _root.yorigin >= _root.ymax - (_root.yres - 1) && move < 0 || _root.yspec < (_root.ymax - (_root.yinit + (_root.yres - 1))) * 5 + 10) && axis == "y" && move < 0 && _root.yspec < 2)
                {
                    _level1[chObj].r = temp.r;
                    _level1[chObj].s = temp.s;
                    _level1[chObj].t = temp.t;
                    _level1[axis + "spec"] = _level1[axis + "spec"] + move / Math.abs(move) * -1;
                    _level1.moveWorld(axis, move);
                    signal = false;
                    xtemp = _root.xorigin;
                    ytemp = _root.yorigin;
                    for (i = xtemp; i < xtemp + _root.xres; i++)
                    {
                        for (j = ytemp - 1; j < ytemp + (_root.yres - 1); j++)
                        {
                            _root["s" + (j * 1000 + i)]._y = _root["s" + (j * 1000 + i)]._y + move;
                            (_root["s" + (j * 1000 + i)]._y + move);
                            _root["s" + (j * 1000 + i)]._x = _root["s" + (j * 1000 + i)]._x + move;
                            updty == "" ? _root["s" + (j * 1000 + i)]._x + move : _root["s" + (j * 1000 + i)]._y + move;
                            tempE = axis == "x" ? i + j * 1000 + delay : j * 1000 + i + delay;
                            if (_root["s" + (j * 1000 + i)]._x == xmxm || _root["s" + (j * 1000 + i)]._y == ymxm)
                            {
                                _root["s" + (j * 1000 + i)]._visible = false;
                                if (typeof(_root["s" + tempE]) != "movieclip")
                                {
                                    (_root.olevel);
                                    _root.olevel = _root.olevel++;
                                    _root.createEmptyMovieClip("s" + tempE, _root.olevel);
                                    _root["s" + tempE]._x = -200;
                                    _root["s" + tempE]._y = -200;
                                    _root.level = 0;
                                    _root["intID" + tempE + 100000] = setInterval(_root.pave, 1, delay, i, j, tempE, chFace);
                                } // end if
                                _root["s" + (j * 1000 + (i + delay))]._x = updtx;
                                (updtx);
                                _root["s" + (j * 1000 + (i + delay))]._x = _root["s" + (j * 1000 + (i + delay + postprev))]._x;
                                updtx == "" ? _root["s" + (j * 1000 + (i + delay + postprev))]._x : updtx;
                                _root["s" + (j * 1000 + (i + delay))]._y = updty;
                                (updty);
                                _root["s" + (j * 1000 + (i + delay))]._y = _root["s" + (j * 1000 + (i + delay + postprev))]._y;
                                updty == "" ? _root["s" + (j * 1000 + (i + delay + postprev))]._y : updty;
                                _root["s" + (j * 1000 + (i + delay))]._visible = true;
                                signal = true;
                            } // end if
                        } // end of for
                    } // end of for
                    if (signal)
                    {
                        signal = false;
                        _root[axis + "origin"] = _root[axis + "origin"] + oupdt;
                    } // end if
                } // end if
            }
            else
            {
                _level1[chObj].r = temp.r;
                _level1[chObj].s = temp.s;
                _level1[chObj].t = temp.t;
                _level1[chObj + "MC"]._y = _level1[chObj + "MC"]._y - move;
                (_level1[chObj + "MC"]._y - move);
                _level1[chObj + "MC"]._x = _level1[chObj + "MC"]._x - move;
                axis == "x" ? _level1[chObj + "MC"]._x - move : _level1[chObj + "MC"]._y - move;
            } // end if
        } // end if
        ();
        ();
        ();
    }
    I thought shorter was faster. At least i don't have to bother about people stealing my code. :P

  7. #247
    Junior Member
    Join Date
    May 2002
    Posts
    0
    what's all this code for?
    code:

    ();
    ();
    ();
    _root;
    delay;
    move;
    //...
    (updtx);
    //...
    ();
    ();
    ();
    //...ect




    ~yarr

  8. #248
    That's the decompiler's version of the code.

  9. #249
    Junior Member
    Join Date
    May 2002
    Posts
    0
    ah, stupid compiler, the programmer should be ashamed of himself

    ~yarr

  10. #250
    Indeed...
    But we're going off topic, this thread is meant to discuss the word "sexy" and such.

  11. #251
    Junior Member
    Join Date
    Jul 2003
    Posts
    14

    memory optimizing ? how ?

    've read some of the post.
    i'm makin some clone of FF 5 for an RPG battle, though not completed yet.
    i use some loop & goto and play ( as usual).
    the battle run normally at first but become SLOWWWWWWWWW after bout 3 minutes. using the task manager, i found that the memory usage is climbing as time goes by ^^.
    even after the battle finished, though the memory usage not climb anymore, it steady as same as when the battle finished.
    if I entered the battle the memory started to climb again .

    is there any way to utilize the memory ???

    though it not finished, the source is as big as 5MB and the swf is 2.5MB.

    please help me ( tasukette kudasai(jpn))

  12. #252
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    2.5 meg swf ? Mate that's bigger than all my games added together

    Although not really an optimising question, check this link out: http://www.flashkit.com/board/showth...hreadid=504216

    Squize.

  13. #253
    Ihoss
    Guest
    im at school so i dont have time to read through the whole thing, but is it true that case is faster than if? and is the

    _root.mc.mc.gotoAndPlay(3);

    faster than

    tellTarget(_root.mc.mc){
    gotoAndPlay(3);
    }

  14. #254
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Originally posted by Squize
    or the old classic

    a+=2;
    instead of
    a*=2;
    Lol, is it just me, or do you think squize had something else ( most probably dirty ) on his mind when typing that one out?

    i guess you mean a ^= 2; But i'm not that hot with bitwise stuff.

    But great link, jobemjobem explains it really well, but can be summarised thus: always use var!

    Ihoss:
    The old flash 4 syntax ( telltargets etc ) are apparently faster than any of the "new" dot-syntax code. If-versus-switch - i think they came out equal. Not 100% though.
    jonmack
    flash racer blog - advanced arcade racer development blog

  15. #255
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    I meant a+=a;

    When I post something stupid, I like to make it really stupid

    I'm just glad no one brought any attention to it

    Switch & If worked out the same ( Strille did a little test a couple of pages back ), and nasty ol' 4 syntax is still quicker under mx ( Even under the 7 player I believe ).

    Squize.

  16. #256
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    I did a little test the other day, forgot to post it.

    If your into making tilebased games (like me =)

    Then you know you floor alot of values.

    We already know that to save from doing Math.floor all the time at the start to do something like

    floor = Math.floor, and then you dont have to do the Math lookup. But funilly enough, the Flash 4 version of the command int(); is faster, by almost double, even when compared against the direct referenced function..

    So use int(); eg: map[int(x/tw)][int(y/th)];
    Christopher Rhodes
    squarecircleco.

  17. #257
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    ...and that's one of the many reasons I love you.

    Thanks for sharing that little gem babe.

    Squize.

  18. #258
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Originally posted by jonmack
    i guess you mean a ^= 2; But i'm not that hot with bitwise stuff.
    And it shows ignore that. I think i was thinking of ( a <<= 1; )

    "I meant a+=a;"

    Ooooooh...! *looks stupid*. I see it now, nice trick. I thought you meant that for any multiple, not 2 only. I must have been thinking about you thinking about dirty stuff..

    I did try it however, and it was slower that a*=2... so what's going on there? I tried <<=1 too, but it doesn't work for floating points. And it quickly goes off the integer scale.

    "So use int(); eg: map[int(x/tw)][int(y/th)];"

    Like it a lot!
    jonmack
    flash racer blog - advanced arcade racer development blog

  19. #259
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,361
    I haven't had time yet to read over this whole really good thread... but I was wondering if it is quicker to check for four different things, that have the same result using one if statement and using OR, or is it better to use four different ifs statement?

    code:
     if(_root.bullet._x<-15|_root.bullet._x>600|_root.bullet._y<-15|_root.bullet._y>415){removeMovieClip(bullet) }
    OR
    if(_root.bullet._x<-15){removeMovieClip(bullet)}
    if(_root.bullet._x>600){removeMovieClip(bullet)}
    if(_root.bullet._y<-15){removeMovieClip(bullet)}
    if(_root.bullet._y>415){removeMovieClip(bullet)}


    Thanks

  20. #260
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    If you nest them, they are faster

    if(_root.bullet._x<-15){
    if(_root.bullet._x>600){
    if(_root.bullet._y<-15){
    if(_root.bullet._y>415){
    removeMovieClip(bullet);
    }
    }
    }
    }

    But its much more annoying to code with the If statements expanded.
    Christopher Rhodes
    squarecircleco.

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