views:

33

answers:

3

I have two classes. The Main class calls a function, which is defined in a Second class. I'm getting the following error: Error 1120: Access of undefined property myFunction

Basically, I am creating buttons in the Main class that will add a corresponding Child to an Object in the Second class (if you click one button, child x1 will be added, if you click another button, child x2 will be added, and so forth).

Here's the relevant code for the Main.as file:

package
{   
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;         
    public class Main extends MovieClip {
        private var x1:X1 = new X1();
        private var x2:X2 = new X2();

        public function Main():void {
            addPlayers();           
        }

        public function addPlayers():void {
            addChild(x1);
            addChild(x2);
            x1.x=325;
            x1.y=5;
            x2.x=366;
            x2.y=5;

            x1.label = "dog";
            x2.label = "cat";

            x1.addEventListener(MouseEvent.CLICK, selectPlayer);
            x2.addEventListener(MouseEvent.CLICK, selectPlayer);
        }       
    }
}

The Second.as file code is:

package  
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.MouseEvent;

    public class Second extends MovieClip
    {
        public var myVar:MyVar = new MyVar();

        public function Second():void
        {   
            addChild(myVar);                        
        }

        private var mc_x1:Mc_x1 = new Mc_x1();
        private var mc_x2:Mc_x2 = new Mc_x2();      

        public function selectPlayer(event:MouseEvent):void
        {
            if (Game(this.parent).turn == 0) {
                myVar.addChild(mc_x1);
            } else {        
            switch (event.currentTarget.label) {
            case "dog":
            myVar.addChild(mc_x1);
            break;
            case "cat":
            myVar.addChild(mc_x2);
            break;
            default:
            myVar.addChild(mc_x1);
            }
            }   
        }   
    }   
}

i've tried defining a new variable as a public static var and that hasn't worked. i've also tried to import the Second class in the Main class and that didn't work either. any thoughts? thank you!

+2  A: 

If I'm understanding what your code says, you can't do what your doing. Even though you are listening to events on X1 and X2, you are listening to them FROM main. In other words, main is attempting to handle the event, and it's not finding the function you specified (selectPlayer). If you want all event handling to happen in main, then main will have to call into X1 and X2 when it receives an event.

ThatSteveGuy
Hi ThatSteveGuy,Thanks for your reply! I don't understand what you mean by "then main will have to CALL INTO x1 and x2 when it receives an event." Can you please clarify? Thank you!
julie2010
There are actually a LOT of ways to accomplish what you are trying to do, and I'm not sure I'm reading your code right, but one way is to have the eventHandler (selectPlayer) in main, and a public function on your player class (Second) called something like setAnimal. Your event handler in main would then do something like: (event.currentTarget as [YourPlayerClassName]).setAnimal(); setAnimal could take a parameter if you need it to, which specifies the new animal. You may want to listen for events on Second as an alternative (as widged suggested).
ThatSteveGuy
A: 

try this

 public function Second():void
 {   
            this.addEventListener(MouseEvent.CLICK, selectPlayer);
            addChild(myVar);                        
}

(and don't forget to remove x1.addEventListener, x2.addEventListener from main)

widged
+2  A: 

I agree with ThatSteveGuy , in Main you are calling a function that doesn't exist, namely selectPlayer(). Following your code the first thing you should do is add a selectPlayer() function in Main.

Then you would need to add an instance of the Second class in Main in order for the following to work

private function selectPlayer(event:MouseEvent):void
{
    second.selectPlayer(event);
}

Now it's a bit difficult to advise you any further because this way of doing things looks a bit convoluted but then again I would need to see the big picture. This is just an explanation about why your code doesn't work. Also, in the Second class , selectPlayer may throw an error because Game(this.parent ) will return a null value so you won't be able to access the turn property...

Is there a good reason why you need to add the two buttons in Main as opposed to adding them in Second?

PatrickS