0

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
         //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!

1 Answer 1

2

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();
      }
   }
Sign up to request clarification or add additional context in comments.

3 Comments

+1 all class constructors will automatically be called on instantiation of that class.
Wow, to think it is that simple... Thanks a bunch Daniel! ---- One more question though, what is the difference between "var myClass = addChild(new MyClass()) as MyClass;" and "var myClass:MyClass = new MyClass;"?
There is not a real difference, but personally I never assign values for complex types in a class property definition. I create instances of complex types the time when I need them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.