A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: classes

  1. #1
    Senior Member x-death's Avatar
    Join Date
    Aug 2009
    Posts
    175

    classes

    How would you write the import class thing?

    when your making a class you need to import classes. i want to know how to right them. i think its something like this, am i right?
    Code:
    import packageName.className
    is that right? or do you just right package name instead?

    and also how would you know what classes your using?

    is itjust the methods you use or is it the properties as well, also is there anything else that would determine what class your using?. and would some properties cross over into other classes as well?

    for example the x property maybe a property in more then one class.

  2. #2
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    You should import only the Classes you need.

    If you want to import the Class ClassName which is in the package packageName, yes, you write
    PHP Code:
    import packageName.ClassName
    If you want to import ALL the classes in a package:
    PHP Code:
    import packageName.*; 

    As for knowing the Classes that you need to import... it's almost equivalent to knowing which Classes you want to use...
    Sometimes it's hard to know/remember in which package these are, but that's what the documentation is for.
    If you used getTimer() in AS2, now you'll have to know that, in case you want to use it in AS3 you'll have to
    PHP Code:
    import flash.utils.getTimer
    Using an ActionScript editor like FlashDevelop is a big help.

    When you have a Class, you may have both methods and properties.
    These properties should be private (not accessible outside of the Class), and you should have methods (getter and setter functions) to access those properties.

    You can have as many Classes with the same methods and properties as the others, as you wish.
    Even more, you can have Classes whose structure is based on other Classes. This is one of the bases of OOP: one Class extends the other.
    You may have seen something like this:
    PHP Code:
    package
    {
     public class 
    MyClass extends Sprite
     
    {
      
    // rest of the class definition goes here...
     
    }

    Last edited by nunomira; 09-05-2009 at 05:41 AM.

  3. #3
    Senior Member x-death's Avatar
    Join Date
    Aug 2009
    Posts
    175
    ok so that solves how to write them. but still unsure what determines the classes you need t import. is it methods or properties or what? i have no idea which one determines what class you would import. and i have no idea what the whole extends is or why that is used....

  4. #4
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    A Class is supposed to have certain functionality associated with it (otherwise it's useless).

    If you want to calculate the sine or cosine of an angle, you use the Math Class because it's the class that has (static) methods to do it.

    If you want to play a sound, you use the Sound Class.

    If you want to preform some easing, you have the great TweenLite Class.

    ... and so on...

    Basicaly, you need to be acquainted with existing / native Classes so that you know which to use.

    After understanding how to use existing Classes, writing your own will be much easier.
    Obviously, all of this requires some study. Reading a good book is a great help.

  5. #5
    Senior Member x-death's Avatar
    Join Date
    Aug 2009
    Posts
    175
    your not understanding my question. i can program what i want to program just fine. and i can make classes just fine also. i want to know if i make a class and do a custom function that does what ever. how am i to know what classes i need to import? how am i to even know all the classes i'm accessing. i wouldn't have the slightest clue. so i want to findout how i would. because every method has its own class but i don't know what determines the class you access if its just the methods or if its the properties also. i don't know and i need to know.

    its really bugging me, not knowing.

  6. #6
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    You need to import the Classes that you want to use.

  7. #7
    Senior Member x-death's Avatar
    Join Date
    Aug 2009
    Posts
    175
    -hits face with palm of hand-

    your still not understanding. thats exactly what i just asked. i want to import the classes in, but i don't know what ones i need to import!

    so in order to find out do i look at all the methods and properties i've used and search for them in the packages until i find the correct class? because i honestly don't know. thats why i asked so i know what ones to import in any situation. thats why i asked what determines what classes you import, methods, properties, both?

    thats why i asked all these questions. so i knew what to import in what ever situation i'm put in.

  8. #8
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    Quote Originally Posted by x-death View Post
    so in order to find out do i look at all the methods and properties i've used and search for them in the packages until i find the correct class?
    If you've created a script that uses a bunch of classes that require importing and you didn't import them, then yes, you basically have to go through that script, find each case where a class is referenced, and add the necessary import code.

    Some IDEs might have auto-importing tools that make this easier (I'm not sure if Flex can do this or not), and you can always just try compiling and go off class-not-found errors too.

    The basic rule is, for each script (i.e. class, or anywhere in a timeline for the current symbol/main timeline in Flash) if a class is defined within a package, if you reference the class by name, that class needs to be imported. The important part there is referencing by name. For example,
    Code:
    var s:Sprite = new Sprite();
    trace(s.scrollRect);
    The only class that needs to be imported for this script is flash.display.Sprite. Even though we're referencing scrollRect which is of the type flash.geom.Rectangle, because "Rectangle" was never referenced in code, there's no need to import it.

  9. #9
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    You need to import the Classes that you want to use.
    In order to know the Classes that you want to use, you need to know that they exist and what they do.
    For me, this is basic logic, but I'll try to help you going another way...

    If you say that you're already using methods and properties, it means that you're using methods and properties of an Object.
    An Object is an instance of a Class, so, you should know which Class it belongs to.

    For example, if you have a MovieClip Object, you can use the gotoAndStop() method.
    On the other hand, if you have a TextField Object, you can't use this method, because the TextField Class doesn't have a gotoAndStop() method.
    So, as you already know that you have a MovieClip and a TextField Object, you have to import both Classes.

    No, the methods or properties don't directly determine the Classes you need to import. What determines the Class you need to import is the Class the methods and properties belong to.
    Example. Consider the following Document Class:
    PHP Code:
    package
    {
        
    import flash.display.Sprite;
        
    import flash.display.MovieClip;
        
        public class 
    MyClass extends Sprite
        
    {
            public function 
    MyClass()
            {
                var 
    mc:MovieClip = new MovieClip();
                
    addChild(mc);
            }
        }

    No methods or properties of the MovieClip Class were used. But an instance of this Class was created, so, flash.display.MovieClip was imported.

  10. #10
    Senior Member x-death's Avatar
    Join Date
    Aug 2009
    Posts
    175
    the reason i asked this is because i had an idea for a utility. see you place code in this input text field it searches through it and when you press button it will tell you all the classes being used. and then which ones need to be imported in.

    it was a good idea at first this way people wouldn't have to think so much towards the classes if there feeling a bit lazy. i figured search the string for each property and basically say if its found write that packageName and class in the results. but it seems that the methods i use in my script don't have any effect on what classes are imported according to you guys. its simply the variable types?

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