Mark D
Mark D

Reputation: 75

Accessing Movieclip added by a different function in the same class?

I can't seem to find an answer to this problem. I can't get the following to work:

public class Callbattle extends MovieClip
    {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.InteractiveObject;
import flash.events.MouseEvent;
import global.Globalvar;

    public var battlegraphics:Array = new Array();
    public function Battle()
    {



        var battlewindow:MovieClip = new Battlewindow() as MovieClip;
        battlegraphics.push(addChild(battlewindow));
        battlewindow.x = 4; 
        battlewindow.y = 248;

        var attackbutton:MovieClip = new Battlebutton() as MovieClip;
        battlegraphics.push(addChild(attackbutton));
        attackbutton.x = 192; 
        attackbutton.y = 255;

        attackbutton.addEventListener(MouseEvent.CLICK, Attackclicked);


    }

    public function Attackclicked(event:MouseEvent):void{
        battlegraphics[attackbutton].y = 248;
    }

}

I'm getting the error

battlegraphics[attackbutton].y = 248;

Before trying this method of adding the movieclips to the array I was doing it like this with the same result:

public class Callbattle extends MovieClip
        {
    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.display.InteractiveObject;
    import flash.events.MouseEvent;
    import global.Globalvar;
public function Battle()
        {



            var battlewindow:MovieClip = new Battlewindow() as MovieClip;
            this.addChild(battlewindow);
            battlewindow.x = 4; 
            battlewindow.y = 248;

            var attackbutton:MovieClip = new Battlebutton() as MovieClip;
            this.addChild(attackbutton);
            attackbutton.x = 192; 
            attackbutton.y = 255;

            attackbutton.addEventListener(MouseEvent.CLICK, Attackclicked);

            while (Globalvar.battle == true){

                if (hero1turn == true){

                }
            }
        }

        public function Attackclicked(event:MouseEvent):void{
            attackbutton.y = 248;
        }
    }

I basically just want to be able to access a movieclip from another function.

Thanks heaps guys, any help would be amazing -Mark

Upvotes: 0

Views: 211

Answers (2)

pravid
pravid

Reputation: 729

battlegraphics[attackbutton].y = 248;

Array should take number and not the variable name you specified. So it will be

battlegraphics[1].y = 248;

Upvotes: 1

Vesper
Vesper

Reputation: 18757

The trick is, if you are referencing contents of an array, you use indexes of type int, or in some cases of type String, while your attackbutton is undefined in that function. The solution is pretty simple: declare attackbutton outside of Battle() function, and remove var from initiating attackButton.

public class Battle extends MovieClip {
    private var attackbutton:MovieClip;
    private var battlewindow:MovieClip;
    public function Battle() {
        attackbutton=new Battlebutton();
        battlewindow=new Battlewindow();
        ...
    }
    public function attackClicked(event:MouseEvent):void {
        attackbutton.y=248; // this is now valid
    }
}

Upvotes: 1

Related Questions