views:

117

answers:

2

Please help Stackoverflow! I have a MapInterface class in AS3 that inherits from Interface class.

public class Interface extends Sprite {
    public function Interface(){
        // do stuff
    }
}

and then

import com.georgecrabtree.Interface;

public class MapInterface extends Interface {
    public function MapInterface(){
        addMapButtons();
    }
    public function addMapButtons():void {
        trace("init");
    }
}

this all works fine, and when I create a new MapInterface class from the document class it traces out init. But when I try to call this:

var mapInterface:MapInterface = new MapInterface();
mapInterface.addMapButtons();

from the main timeline I get this error:

1061: Call to a possibly undefined method addMapButtons through a reference with static type com.georgecrabtree:Interface.

Thanks in advance for any help, George

A: 

If I understand the problem correctly, I think your second code sample has a typo. I imagine it is actually:

var mapInterface:Interface = new MapInterface();
mapInterface.addMapButtons();

The problem is that your variable is of type Interface, which does not define an addMapButtons() method. You can fix the problem either by changing the variable type to MapInterface, or by adding an addMapButtons() method to Interface and overriding the method in MapInterface.

Richard Szalay
Thanks for your help Richard, but surely if the MapInterface class extends the Interface class and then I create the mapInterface var as an MapInterface, it should be of type MapInterface? Also if I trace the variable trace(mapInterface) just before the addMapButtons(); function call it the output window shows that it is of type MapInterface?!
supercrabtree
Your original code looks like it should compile to me. If a variable is typed as a superclass, the child class properties are not available (unless you cast the variable to the child class type).
Richard Szalay
So, the code by supercrabtree did had a typo?
LopSae
Still waiting for confirmation :)
Richard Szalay
A: 

Hi guys thanks for your help and attention on this one. After a good nights sleep I finally figured out my problem. Obviously the code I posted above was a watered down version of what I actually had.

The problem was that my document class was not extending Sprite, it was extending a class called Installation which itself extends Sprite. This was because I am making 10 installations and they all have a a bunch of common ground, they all have a screen saver, intro video, etc. They all have an interface too, which is where my problem came in, as 9 of the installations use the same interface, but one (the map installation) needed to have a few extra GUI elements.

My problem was that my Installation base class looked like this (im showing a trimmed down version):

public class Installation extends Sprite {

    protected var _interface:Interface;

    public function Installation(){
        //do stuff
    }
}

So when my MapInstallation extended this class it inherited the _interface variable. So when I created the _interface variable as type MapInterface..

_interface = new MapInterface();

..the complier recognised the new MapInterface as of type Installation but the _interface variable was still strongly typed as Interface, so when I later tried to call the addMapButtons() function on _interface, although the function was there the complier had other ideas. My solution was to using casting..

(_interface as MapInterface).addMapButtons();

and it all worked hunky dory. Thanks for your help guys :)

supercrabtree