A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: is using include a good idea?

  1. #1
    Senior Member
    Join Date
    Jul 2008
    Posts
    418

    is using include a good idea?

    i have made a set of setters and getters for speed, dir, accelleration and accelerationDir. i put them in a .as file, without a class or package, just the functions. and then include "spdDirFunctions.as" in the decleration of a class that needs them. is this the good way to do it?

  2. #2
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    The best way to do it is to make an Interface that contains references to those getters and setters, then make a base class that implements the interface and contains the get/set functions themselves, and then make each of your classes extend that base class.

  3. #3
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    Unless that is these are just functions that do some operation on a variable and return the same variable with a modified value based on some other input, in which case you don't want getters or setters at all, you want to create a public class full of static functions that just do the mods you want and can be called from anywhere without ever instantiating your math class.

  4. #4
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    no they dont just calculate values from input. the .as file also declares 6 vars, for the class. but why is it better to have an abstract class for this?

    btw it is also nessesairy to adjust x and y in a timereventfunction, in which case it would be better to have an abstract class. but not every class has the same timer. so it wouldnt work out anyway.

    ps why would i use an interface in that case? i would already have an abstract class. (i'm new to interfaces btw :P)

  5. #5
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    I've come to believe in writing interfaces 'cause I always reuse code for other projects later. I was a relatively late convert, and like most of those, I'm fervent about it.

    The point is, you want to whittle down the most basic things all your moving display objects share in common to a set of functions in a base class, and extend that class for each moving object, overriding the base class functions where necessary.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    This is my opinion, but I think it's valuable to put out there. When you are writing any code longer than a few lines, one of your main considerations should be "If I gave this to another developer, could they use it easily?" It helps you organize things in a way that makes sense, and encourages re-usable code.

    And most importantly, three months down the road, you ARE a different developer and will curse your stupid self if you don't do it.

  7. #7
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    IMO Include is always a bad idea, and is even more of a bad idea if you are putting it in the middle of the code.

    For example:
    Code:
    class Foo {
        var x;
        var y;
        
        public Foo () {
            ...
        }
    
        #include "Math.as"
    }
    Suddenly you have an unknown amount of code that does who-knows-what sitting in a class that now has a very vague purpose.

    On top of that, you bring along most of the potential pitfalls of macros. What happens if your Math.as file also has a variable (or function) named x?

    At best, you are going to get some cryptic error message that might suggest what happened. At worst, you are going to get some cryptic error message pointing to an incorrect line number or a line that doesn't exist.


    Finally, there are far better ways to do this. You have essentially just hacked together a fragile and syntactically-ugly method of implementing inheritance. (I have actually seen recommendations to do this to fake OOP in old, old versions of flash).

    Josh has the idea essentially down, but you want to either inherit from a class or an abstract class. Interfaces do not allow implementation of functions which defeats the point of reducing redundant code.

    To sum it all up, you are just hacking in behavior which already exists in the language, except your method uses an outdated compiler feature, makes the syntax ugly and introduces the potential for errors that will be very difficult to find.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  8. #8
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    fair enough. i'm convinced.
    no more include at all?

    edit: if i put in a timerHandler
    x += hspeed;
    y += vspeed;
    but the subclasses need different times for that, what do you do?
    Last edited by omniscient232; 10-28-2008 at 01:02 AM.

  9. #9
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    the 2 classes i created are both abstract, and 1 extends the other the other extends sprite. the thing is. maybe i'll need an object that subclasses those 2 classes but is also a MovieClip. how do i do this? i can't make interfaces out of those classes, because they both declare vars and define functions.

  10. #10
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    Ah, it can be a little tricky to get a situation like that working but in your case I would do this:

    IFoo // wasAbstract class 1
    IBar // was Abstract class 2
    AFooMC extends MovieClip implements IFoo // Now include the implementation of foo/bar here
    AFooBarMC extends MovieClip implements IFoo, IBar or AFooBarMC extends AFooMC implements IBar
    ABarSprite extends Sprite implements IBar
    (Interfaces prefixed with I, abstract classes with A)

    Its more of a pain when you combine various combinations of interfaces and preexisting classes into abstract classes like this, but I would still prefer it to include so you get the polymorphism and you aren't playing tricks with the compiler.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  11. #11
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    what i find so anoying about interfaces, is that you have to define all the methods anyway. so basically i just copy the two classes and make 1 for sprite 1 for mc.

    i still don't truly get interfaces. the really only thing they do is turning errors into compile errors right? they don't do anyhthing?

    edit: maybe i should use composition?
    Last edited by omniscient232; 10-29-2008 at 12:41 PM.

  12. #12
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    Quote Originally Posted by omniscient232
    what i find so anoying about interfaces, is that you have to define all the methods anyway. so basically i just copy the two classes and make 1 for sprite 1 for mc.

    i still don't truly get interfaces. the really only thing they do is turning errors into compile errors right? they don't do anyhthing?

    edit: maybe i should use composition?
    Interfaces (And all of OOP including classes and inheritance) is really there for the polymorphism (Although since AS has dynamic typing it only affects performance). Composition would also be a good solution for this but I tend to avoid it in these kind of situations because typing mc.Values.X instead of mc.X gets annoying fast.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

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