5 Replies Latest reply on Aug 8, 2007 1:38 AM by Newsgroup_User

    different question about communicating between classes in AS3

    Level 7
      Hello

      I have two classes - Person and Car - both classes are public and each
      class has its own .as file.

      public class Person extends Sprite
      and
      public class Car

      The package name on both files are the same, both classes are declared
      on the my first frame of my main timeline and they work correctly at
      this point.

      I also need to Person to access and use some of the Car methods and this
      is where the problem begins, if I try to call some method on the Car
      class from the person class I get all kind of errors, when using:

      CarInstance.Method();
      Error 1120, Access of undefined property CarInstance.

      Car.CarInstance.Method();
      Error 1119, Access of possibly undefined property CarInstance through a
      reference with static type


      I tried the following option with similar results:

      - On the Person.as file:
      import Car;

      - Tried to declare Car on the same file as Person but ouside the
      package, using Car as a helping class, but this way I cannot use Car
      from the main timeline because is not a public class.


      So, I need to access Car from the Person class and the fla, does anybody
      has an idea how to do this? any help will be very appreciated!

      Regards,
      Hugo Hiram.
        • 1. Re: different question about communicating between classes in AS3
          Marghoob Suleman Level 1
          Hi there...

          I made two classes. Person.as and Car.as.

          see this...

          • 2. Re: different question about communicating between   classes in AS3
            Level 7
            Hi

            The code seems to be ok, but I need to create instances of Car and
            Person from the main timeline, and be able to use Car from Person as
            well. I know that the following code doesn't work, but this is the idea
            of what I want to do:


            //Car.as
            package {
            public class Car {
            var private speed:int = 0;
            function Car() {

            }
            public function setSpeed(sp:int):void {
            speed = sp;
            }
            public function displaySpeed():void {
            trace("The speed is: "+speed);
            }
            }
            }

            //Person.as
            package {
            import Car;
            public class Person {
            var private currentCar:String;

            function Person () {
            //
            }

            function useCar(theCar:String):void {
            currentCar = theCar;
            }
            function currentSpeed():void {
            this[currentCar].displaySpeed(); //?????
            }
            }
            }

            //Main.fla
            import Person;
            import Car;

            var newPerson:Person = new Person();
            var car1:Car= new Car();
            var car2:Car= new Car();

            car1.setSpeed(100);
            car2.setSpeed(50);

            newPerson.useCar("car1");
            newPerson.currentSpeed();

            //Result
            The speed is: 100

            • 3. Re: different question about communicating between   classes in AS3
              Marghoob Suleman Level 1
              hi hugohiram,
              will this work for you...????

              see this...

              • 4. Re: different question about communicating between classes in AS3
                SymTsb Level 2
                If you want the function inside the classes to remain private but you need your Car class to be able to access the functions of Person, you need to extend the Person class onto the Car class...

                public class Car extends Person {

                This should give Car access to all of the members of the Person class even if they are private...
                • 5. Re: different question about communicating between     classes in   AS3
                  Level 7
                  Hi,

                  It did work, thanks for helping.

                  best regards,
                  Hugo Hiram.
                  ---

                  Marghoob Sulemaan escribió:
                  > hi hugohiram,
                  > will this work for you...????
                  >
                  > see this...
                  >
                  >
                  >
                  > //Person.as
                  > package {
                  > public class Person {
                  > import Car;
                  > private var currentCar:String;
                  > private var theLevel:Object;
                  >
                  > function Person () {
                  > //
                  > }
                  >
                  > public function useCar(Level:Object, theCar:String):void {
                  > currentCar = theCar;
                  > theLevel = Level;
                  > }
                  > public function currentSpeed():void {
                  > theLevel[currentCar].displaySpeed(); //?????
                  > }
                  > }
                  > }
                  >
                  > // Car.as
                  > package {
                  > public class Car {
                  > private var speed:int = 0;
                  > function Car() {
                  > trace("new car made")
                  > }
                  > public function setSpeed(sp:int):void {
                  > speed = sp;
                  > }
                  > public function displaySpeed():void {
                  > trace("The speed is: "+speed);
                  > }
                  > }
                  > }
                  >
                  > // on stage.
                  > import Person;
                  > import Car;
                  >
                  > var newPerson:Person = new Person();
                  > var car1:Car= new Car();
                  > var car2:Car= new Car();
                  >
                  > car1.setSpeed(100);
                  > car2.setSpeed(50);
                  >
                  > newPerson.useCar(this, "car1");
                  > newPerson.currentSpeed();
                  >