A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: How to not Import Everything?

  1. #1
    Senior Member
    Join Date
    Aug 2001
    Posts
    227

    How to not Import Everything?

    Ok, i've got a fairly simple problem. I don't want to globally open some specific packages. (for name overlapping instances, etc.)

    So i simple want to be able to create an object like such:
    Code:
    this.square = new AS.Geo.Square();
    But that throws the following error:
    Code:
    **Error** C:\~~\asv301\AS\UIMng\Index.as : Line 10, Column 21 : [Compiler] Error #1046: Type was not found or was not a compile-time constant: Square.
    		var square:AS.Geo.Square;
    ReferenceError: Error #1065: Variable Timeline0_64312f8557c4d469a75a66628e9025 is not defined.
    Which confuses me, because it works fine if i import it such as:
    Code:
    import AS.Geo.Square;
    So any ideas?

  2. #2
    Member
    Join Date
    Sep 2006
    Posts
    43
    Importing is not optional in AS3

  3. #3
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    So you must import everything to the class level? You can't cast an object with a reference to where it is located?

  4. #4
    Member
    Join Date
    Sep 2006
    Posts
    43
    You can import a class (import package.Class) or all the classes within a package (import package.*). What you can't do is have two Classes with the same idenitifier in the same namespace at the same time - which is a good thing.

  5. #5
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    Yea you have to name them correctly, i am with you on that.. but in other languages you can use two of the same class name (ie Square) if you call them from different places.

    ie
    var blah = new AS.Shapes.Square();
    var blah2 = new AS.Objects.Square();


    but with imports, that won't work because in terms of each package, you are globally defining the square outside of its own package.. and thus limiting you on names.


    Mostly it just kinda sucks if you make a big class, then down the road find out its a name conflict with a class flash already has, but you were just never using that other flash class.

  6. #6
    \x3a\x6f\x29
    Join Date
    Sep 2005
    Location
    paris
    Posts
    88
    There is also a missunderstanding in what Adobe calls a namespace and a namespace generally is.

    Adobe reference says
    - Namespaces of XML objects
    - Namespace to differentiate methods
    - Namespaces for access control

    Cplusplus.com says
    Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.
    Namespaces you may use for classes are public, private and internal.
    Functions may use public, private, internal and protected afaik.

  7. #7
    Member
    Join Date
    Sep 2006
    Posts
    43
    Sorry, I just realised all you need is:

    var myFoo:AS.Geo.Square = new AS.Geo.Square();

    You forgot to declare your type
    Last edited by misuser; 09-12-2006 at 12:26 PM.

  8. #8
    Member
    Join Date
    Sep 2006
    Posts
    43
    @joa__

    I'm not sure I get your point - This is how adobe describe packages...

    Packages in ActionScript 3.0 are implemented with namespaces, but are not synonymous with them. When you declare a package, you are implicitly creating a special type of namespace that is guaranteed to be known at compile time.

  9. #9
    \x3a\x6f\x29
    Join Date
    Sep 2005
    Location
    paris
    Posts
    88
    It is about terminology. It is a little bit confusing if you have some C++ experience for example. That's all what I wanted to address. Maybe a little bit stupid because I read about namespaces etc. and I thought Zeusbwr was thinking about namespaces as a "Namespace" (which is a new feature of AS3).

  10. #10
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    Quote Originally Posted by misuser
    Sorry, I just realised all you need is:

    var myFoo:AS.Geo.Square = new AS.Geo.Square();

    You forgot to declare your type
    I tried that though.. well ni a different way.

    If you look at my example, i declare my object like that
    Code:
    var square:AS.Geo.Square;
    and i define it like that also
    Code:
    this.square = new AS.Geo.Square();
    (the first being at the top of the class, the 2nd being inside a function in the class)
    I should'nt have to declare the variable type twice right? Infact i would imagine that would throw an error.

  11. #11
    Member
    Join Date
    Sep 2006
    Posts
    43
    Ok so I guess you're doing something like this?

    Code:
    package {
    	import com.myPackage.Square;
    	import com.myOtherPackage.Square;
    	
    	public class myClass
    	{
    		public var square1:com.myPackage.Square;
    		public var square2:com.myOtherPackage.Square;
    		
    		public function myClass()
    		{
    			square1 = new com.myPackage.Square();
    			square2 = new com.myOtherPackage.Square();
    		}
    	}
    }
    Also remember that class / package name are case sensitive - Also lower case "as" is now a reserved word so that might be causing you problems too

  12. #12
    Senior Member
    Join Date
    Aug 2001
    Posts
    227
    Well the import example i gave works just fine.. so i assume the package name is fine.. :/

    Dunno, it'll probably work itself out eventually.. thanks

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