Logan
Logan

Reputation: 21

Actionscript: Classes inside Classes

i'm trying to create a flash game, but i'm having trouble with my classes. i'm importing the classes on the main script. they're imported okay, i've tested it all. but i dont know how to use classes inside another class

this is what i've got so far:

class Class.Player {
  public static var self:MovieClip;
  public static var bullet:Class.Bullet;

  function controls() {
    //Shoot
    if (Key.isDown(Key.SPACE)) {
      bullet = new Bullet(100, 100);
  }
}

it loads the class and declares the variable, but it claims "Bullet" is not an existing method. it is an existing method, and it works when i call it from the main script.

also, do i need to declare the class like public static var bullet:Class.Bullet;? it's the only way that works for me but wondered if there's a better way?

Upvotes: 1

Views: 356

Answers (2)

Amy Blankenship
Amy Blankenship

Reputation: 6961

You're putting your package information in the Class name. I have no idea how this is even compiling for you (especially given that Class is a reserved word.

Your files should look more like:

package somePackage {//somePackage instead of the reserved word Class you were using

    import someOtherPackage.Bullet;

    public class Player extends MovieClip {
         public var bullet:Bullet;
         public function Player():void {
            super();
            //other constructor logic here
         }
    }

}

Upvotes: 0

Jan
Jan

Reputation: 2522

It looks like you are having problems with the naming of your classes. You don't have to call your classes Class.Name. Just use Name.

In your case, you could have a file called

Player.as

With this content:

package {
    import flash.display.MovieClip;

    public class Player extends MovieClip {
        public private var bullet:Bullet;

        public function Player(){
            //constructor
        }

        function controls() {
            //Shoot
            if (Key.isDown(Key.SPACE)) {
                bullet = new Bullet(100, 100);
            }
        }
    }
}

And then a file called

Bullet.as

With this content:

package {
    import flash.display.MovieClip;

    public class Bullet extends MovieClip {

        public function Bullet(x:Number, y:Number){
            //constructor
        }

    }
}

You can read more information about creating and using custom classes here: http://www.flashandmath.com/bridge/intro/class.html

Upvotes: 1

Related Questions