amko23
amko23

Reputation: 79

AS3 Run gotoAndStop from a class

I have the problem with Actionscript 3.0 in Adobe Flash. I can't run "gotoAndStop" from a class (not document class). With the help of the Internet I tried several things, but none of them worked:

1)

MovieClip(root).gotoAndStop(3);        

2)

package 
 {

import flash.display.MovieClip;

public class CustomClassName extends MovieClip
{
    public static var mainTimeline:MovieClip;
    public function CustomClassName()
    {
        // constructor code
    }
 }
}

3)

 public class np extends SimpleButton {
    var _root:MovieClip;

    public function np() {
        this.addEventListener(Event.ADDED_TO_STAGE,init);
        this.addEventListener(MouseEvent.CLICK,nextF);
    }

    private function init(e:Event):void{
        _root = MovieClip(this.root);
    }

    private function nextF(e:MouseEvent):void{
        _root.addEventListener(Event.RENDER,renderF);
        stage.invalidate();
        _root.nextScene();
    }

    private function renderF(e:Event):void {
        _root.gotoAndStop(5);
    }
}

I have these imports:

import flash.display.MovieClip;
import flash.display.Graphics;
import flash.display.Stage;
import flash.events.Event;

And if I run these lines of code:

trace('frame:',currentFrame);
super(this).gotoAndPlay(2);
trace('frame:',currentFrame);

... I get 0 as currentFrame as a result.

I have a class where I want to run gotoAndStop(2). And in my .fla file I have these in the first frame:

stop();
import Buzzer.*;

var buzzerClip:Buzzer = new Buzzer();
stage.addChild(buzzerClip);

But the code doesn't run the gotoAndStop function. And actually no error will be returned. Does someone has another idea?

Upvotes: 0

Views: 496

Answers (1)

invisible squirrel
invisible squirrel

Reputation: 3008

The property root is null until the display object has been added to the display list.

So to adjust your first attempt:

public function MyDisplayObject()
{
    init();
}

private function init():void
{
    addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}

private function addedToStageHandler()
{
    MovieClip(root).gotoAndStop(3);
}

Upvotes: 1

Related Questions