Skip to main content
Inspiring
October 13, 2013
Answered

How to code different scenes in package function

  • October 13, 2013
  • 2 replies
  • 2635 views

package actions {

    

    import flash.events.MouseEvent;

    import flash.display.MovieClip;

    import flash.text.TextField;

   

   

    import flash.events.Event;

    import flash.text.*;

    import flash.net.*;

   

    

        

    public class submit extends MovieClip {

        

        public function submit():void {


     }

}

Somehow it comes to a point I need to learn how to use package vs previously I put all the codes into .fla..

So I'm learning this, this is the basic package, when in the main.fla I have document Class linked to 'action.submit'. And I have a folder called 'action' and inside is this 'submit.as' that has the code above in it.

To my understand whatever within 

public function submit():void {

    

     }

will  be loaded first? But my question is what happend if I have more than 1 scene, like for example scene 1 I have 'bttn1', scene 2 I have 'bttn2'. If I do it like this

public class submit extends MovieClip {

        

        public function submit():void {

              

              bttn1.buttonMode = true;

               bttn1.addEventListener(MouseEvent.MOUSE_DOWN, gotoScene2);

               bttn2.buttonMode = true;

               bttn2.addEventListener(MouseEvent.MOUSE_DOWN, checkLogin);

     }

}

will both buttons executed when user press them?

Second question is if I want on scene 2, to run a function whenever scene 2 is loaded, how do I do that in package?

Thank you

This topic has been closed for replies.
Correct answer Amy Blankenship

You can have functions that run when only certain objects are on stage (which is equivalent to what you're asking). However, that requires advanced concepts you're probably not ready for.

You might want to consider instead having a MovieClip in scene 1 that has the functionality that you need to have happen in scene 1 and another MC that has different functionality, but the same method name, in scene 2.

So:

public interface IDoer {

     function doSomething():void;

}

public class Scene1Doer extends MovieClip implements IDoer {

     public function doSomething():void {

          //do whatever you need to do in scene 1

     }

}

public class Scene2Doer extends MovieClip implements IDoer {

     public function doSomething():void {

          //do whatever you need to do in scene 2

     }

}

Then, on your timeline, you would place both instances of library symbols that have Scene1Doer and Scene2Doer as their base Classes. Call both instances doerMovieClip.

Your main Document would look something like this:

public class MainDocument extends MovieClip {

     public var btn1:SimpleButton;

     public var btn2:SimpleButton;

     protected var _doerMovieClip:IDoer;

     public function MainDocument() {

          super();

          btn1.addEventListener(MouseEvent.CLICK, goToScene2);

          btn2.addEventListener(MouseEvent.CLICK, doSomething);

     }

     public function get doerMovieClip():IDoer{

          return _doerMovieClip;

     }

     public function set doerMovieClip(value:IDoer):void {
           _doerMovieClip = value;   
      }    
      //skipping goToScene2, not dealing with the issues involved in that in this post    
      protected function doSomething(e:MouseEvent):void {
           if (_doerMovieClip) {
               _doerMovieClip.doSomething();
           }
      }
}

Note that you may get warnings that your implementations of doerMovieClip are not the same. Chalk it up to the Flash team not expecting people to actually program to interfaces on the timeline and move on.

2 replies

Amy Blankenship
Amy BlankenshipCorrect answer
Legend
October 14, 2013

You can have functions that run when only certain objects are on stage (which is equivalent to what you're asking). However, that requires advanced concepts you're probably not ready for.

You might want to consider instead having a MovieClip in scene 1 that has the functionality that you need to have happen in scene 1 and another MC that has different functionality, but the same method name, in scene 2.

So:

public interface IDoer {

     function doSomething():void;

}

public class Scene1Doer extends MovieClip implements IDoer {

     public function doSomething():void {

          //do whatever you need to do in scene 1

     }

}

public class Scene2Doer extends MovieClip implements IDoer {

     public function doSomething():void {

          //do whatever you need to do in scene 2

     }

}

Then, on your timeline, you would place both instances of library symbols that have Scene1Doer and Scene2Doer as their base Classes. Call both instances doerMovieClip.

Your main Document would look something like this:

public class MainDocument extends MovieClip {

     public var btn1:SimpleButton;

     public var btn2:SimpleButton;

     protected var _doerMovieClip:IDoer;

     public function MainDocument() {

          super();

          btn1.addEventListener(MouseEvent.CLICK, goToScene2);

          btn2.addEventListener(MouseEvent.CLICK, doSomething);

     }

     public function get doerMovieClip():IDoer{

          return _doerMovieClip;

     }

     public function set doerMovieClip(value:IDoer):void {
           _doerMovieClip = value;   
      }    
      //skipping goToScene2, not dealing with the issues involved in that in this post    
      protected function doSomething(e:MouseEvent):void {
           if (_doerMovieClip) {
               _doerMovieClip.doSomething();
           }
      }
}

Note that you may get warnings that your implementations of doerMovieClip are not the same. Chalk it up to the Flash team not expecting people to actually program to interfaces on the timeline and move on.

Inspiring
October 15, 2013

hmmm

so I put a btn2 on scene2? and have user click on btn2 to activate doSomething?

Is it possible to do something like on scene1 when user click btn1, it will execute both gotoScene2 and doSomething, and then on scene2 the result of doSomething ( lets say doSomething will collect fname data from my database) will automatically appear in a input box?

If on scene1 btn1 executed doSomething, will the returned value still accessible on scene2, I think this is what my real confusion is, if I access to my database thru php on scene1, will the data still be usable on scene2?

Thanks a lot.

Amy Blankenship
Legend
October 15, 2013

No, the code you posted implied that both buttons "live" for the entire life of the swf. If they don't, just put each button in the MovieClip that you're using in each scene.

kglad
Community Expert
Community Expert
October 13, 2013

you shouldn't those buttons to the main timeline.  add them to a movieclip and create a class for that movieclip.  add the code for your buttons to that movieclip's class.  add that movieclip to your main timeline etc.

Inspiring
October 14, 2013

Ok, I get what you mean, load externally the buttons into main timeline and apply class for them on their own.

Thats good I can do that, but what about on main timeline I have multiple scenes, and for example I want on scene 2 once the scene is loaded, I want to load an mc into  'scene2mc', etc. Can I do that in the package? Or I'll have do it the old way have actionscript on scene2?

Is my question confusing I hope not? So basically in here

public class submit extends MovieClip {

        

        public function submit():void {


     }

}

There's no way I can make function that only runs on scene 2 but not scene 1? I hope I made it understanding

Thank you for reply

Inspiring
October 14, 2013

Sorry, let me rephrase the question...

on Scene 2 right when it starts, it should grabs 'fname' ( first name ) from mysql. and display it on the screen.

I have an input box on Scene 2 - 'fname2'

In my package I have this function

public function processCreate(showCondition):void {

            var urlVars:URLVariables = new URLVariables();

            

            var urlFileRequest:URLRequest = new URLRequest("php/controlpanel.php");

            

            urlFileRequest.method = URLRequestMethod.POST;

                        

            urlFileRequest.data = urlVars;

           

            var php2Loader:URLLoader = new URLLoader();

            php2Loader.dataFormat = URLLoaderDataFormat.VARIABLES;

            if (showCondition=="createprofile"){

                php2Loader.addEventListener(Event.COMPLETE, showResult); }

            if (showCondition=="retrieveData"){

                php2Loader.addEventListener(Event.COMPLETE, showResultS2); }

           

            urlVars.systemCall = showCondition;

            urlVars.email = email.text;

            urlVars.password = password.text;

            urlVars.fname = fname.text;

            urlVars.lname = lname.text;

           

            php2Loader.load(urlFileRequest);

           

           

       

        }

back to Scene 2, I have in AS

stop();

processCreate("retrieveData");  // so that it will run the function when scene2 is loaded.