开发者

Controlling/initializing external Class [AS3]

Before firing away, I know there are many questions here on SO that are quite similar. Yet, none of the solutions given were of any help to me, probably because my case is a little different.

I have a main class which loads an external class (separate .as file). In this external class, there are several objects which have tweens and time events bound to them. What I want to do, is starting the animations when a certain function is called in my Main class. However, I've tried numerous things to stop and/or reset the animations in the external class, so it will start from the beginning if the required function in Main is called.

Main.as:

package  {
   //required imports

   public class Main extends MovieClip {
   var myClass:MyClass = new MyClass; //this is the external class
   var button:Button = new Button; //movieclip in the library   

      public function Main() {
         addChild(myClass); //I want to do this here so the objects show from the start
     开发者_如何学Python    //try 1: myClass.gotoAndStop(1);
         //try 2: myClass.stop();

         button.addEventListener(MouseEvent.MOUSE_CLICK, playAnimation);
      }

      function playAnimation (e:MouseEvent) {
         //try 1: myClass.gotoAndPlay(1);
         //try 2: myClass.start();
         //try 3: controlling the startTweening() function in MyClass, I tried different ways
      }
   }
}

The problem starts in the Main class above. I don't want to animate yet!

MyClass.as:

package {
  //required imports

   public class MyClass extends MovieClip {
      //vars

      public function MyClass() {
         startTweening();
      }
      function startTweening() {
        //tween event
        //calling next function (with use of a TimerEvent) after tween is done. This is repeated several times.
      }
   }
 }

Everything in this class works fine, so that's not the problem. If this makes any difference, I used TweenMax in MyClass for tweening. I didn't use the timeline in the .fla.

Any help would greatly appreciated!


If you don't want to animate at creation of MyClass remove startTweening(); call from the constructor of MyClass.

Make startTweening(); a public function and call it whenever your need with myClass.startTweening().

Here the MyClass

public class MyClass extends MovieClip {
  //vars

  public function MyClass() {

  }

  public function startTweening() {
    //tween event
    //calling next function (with use of a TimerEvent) after tween is done. This is repeated several times.
  }
}

and here the Main class

public class Main extends MovieClip {
   var myClass:MyClass;
   var button:Button = new Button; //movieclip in the library   

      public function Main() {
         myClass = addChild(new MyClass()) as MyClass; 
         button.addEventListener(MouseEvent.MOUSE_CLICK, playAnimation);
      }

      function playAnimation (e:MouseEvent) {
         myClass.startTweening();
      }
   }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜