dcsimg
A Flash Developer Resource Site

Page 14 of 21 FirstFirst ... 4101112131415161718 ... LastLast
Results 261 to 280 of 411

Thread: Optimization Tips

  1. #261
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,367
    Huh? I don't get that...Wouldn't all of those have to be true to have the movieclip removed?

  2. #262
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    *slaps himself*

    Yes of course.. its 7am in the morning here.. I wont visit flashkit this early again =D

    If the if were all && statements.. then the nesting if's would be faster, but because they are ||'s you win =D.

    In your case I would say that the code you have (the 4 ifs in a row) is faster, but as always, do your own comparison. Check the speeds that they excecute, and bring back the results here.
    Christopher Rhodes
    squarecircleco.

  3. #263
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,182

    :)

    Expanding code is faster most of the times...

    With expanding I meen using:
    if(){}
    if(){}

    Instead of:
    if( || ){}

    Or convertinG for to while-loops or even unroLl them:
    i=0;
    while(i<5){
    ++i;
    }

    Instead of:
    for(i=0; i<5; i++){
    }

    Or even faster:
    i = 0;
    i = 1;
    i = 3;
    i = 4;

  4. #264
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,367
    Let me make sure of something here...so its almost always faster to have tons of different ifs then using the &&?
    I'm gonna switch my game I'm working on over to this is your pretty sure its gonna be faster...

    Thanks

  5. #265
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    In Flash yes, its faster to have all your if's expanded. In a compiled language thats usually not the case
    Christopher Rhodes
    squarecircleco.

  6. #266
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,367
    One other question...I don't mean to be a bother though
    In my game(which will hopefully come out soon...) I have a character and it checks if space,left,right,down,up,ctrl,shift are down(any of them) What I mean is that there is a different if statement for each.
    The first is an if statement and the rest else ifs...But the space action and ctrl action take time one they're activated(2-3 seconds) and a variable(a different one for each) is set so nothing else can be pressed...Each if statements checks to see if the key is down and if the other variables==0. So the other else if action don't work unless the statements are true, almost all of them(A few are nested)..
    Is this the most optimized way to do this, and if you can't understand, just ask...

    Thanks again....

  7. #267
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,928
    I think I get what you're saying, but rather than me write a long answer and have totally got the wrong end of the stick, could you just post a snippet of the code ?

    Squize.

  8. #268
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,367
    Heres the code...

    code:
    onClipEvent(enterFrame){
    if(!Key.isDown(Key.SHIFT) && !Key.isDown(Key.RIGHT)|!Key.isDown(Key.LEFT)){
    _root.shooting=0}



    if(Key.isDown(Key.SPACE) && _global.amountofwater>=10*_root.game.bucket && !_global.refilling && (_root.game.hurting==0) && (_root.shooting==0)){
    gotoAndStop(6)}
    else if(Key.isDown(Key.CONTROL)&& (!_root.shooting==1)){
    if(_x>10 && _x<75 && _y>-23 && _y<10){
    _root.waterhose.gotoAndStop(2);
    gotoAndStop(7)
    _x=50;
    _y=-20
    }}else if(Key.isDown(Key.SHIFT) && Key.isDown(Key.RIGHT)|(Key.isDown(Key.SHIFT) && Key.isDown(Key.LEFT))){
    if(!watering && !_global.refilling && (_root.game.hurting==0)){
    if(Key.isDown(Key.RIGHT) && (Key.isDown(Key.SHIFT))){
    gotoAndStop(10)
    _root.shooting=1}
    else if(Key.isDown(Key.LEFT) && (Key.isDown(Key.SHIFT))){
    gotoAndStop(9)
    _root.shooting=1
    }}}

    else if (Key.isDown(Key.DOWN) ){
    if(!watering && !_global.refilling && (_root.game.hurting==0)){

    gotoAndStop(2)}
    }
    else if (Key.isDown(Key.UP)){
    if(!watering && !_global.refilling && (_root.game.hurting==0)){
    gotoAndStop(4)
    }}
    else if (Key.isDown(Key.RIGHT)){
    if(!watering && !_global.refilling && (_root.game.hurting==0)){
    gotoAndStop(3)

    }}
    else if(Key.isDown(Key.LEFT)){
    if(!watering && _x>4 && !_global.refilling && (_root.game.hurting==0)){
    gotoAndStop(5)}}
    else if (!Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.UP) && !Key.isDown(Key.DOWN ))
    {if(!_global.watering && (_root.game.hurting==0) && (!_global.refilling)){
    gotoAndStop(1)}}}


    Thanks...Hope this helps...I'm knew at actionscript so any other optimization will be appreciated...

  9. #269
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,928
    omg !

    That's more code than I have in all my games put together

    Firstly, I'm sure you've read about multiple conditionals on a line being a killer, so split those little beauties up.

    I'm a fan of using function pointers in code. Yeah you get an overhead of actually calling a function, but you only run the code you actually need to, plus I find it makes my code a lot neater having lots of seperate function calls.

    So in your onLoad have something like this:
    code:

    onClipEvent(load){
    var myfunc1=keyboardCheck1;
    var myfunc2=keyboardCheck2;
    }


    Then define those functions in there as well, so something like
    code:

    function keyBoardCheck1(){
    //Do your check for your action buttons ( Space & CTRL )
    }
    function keyBoardCheck2(){
    //Check all them other keys
    }


    Then in your enterFrame you just need:
    code:

    myfunc1();
    myfunc2();



    Phew, nearly there...

    In your explanation post ( Sorry I can't face looking at your code mate ) you said you wanted to ignore key presses if space or ctrl had been pressed ?
    So make a new function in the onLoad, eg
    code:

    function amountOfTimeToIgnoreKeys(){
    //I think you can guess what this function is meant to do;
    }


    So in your keyBoardCheck1 function, you check for either of the action keys being pressed. If they are you simply:
    code:

    myfunc2=amountOfTimeToIgnoreKeys;


    So that means your enterFrame still looks the same, but it now calls the amountOfTimeToIgnoreKeys function instead of checking for all those other keys. It's avoiding all those un-needed key checks.
    When the amountOfTimeToIgnoreKeys function is complete ( ie the delay is over ) you just return things back to normal with a,
    code:

    myfunc2=keyboardCheck2;


    I hope that makes sense, it's a fair bit to take in, and it's pretty much a loose example, you could refine it a lot more.

    Squize.

  10. #270
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,367
    I'm not exactly sure you understand me...for the time thing its not really measured time, its a movie clip that played and at the end a variable is changed, to allow other actions to happen...

    From what I undersant, I should make all of my actions functions as much as possible?I think I may be a little confused...

    I need all the &&'s because i'm using else ifs so multiple things have to be true to cancel out the other else ifs...Would it be faster to use just plain old ifs and have all of them nested?

    The way I think I should run it(correct me please from what people tell me and what my game needs) is use functions in the if/else ifs statements but keep the checks pretty much the same unless I take away all the elses which might hurt my performance...

    Is it better to have nested if statements or non-nested else statements in this case...

    LOL...sorry I'm not sure what to do...and thanks for everyone's help on this...

    P.S. This is only some of the code, a very small amount...LOL

  11. #271
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,928
    The time function can be altered to anything, so it could just be checking if the mc you've triggered is on the correct frame.

    You've really got to un-nest those if statements, debugging must be a nightmare for you mate.

    You don't need all of them, just structure it differently. Just using the first line as an example:
    code:

    if(!Key.isDown(Key.SHIFT) && !Key.isDown(Key.RIGHT)|!Key.isDown(Key.LEFT)){
    _root.shooting=0
    }


    It's a lot quicker to just clear the var in the first place without the check ( You go on to check the keys anyway, you're just checking things too many times ).

    This line is a killer cpu wise ( Sorry, I'm not tearing your code apart on purpose )
    code:

    if(Key.isDown(Key.SPACE) && _global.amountofwater>=10*_root.game.bucket && !_global.refilling && (_root.game.hurting==0) && (_root.shooting==0)){


    The way Flash works when looking at an if conditional, is to get ALL data in it before it does anything with it. So that line is telling the Flash player to get the following data:

    Key.isDown(Key.SPACE) //ie scan the keyboard
    _global.amountofwater
    _root.game.bucket //and then multiply that by 10
    _global.refilling
    _root.game.hurting
    _root.shooting

    All that takes time, and it retrieves all that data before running a single check. So if just one of those fails it's check, the rest of it is a waste.
    I would structure it so you check for space, and then put all those checks ( Un-nested ) in a function. It will make your code a lot readable, and faster.

    You don't have to make everything a function, but it makes life sooo much easier to use them sensibly. It reduces your debugging time a lot, and let's face it, anything that reduces that has to be a good thing.

    For example, where you've got
    code:

    else if (Key.isDown(Key.DOWN) ){
    if(!watering && !_global.refilling && (_root.game.hurting==0)){
    gotoAndStop(2)}
    }

    You could write than as
    code:

    runAnimation=function(frame){
    if(!watering && !_global.refilling && (_root.game.hurting==0)){
    gotoAndStop(frame);
    }

    And then just call that same function for all your arrow keypress, like
    code:

    else if (Key.isDown(Key.DOWN) ){
    runAnimation(2);
    }

    It means you have less code to sort through, and if you want to change what it checks for, the alteration applies to all the arrow keys in one go.
    BTW, un-nest those if statments as well

    Squize.

  12. #272
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,367
    Thanks for your continued help squize...
    About the first time thing, that is going to be changed, its a item change not a movie clip change, so while the correct keys are down that's the movieclip used. I'm soon just gonna allow an item change with shift and again to change back etc..
    My main problem is if I use the else if statements and nest them it won't work...because if an action couldn't be completed but the key is down any other key press will fail(I'm pretty sure). So my last question(I hope) is, is it better to use all if statements and have them all nested or to use else ifs and have most of them not nested? Maybe your way would work but I'm not quite sure if it will do what I want to accomplish...

    Thanks again...

  13. #273
    Vox adityadennis's Avatar
    Join Date
    Apr 2001
    Posts
    751
    Just an example of using vector vs bitmap gfx:

    vector:
    www.geocities.com/adityadennis/Sackman6s.html

    bitmap:
    www.geocities.com/adityadennis/Sackman6f.html

  14. #274
    =========== io3's Avatar
    Join Date
    Dec 2003
    Location
    Reality: what a concept !
    Posts
    109
    Originally posted by adityadennis
    Just an example of using vector vs bitmap gfx:

    vector:
    www.geocities.com/adityadennis/Sackman6s.html

    bitmap:
    www.geocities.com/adityadennis/Sackman6f.html
    Yep, makes a big difference.

    Anyways, excellent thread! Wish I had found it earlier

    Istvan

    p.s. This has to be the 'sexiest' thread on Flahskit
    Looking for something different? Check out Tuti TV at http://www.tuti.tv/

  15. #275
    Vox adityadennis's Avatar
    Join Date
    Apr 2001
    Posts
    751
    Of course, I started it

  16. #276
    Senior Member random10122's Avatar
    Join Date
    Mar 2002
    Location
    Sheffield, UK
    Posts
    1,747
    By the way, if you search google for 'Flash Code Optimization' or something along those lines my page that is derived from this thread (see footer) comes up on the first page of results in yahoo and google

    yay.

    fracture2 - the sequel
    fracture - retro shooter
    blog - games, design and the rest

    "2D is a format, not a limitation" -Luis Barriga

  17. #277
    Senior Member Kirill M.'s Avatar
    Join Date
    May 2002
    Location
    Toronto, Canada
    Posts
    712
    Hmm, why is it faster to use nested if statements rather than use && and ||?

    Example: true && true, both of instances of true get executed no matter which method you use.

    false && true, both methods don't evaluate the second true.

    true || false, both methods don't evaluate the second false.

    So to me it seems they perform the same.


    Also this is something I tested in Java, but if you need to do a lot of divisions or multiplications by numbers that are powers of 2, it's way faster if you use the bitwise shift operators (<< and >>) instead of actual arithmetic operators (* and /). There will be extra overhead in Flash to convert the doubles to integers, but it will most liekly be faster still.

  18. #278
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    Read Squize's last post, he explains it at great depth
    Christopher Rhodes
    squarecircleco.

  19. #279
    Junior Member
    Join Date
    Jan 2003
    Location
    Vancouver, BC, Canada
    Posts
    17
    Originally posted by random10122
    By the way, if you search google for 'Flash Code Optimization' or something along those lines my page that is derived from this thread (see footer) comes up on the first page of results in yahoo and google
    yay.
    Excellent! A link to your page has been added to my 'Favories' when I first saw it. Will be a great quick reference in the future.

    Istvan
    Looking for something different?
    Visit Tuti TV at www.tuti.tv
    (Some Content Rated: R)

  20. #280
    Senior Member Kirill M.'s Avatar
    Join Date
    May 2002
    Location
    Toronto, Canada
    Posts
    712
    Most you probably know about this but in taking apart the if's, rather than actually nest them using tabs and extra braces you can just stack them on top of one another.

    i.e.
    code:

    if(true){
    if(true){
    trace("")
    }
    }


    you can do
    code:

    if(true)
    if(true){
    trace("")
    }


    Which is cleaner and more readable if you have a whole bunch of those if's nested. In order for this to work, though, each outer if statement must not have anything else in it escept another if statement.

    Edit: It's a shortcut to the braces syntax I like to use.
    Last edited by Kirill M.; 12-14-2003 at 12:15 AM.

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