MKII
MKII

Reputation: 911

Undefined MovieClip array, for some reason (Actionscript 3.0)

I am simply trying to do one of those "match-2" games. I just started doing it, and since I am a beginner, I am trying to understand how Arrays work. Therefore I wrote this simple program:

package  {

import flash.display.MovieClip;
import flash.events.MouseEvent;

public class Main extends MovieClip
{
    var Ar:Array = [];
    Ar[0] = A;
    Ar[1] = B;
    Ar[2] = C;

    public function Main()
    {
        for( var i = 0; i < 3;  i++ )
        {
            Ar.buttonMode = true;
            Ar[i].addEventListener( MouseEvent.MOUSE_OVER, MouseOverAct );
            Ar[i].addEventListener( MouseEvent.MOUSE_OUT, MouseOutAct );
        }
    }

    public function MouseOverAct( mouseEvent:MouseEvent ):void
    {
        mouseEvent.target.alpha = 0.1;
    }

    public function MouseOutAct( mouseEvent:MouseEvent ):void
    {
        mouseEvent.target.alpha = 1.0;
    }
}

}

However, after declaring the array and trying to put the MovieClips (which are already on the stage, with instance names A, B, C) inside it I get an "Undefined property" error. I have tried to correct it using Ar.push(), but it doesn't work as well. Can someone help me?

Upvotes: 1

Views: 310

Answers (1)

Emin A. Alekperov
Emin A. Alekperov

Reputation: 1067

This

var Ar:Array = [];
Ar[0] = A;
Ar[1] = B;
Ar[2] = C;

is an incorrect code. You should initialize an instance property (in your case the array) either at variable declaration or at any method. It is possible to initialize a static protperties in static block. I think this link about static block initialisation would helpfull for you. So you should do either:

public var _array:Array = [A, B, C];

or

public var _array:Array;

public function Main()
{
    _array = [A, B, C];
    for( var i = 0; i < 3;  i++ )
    {
        _array.buttonMode = true;
        _array[i].addEventListener( MouseEvent.MOUSE_OVER, mouseOverHandler );
        _array[i].addEventListener( MouseEvent.MOUSE_OUT, mouseOutHandler );
    }
}

Upvotes: 2

Related Questions