A Flash Developer Resource Site

Page 5 of 21 FirstFirst 12345678915 ... LastLast
Results 81 to 100 of 411

Thread: Optimization Tips

  1. #81
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    RipX:
    I don't wanna spam this thread up 'cause it's one of the few really good ones at the moment IMHO, but it's a new game ( ColourWorld is long dead ).
    It scrolls and it blasts and I'll post an alpha as soon as possible. The very few people who have seen it so far like it, and they are all people whose views I really value, so I'm quite pleased at the moment.

    It's my attempt at a AAA game and is far and away the biggest thing I've attempted in Flash.

    --- End spam ( And hype ) ---

    jtnw:
    Prototypes don't have to be for code that is used a lot, you could quite easily write any game you could think of in Flash and never use a prototype. It's just a structured way to increase an objects functions.

    Functions don't have to be used locally, you just use them for code that may be repeated in a game. The concept of functions is that you write them generically so they can take arguments and can be re-used.

    For example, take a function that does all the collision detection for all the baddies in your game. It can take any baddies x/y position and perform a test and give you back the result. Then later on during development you have this killer idea but it means you need to pass the animation frame to the collision function. 'Cause it's in a function you only have to alter that and all the baddies will use the same code, rather than updating each instance of the collision code by hand.

    Performance wise calling a function is actually slower, but there is a cut off point where optimising will just make your code un-readable ( I can't stand seeing code with one letter var names, it's just so easy to screw things up and when you look at that code in a months time it's going to take you twice as long to understand it ).
    Also another beauty of using functions is that once it works you can re-use it in every other project you do.

    Squize.

  2. #82
    w00t io::pred's Avatar
    Join Date
    Mar 2003
    Location
    Sydney, Australia
    Posts
    163
    The easiest way to describe prototypes, in the movieclip sense, is that all of the current functions in a movieclip (stop() lineTo() etc.) are all prototypes, each movieclip has access to these functions.. lets say we define a function


    movieclip.prototype.moveto = function(x,y){
    this._x = x;
    this._y = y;
    }

    then EVERY single movieclip in that timeline can call:

    moveto(150,145);

  3. #83
    Junior Member
    Join Date
    May 2002
    Posts
    0
    thanks pred & squize, i get it now , especially now that i know that lineto, stop, play are prototypes

  4. #84
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    Lukstr suggested on MSN (wristslash@msn.com) to make this thread a sticky, so there you go.

    You can get me on IQC: 73073602 as well say another sticky-worthy thread pops up.

  5. #85
    Senior Member random10122's Avatar
    Join Date
    Mar 2002
    Location
    Sheffield, UK
    Posts
    1,747
    Hey, it was my idea hehe... oh well at least its sticky now!

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

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

  6. #86
    Vox adityadennis's Avatar
    Join Date
    Apr 2001
    Posts
    751
    Woohoo!
    Here's another tip most of you may probably already know:
    when making a tilebased game, the same actions with very few differences apply whenever the character moves. So instead of having an on(enterframe) event or putting the same code for every keypress, just make a function with the code and call it on a keypress. Cuts down on the code and makes the game faster, too.

  7. #87
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    I wish I know as much as all of you do. My game could without doubt be xx% faster. Most of the code I'm using are so clumsy.

    In one of my badguy MCs I have on the first keyframe this code which loops till it hits the "guy" (the hero), when guys is hitted it goes to the labeled frame "random"

    keyframe 1 that loops:
    Code:
    if (_root.guy.hitTest(this)) {
    	gotoAndPlay ("random");
    code on the random labeled frame:

    Code:
    f = [78, 112, 134, 172];
    	r = random(4);
            frame=f[r];
    	      gotoAndPlay(frame);
    78=highkick, 112=highpunch....... (yeah I know, why dont I use labels instead of numbers??... donno )


    I have a little problem which I have solved but I think it can be made much shorter. You see all the keyframes in a row marked green...[attached picture).. All of them are the same, they are just copied. The code on one of them are: (the same for all of them)
    Code:
    if (_root.guy.highkick.highkick.hitTest(this)) {
    	this.scores.gotoAndPlay("score100");
    	scores._xscale=this._xscale;
    	_root.panel.score += 100;
    	gotoAndPlay ("death4");
    	
    } else if (_root.guy.lowkick.lowkick.hitTest(this)) {
    	this.scores.gotoAndPlay("score100");
    	scores._xscale=this._xscale;
    	_root.panel.score += 100;
    	gotoAndPlay ("death3");
    	
    } else if (_root.guy.punch.punch.hitTest(this)) {
    	this.scores.gotoAndPlay("score200");
    	scores._xscale=this._xscale;
    	_root.panel.score += 200;
    	gotoAndPlay ("death1");
    	
    } else if (_root.guy.lowpunch.lowpunch.hitTest(this) and _root.score eq 1000000) {
    	gotoAndPlay ("nutpunch");
    	
    } else if (_root.guy.lowpunch.lowpunch.hitTest(this)) {
    	_root.panel.score -= 100;
    	this.scores.gotoAndPlay("score-100");
    	scores._xscale=this._xscale;
    	gotoAndPlay ("nutpunch");
    	
    } else if (_root.guy.jumpkick.jumpkick.hitTest(this) and _root.bonus eq 12) {
    	this.scores.gotoAndPlay("score5000");
    	scores._xscale=this._xscale;
    	_root.bonusmc.gotoAndPlay(2)
    	_root.bonus = 0
    	_root.panel.score += 5000
    	gotoAndPlay ("death2");
    
    } else if (_root.guy.jumpkick.jumpkick.hitTest(this)) {
    	this.scores.gotoAndPlay("score300");
    	scores._xscale=this._xscale;
    	_root.bonus += 1;
    	_root.panel.score += 300;
    	gotoAndPlay ("death2");
    	
    } else if (_root.killallenemies=="on") {
    	gotoAndStop ("kill");
    }
    I had to put this code on ALL the frames in a row otherwise when guy kicks or punches the badguy, nothing happends.



    If anyone of you understand what I mean (I know I don't), please explain how to only use this code once instead of alot of keyframes in a row..



    ehh, thanks
    Attached Images Attached Images
    Last edited by Ironclaw; 04-27-2003 at 04:05 AM.

  8. #88
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    Well, lookin at that, this is where I think OOP may start to come in handy, I don't know OOP at all so what I say may be totally wrong and I don't completly know the benefits but it's good for keeping your code organised. Also using OOP let you plan everything before you start to avoid this spaghetti like structur of code (which I do too)

    RipX

  9. #89
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    So..... heh ... what is OOP?

  10. #90
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    Object Oriented Programming!! There's loads about this stuff on the message board and i'm trying to learn it myself!! It's basically a way to assign code to objects and build code relative to real life situations / objects but that's about all I know to make constructing your code organised and easy to deal with! lol - Also check out

    C:\Program Files\Macromedia\Flash MX\Help\Flash\html\12_understanding65.html

    in your flash help file!

    RipX

  11. #91
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    Ehh, "Assigning methods to a custom object". Should this help me?

  12. #92
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    Maybe someone can break down this for you from the ActionScript Dictionary?

    Code:
    Circle.prototype.area = function() {
    	return Math.PI*this.radius*this.radius;
    };
    function Circle(radius) {
    	this.radius = radius;
    }
    var myCircle = new Circle(5);
    var myCircleArea = myCircle.area();
    trace(myCircle.area());
    Unfortunatly I can't!!

    RipX

  13. #93
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    Well, lookin at that, this is where I think OOP may start to come in handy
    Still cant understand how:

    Code:
    Circle.prototype.area = function() {
    	return Math.PI*this.radius*this.radius;
    };
    function Circle(radius) {
    	this.radius = radius;
    }
    var myCircle = new Circle(5);
    var myCircleArea = myCircle.area();
    trace(myCircle.area());
    OOP would help me.

  14. #94
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    Well, in all honesty, your code is scruffy as hell (tried not to say that) - Using OOP will help you to organise your code in a more structured and understandable way to avoid the messy layout of your code; using cases may also help you to avoid all that elseif/if bumpf.

    http://www.koyote.dk/index.php?sid=51

    RipX

  15. #95
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    Ok, but I'm such a newbie, I don't know how to do that. (make it smaller and less CPU demanding)

  16. #96
    Vox adityadennis's Avatar
    Join Date
    Apr 2001
    Posts
    751
    Here's an idea, it uses functions and will make your code a LOT smaller:
    instead of:

    if (_root.guy.highkick.highkick.hitTest(this)) {
    this.scores.gotoAndPlay("score100");
    scores._xscale=this._xscale;
    _root.panel.score += 100;
    gotoAndPlay ("death4");

    } else if (_root.guy.lowkick.lowkick.hitTest(this)) {
    this.scores.gotoAndPlay("score100");
    scores._xscale=this._xscale;
    _root.panel.score += 100;
    gotoAndPlay ("death3");

    use this:

    if (_root.guy.highkick.highkick.hitTest(this)) {
    hitcode("death4")
    } else if (_root.guy.lowkick.lowkick.hitTest(this)) {
    hitcode("death4")}

    function hitcode(flabel){
    this.scores.gotoAndPlay("score100");
    scores._xscale=this._xscale;
    _root.panel.score += 100;
    gotoAndPlay (flabel);}


  17. #97
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    Looks weird . I'll use my long code, it's easier to understand and read . thanks anyway



    What I would like to know is how to avoid using my code on ALL the keyframes... if I don't, the hitTest wont work...

  18. #98
    Vox adityadennis's Avatar
    Join Date
    Apr 2001
    Posts
    751
    How 'bout making a new layer and making a keyframe on the first frame and making the rest of the frames just plain ol' frames?
    Then stick the code on the keyframe, DONT use stop(), and it should work fine.

    Looks weird . I'll use my long code, it's easier to understand and read . thanks anyway
    Yeah, functions are kinda hard to understand at first, but they simplify your life after you get the hang of it. I love them

    I dunno if Ed Mack has any tutes on them, but seriously, give them a try.

  19. #99
    Senior Member mbenney's Avatar
    Join Date
    Mar 2001
    Posts
    2,744
    Originally posted by Ironclaw
    Still cant understand how:

    Code:
    Circle.prototype.area = function() {
    	return Math.PI*this.radius*this.radius;
    };
    function Circle(radius) {
    	this.radius = radius;
    }
    var myCircle = new Circle(5);
    var myCircleArea = myCircle.area();
    trace(myCircle.area());
    OOP would help me.

    well, this is as i would tell it...

    Circle is the class

    PHP Code:
    function Circle(radius) {
        
    this.radius radius;

    this would be the class constructor, a function which constructs a new instance of the circle class

    PHP Code:
    Circle.prototype.area = function() {
        return 
    Math.PI*this.radius*this.radius;
    }; 
    this is a prototype function of the circle class, all instances (all circles) will have access to this function.

    An instance of the circle class is constructed with:
    PHP Code:
    var myCircle = new Circle(5); 
    the circle class constructor asks for a radius of all its new instances, in this case myCircle has a radius of 5

    as myCircle is an instance of the circle class it has access to the Circle prototype function...
    PHP Code:
    var myCircleArea myCircle.area(); 

    hope this helps...

  20. #100
    Senior Member Kirill M.'s Avatar
    Join Date
    May 2002
    Location
    Toronto, Canada
    Posts
    711
    Also if you want to reuse the same function without having to redifine it in every mc or frame you can make a variable to point to it. Such as:

    function sayHi(){
    trace("Hi")
    }

    And in a MC that's inside the one with this function you can do:

    var hi=_parent.sayHi;

    And then call hi() inside the MC instead of _parent.sayHi().

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