A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: external class

  1. #1
    Junior Member
    Join Date
    Nov 2000
    Posts
    11

    external class

    i know this is a stupid probably obvious question but after spending a ridiculous amount of time on it i give up. I have a class lets say Poker in an actionscript file called Poker.as. i want to use this class in an fla in the same directory. i instantiate it in the fla with the following line:
    var myVar:Poker = new Poker();

    This however gives me the errors:
    1046: Type was not found or was not a compile-time constant: Poker.
    1180: Call to a possibly undefined method Poker.

    I have even tried doing "import Poker;" and that does nothing. What im doing i know works just fine in actionscript 2 but damn this actionscript 3. please help, i know this should be an easy answer, thanks in advance.

  2. #2
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    you need to define a classpath so that the compiler knows where to search for the AS file(s). To do this goto publish settings (F12?- not sure though) and somewhere there should stand compiler and or classpath.

  3. #3
    Senior Member
    Join Date
    Sep 2001
    Location
    Manhattan
    Posts
    398
    If the Poker.as file is in the same directory as the fla then you do not need to import the classpath. You have another issue. From the looks of it you are doing everything right in instantiating you Poker Class. It is something simple in your setup somehow. Either it is not in the same directory or you have something named wrong like the constructor name in the class file. Double check everything and if you still can't find the problem post the files somewhere so we can take a look.
    JA

  4. #4
    Junior Member
    Join Date
    Nov 2000
    Posts
    11
    i have attached just a demonstration simple class that i cant get to work that illustrates my problem.
    The two files are simply TestClass.as:

    class TestClass {

    var myVar:int;

    function TestClass() {
    myVar = 5;
    }

    function getMyVar():int {
    return myVar;
    }

    }

    and TestFLA.fla that has the following script on the first frame:

    var newInstance:TestClass = new TestClass();
    trace(newInstace.getMyVar());

    both files are in the same directory. thanks in advance.
    Attached Files Attached Files

  5. #5
    Junior Member
    Join Date
    Nov 2000
    Posts
    11
    i seem to have gotten around my problem by putting my class into a package. but for future reference it would be really nice to know what was wrong with my previous code.

    For reference to anyone that has a similar problem, ill explain in further detail. i simply took my Deck class and added "package poker {" before class declaration (obviously closing bracket at bottom of class too). I then put that class in a folder named "poker" and in my fla imported it using "import poker.*;". my directory structure was simply fla and poker directory in base folder with Deck.as in poker folder. Also made Deck class public along with any functions i needed to call. I also set classpath in publish settings to the base directory.

  6. #6
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    you are missing the package { } block. The package block is required for AS3, even if you aren't in a package.

  7. #7
    Senior Member
    Join Date
    Sep 2001
    Location
    Manhattan
    Posts
    398
    Your issue is a namespace issue. When you do not strictly define your classes, methods, variable members, etc they are put in the the default namespace of "internal". That being the case you were not allowed access to the class you created because it was not publicly available. All you needed to do to your old code was the following:

    Code:
    package{
    
         public class TestClass {
    
              protected var myVar:int;
    
              public function TestClass() {
                   myVar = 5;
              }
    
              public function getMyVar():int {
                   return myVar;
              }
    
         }
    
    }
    It really a good habit to always use strict typing etc so that you never run into this sort of problem. I hope this helps.
    Last edited by JHarlequin; 07-30-2007 at 01:49 PM.
    JA

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