0
votes

I'm developing a game in AS3. There is a Weapon superclass, which contains methods such as shoot and reload, which will behave the same across all weapons.

The specific weapons, such as Pistol, Shotgun inherit from this class so they can use these methods. They have public static variables, such as what type of bullet to shoot, rate of fire, bullet spread, that make them unique, and are used in these methods. They need to be public static variables so I can look them up from somewhere else in the core when all I've got there is the type of weapon that was fired.

Is this how I should be trying to do it? How does the Weapon superclass access these variables?

1
I think an Interface is a good approach for this design. I have to believe that a shotgun and a pistol will require specific functionality and therefore should be handled in the appropriate class. - prototypical
Here's a good link to learn about an Interface, sorry to not explain it all. Don't have the time right now to detail it, but this will help you understand a good approach -> blog.shoguniphicus.com/2011/02/02/… - prototypical

1 Answers

0
votes
public static const RATE:uint = 2;

That is accessed by the Weapon class either as Weapon.RATE or as RATE. Scope works a little weird when it comes to static objects. I personally don't think you should be able to access the static objects with just RATE, but it works.

Subclasses do not inherit static properties and methods. They belong solely to the class they are created in (which makes sense if you know what a static object really is). So for all classes, even classes that extend Weapon, you must access a public static object via Weapon.RATE.

There is an oddity I have noticed however. If you use the protected access modifier instead of public, classes can access static objects in their super classes via RATE, as if it were created within the class itself. I don't know the logic behind that, but it works.

So:

public class Weapon {
    protected var RATE:uint = 2;
    public var RATE2:uint = 5;
}

public class Gun extends Weapon {
    trace( RATE ); // output 2
    trace( Weapon.RATE ); // output 2
    trace( RATE2 ); // output Error, access of undefined property
    trace( Weapon.RATE2 ); // output 5
}

EDIT: In response to the first comment:

The way superclasses work, an object that extends a class has access to all public and protected objects in the super class.

So let's say the weapon class is this:

public class Weapon {
    public function shoot():void{}
    protected function reload():void{}
    private function aim():void{}
}

You would access those methods within the subclass like you would in the super class itself:

public class Pistol extends Weapon{
    public function Pistol() {
        this.shoot(); // works
        this.reload(); // works
        this.aim(); // doesn't work because it is private
    }
}

Now if you are looking to abstract things further, you can set up properties within your super class with a protected or public modifier with a default value for all weapons. In your superclass methods, you simply call these values. In the subclass, you change them to be whatever you need them to be

public class Weapon {
    public var rate:uint = 2;

    public function shoot():void{
        // use this.rate here
    }
    protected function reload():void{}
    private function aim():void{}
}

public class Pistol extends Weapon{
    public function Pistol() {
        this.rate = 5; // value of rate is now 5 and will be used in shoot()
        this.shoot(); // works
        this.reload(); // works
        this.aim(); // doesn't work because it is private
    }
}