15 Replies Latest reply on Jul 19, 2013 3:59 PM by kglad

    Timing in OOP programming

    Charine -FB buggoop

      Hi,

       

      I need to get the order in which stuff happens in my first game right. Here is what I want to do.

       

      1. Player clicks play button, button goes away and timer starts. //This is all working fine
      2. Next I want a text box to animate for 60frames. //This works fine
      3. ONLY then I want my next movie clips to start playing

       

      How do I make step 3 wait for that 60 frames? I have no idea of how to code this.

       

      Please help

      Charine

        • 1. Re: Timing in OOP programming
          kglad Adobe Community Professional & MVP

          use a timer instance or an enterframe loop to check when the timeline that contains your textfield plays those 60 frames.

          1 person found this helpful
          • 2. Re: Timing in OOP programming
            sinious Most Valuable Participant

            You can use simple timeline scripts to talk to other timelines. Adding instance names will assure you use the right display list object as well. An example of playing 60 frames, then asking another clip to play, then at 120 the second clip setting the first clip back to frame 1:

             

            Example Source (saved down to CS5)

             

            There's many other ways to do this but this is pretty simple. Just go in the 2 animation clips and look at the scripts on the timeline.

            1 person found this helpful
            • 3. Re: Timing in OOP programming
              Amy Blankenship Level 4

              The correct way to have this happen, whether you're using the timeline or not, is to generate an event in the child View when you want something to happen, then catching it at a higher level. You're already doing this with your MouseEvent.CLICK handler, whether you've thought about it or not.

               

              There are several ways that you can generate this event.

              1. The first, and most obvious, is to just use a frame script added in the normal way (dispatchEvent(new Event(MyEventKind.DO_SOMETHING, true))).
              2. If you need to do this a lot, you'll probably want to create a Class that you can just apply this behavior to with addFrameScript. I have a Class called AutoStopWithEvent that will dispatch an event with the type property set to the instance name and stop() the movie at the last frame.
              3. You can also create a custom Class with the same idea as above that uses a Timer or polls the state of the MC on ENTER_FRAME. Note that I tend to avoid ENTER_FRAME because it can be a source of memory leaks.

               

              At your main Document Class (or some other convenient point), you can do something like:

               

              function DocumentClass() {

                   super();

                   addEventListener(MyEventKind.DO_SOMETHING, startAction);

              }

               

              protected function startAction(e:Event):void {

                   goToAndStop('GetThisPartyStarted');//or whatever

              }

              The nice thing about this approach is that you can generate this same event other ways and still handle it the same way. So, for instance, if you suddenly want to start the action when an audio file stops, you watch the SoundChannel for COMPLETE and dispatch a DO_SOMETHING event on the display list. Conversely, if you want to change what happens in response to a DO_SOMETHING, it doesn't affect your animation MC even a little bit.

              1 person found this helpful
              • 4. Re: Timing in OOP programming
                sinious Most Valuable Participant

                For average timeline users (designers, etc), the power of Flash is not needing to know the most proficient scale-friendly way to code, or patterns, or practices, etc. While I use the event model (and frankly never a timeline), I think it's hard to grasp and overkill for someone with this type of question. Asking for a code example of how to trigger something after frame 60 isn't really a request for pragmatic pattern friendly scalable object oriented programming 101. More like, whatever is easiest. OP can decide.

                • 5. Re: Timing in OOP programming
                  Amy Blankenship Level 4

                  Did you read the title of the question? They're not looking for a "just hack it any old how" solution.

                  • 6. Re: Timing in OOP programming
                    Charine -FB buggoop Level 1

                    Hi kglad,

                     

                    I built a new timer with an event listener to trigger the appearance of my text box after the play button has been pushed.

                     

                    Once the first event has played I will make the next one follow. But I have a problem with the first even.

                     

                    My swf runs, but without the text box. It gives me the following error message the second time round:

                     

                     

                              Word Timer Started

                              TypeError: Error #1009: Cannot access a property or method of a null object reference.

                                  at BugGoopFSGame/placeMyWordBox()

                                  at flash.utils::Timer/_timerDispatch()

                                  at flash.utils::Timer/tick()

                              Word Timer Finished

                     

                     

                     

                    1. My main .as file has the timers. The one not working call a function for wordArray.placeMyWordBox();
                    2. My WordArray .as file is for the word array in which the loop and all the logics will work. In it it has this function

                    public function placeMyWordBox():void

                            {

                                addChild(wordBox);

                            }

                      3.My WordBox .as class file is the one in which the text box with TLF text lives within a movie clip (embedded text)

                     

                    I checked for spelling mistakes and syntex errors - can't find anything. How do I fix this?

                     

                    Thanks

                    Charine

                     

                    Here are my files:

                     

                    1. Main .as file with three timers. Two work (game and tile timers) and the word timer give above error

                     

                     

                     

                    package

                    {

                        import flash.display.*;

                        import flash.utils.Timer;

                        import flash.events.*;

                        import flash.events.MouseEvent;

                        import flash.events.Event;

                        import flash.text.*;

                        import flash.//others not relevant to this

                     

                     

                        public class BugGoopFSGame extends MovieClip

                        {

                            public var *//lots not relevant,

                            public var gameTimer:Timer;

                            public var wordTimer:Timer;

                            public var tileTimer:Timer;

                     

                            public var myplaybtn:Play;

                            public var wordBox:WordBox;

                     

                            public var wordArray:WordArray;

                     

                            public function BugGoopFSGame()

                            {

                     

                                 // lots of children not relevant

                     

                                myplaybtn = new Play();

                                addChild(myplaybtn);

                     

                                gameTimer = new Timer(31,5120);

                                wordTimer = new Timer(1000,1); //Just once for 60 flames, because I want game timer to follow this later.

                                tileTimer = new Timer(500,384);

                     

                                //Add event listener for start button

                                myplaybtn.addEventListener(MouseEvent.CLICK, startTimer);

                     

                                //Add event listener for Game timer WORKS FINE;

                                gameTimer.addEventListener(TimerEvent.TIMER, tickTock);

                                gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerFinished);

                                gameTimer.addEventListener(TimerEvent.TIMER, scaleLifeBar);

                     

                     

                                // Add event listener for tileTimer functions WORKS FINE

                                tileTimer.addEventListener(TimerEvent.TIMER, runTile);

                                tileTimer.addEventListener(TimerEvent.TIMER_COMPLETE, tileTimerFinished);

                                tileTimer.addEventListener(TimerEvent.TIMER, gameLoop);

                     

                                //Add event listener for word Timer functions - problem here

                                wordTimer.addEventListener(TimerEvent.TIMER, wordTimerStarted);

                                wordTimer.addEventListener(TimerEvent.TIMER_COMPLETE, wordTimerFinished);

                                wordTimer.addEventListener(TimerEvent.TIMER, placeMyWordBox);// This does not run in swf

                     

                                }

                     

                            //Start timer function

                            public function startTimer(timerEvent:MouseEvent):void

                            {

                               gameTimer.start();

                                trace("Timer started");

                                myplaybtn.visible = false;

                     

                                tileTimer.start();

                                trace("TileTimer started");

                     

                                wordTimer.start();

                                trace("Word Timer Started");

                            }

                     

                           public function tickTock(timerEvent:TimerEvent):void

                            {

                                trace("Tick");

                            }

                     

                            public function timerFinished(timerEvent:TimerEvent):void

                            {

                                trace("Timer is finished");

                            }

                     

                            public function runTile(timerEvent:TimerEvent):void

                            {

                                trace("Run Tile, RUN!")

                                }

                     

                                public function tileTimerFinished(timerEvent:TimerEvent):void

                            {

                                trace("Tile Timer is finished");

                            }

                     

                            //start wordArray

                     

                            public function wordTimerStarted(timerEvent:TimerEvent):void

                            {

                                trace("Word Timer Started");

                            }

                     

                            public function wordTimerFinished(timerEvent:TimerEvent):void

                            {

                                trace("Word Timer Finished");

                            }

                     

                            public function placeMyWordBox(timerEvent:TimerEvent):void //box never appears in swf

                            {

                                wordArray.placeMyWordBox();

                     

                                trace("WordArray added");

                            }

                     

                          

                     

                            //Start gameLoop

                     

                            public function gameLoop(timerEvent:TimerEvent):void

                            {

                                        letterArray.gameLoop();

                            }

                     

                        }

                    }

                     

                     

                    2.  .as file in which the loop and all the logics will work

                     

                     

                    package

                    {

                        import flash.display.MovieClip;

                     

                        public class WordArray extends MovieClip

                        {

                            public var wordBox:WordBox = new WordBox();

                            //private var  wordsL1:Array = ("a","has","of","off","on","not","got","in","is","it");

                     

                            // ***constructor code

                            public function WordArray()

                            {

                     

                                trace("Word Array is working");

                            }

                     

                            public function placeMyWordBox():void

                            {

                                addChild(wordBox);

                            }

                        }

                    }

                     

                    3..as class file in which the text box with TLF text lives within a movie clip (embedded text)

                     

                     

                    package 

                     

                    {

                        import flash.display.MovieClip;

                        import flash.text.*;

                       

                        public class WordBox extends MovieClip

                        {

                            public var wordBox = new WordBox();

                            // constructor code

                            public function WordBox()

                            {

                                x = 305;

                                y = 326;

                            }

                        }

                       

                    }

                    • 7. Re: Timing in OOP programming
                      kglad Adobe Community Professional & MVP

                      you have a major problem with your WordBox class.  the fact that you're not seeing a major problem leads me to believe that you are other class files are not using the WordBox class you showed.

                       

                      i'm not sure how that's occuring.  but, if you were using WordBox i would expect an endless loop error or a Flash crash because:

                       

                      package 

                       

                      {

                          import flash.display.MovieClip;

                          import flash.text.*;

                       

                          public class WordBox extends MovieClip

                          {

                              public var wordBox = new WordBox();  //<-this line creates a new WordBox instance, wordBox.  that new WordBox instance invokes the code in another WordBox class file ad infinitim.

                              // constructor code

                              public function WordBox()

                              {

                                  x = 305;

                                  y = 326;

                              }

                          }

                       

                      }

                       

                      // to remedy, use the following AND make sure there's a library movieclip with class WordBox.

                       

                      package 

                       

                      {

                          import flash.display.MovieClip;

                          import flash.text.*;

                       

                          public class WordBox extends MovieClip

                          {

                              public function WordBox()

                              {

                                  this.x = 305;

                                  this.y = 326;

                              }

                          }

                       

                      }

                      1 person found this helpful
                      • 8. Re: Timing in OOP programming
                        Charine -FB buggoop Level 1

                        Hi kglad, thanks for you quick responce.

                         

                        It is a good thing then that I have not yet added anything to the wordbox.

                         

                        My question then - Where and how do I place this wordbox correctly?

                         

                        This is what I'm aiming for:

                        Ok so I want the word box to be populated with words from an array. The word will be sliced into letters so that they can be tested if equal to tilesMC if tiles aftre clicked on.

                         

                        The reason I made the word box like this is because it has to appear at the start of the game scaled bigger and then move to the bottom of the screen to sit there until the player (5+ yr old kids) has copied it by clicking on the tiles. (Motion tween with embedded font)

                         

                        I'm afraid this is my first attempt at OOP and with very limited coding experience. I basically write pseudo code and the learn how to do it properly. At the end I'll be walking proof that flash and .as is do-able for dummies!

                         

                        So for this part of the game my pseudo code looks like this:

                         

                        1. Make a text box to populate (tripped up here as you pointed out)
                        2. Make arrays of words for all my levels
                        3. Take level 1's array of words and push first word into a child array (active word array)
                        4. Display this word in text box from point 1.
                        5. Remove pushed word from array
                        6. Slice the active word array into letters, push 1st letter into new grand child array  ()
                        7. if child clicks on a letter tile MC, test if letter tile string == letter in grand child array
                        8. If tile letter == word letter, remove from child array and push next letter to grand child array.
                        9. also play movie to let child know he was correct and give pionts
                        10. If tile letter!== word letter, play movie and wait for 8 to happen
                        11. If all the letters of the word had been matched, push new word from level 1 word array
                        • 9. Re: Timing in OOP programming
                          kglad Adobe Community Professional & MVP

                          what's the game?  a word appears and moves to stage bottom and then the player clicks on some tiles to spell the word displayed at stage bottom?

                          • 10. Re: Timing in OOP programming
                            Charine -FB buggoop Level 1

                            Hi kglad,

                             


                            Yes exactly. Here's a screenshot.

                            Yellow word appears in middle, move and sits at the bottom.

                            Letter tiles falls from the log

                            Player clicks on tiles to match the yellow word at the bottm

                             

                            Really appreciate your help. This forum has led my this for with zero experience.

                             

                            BuggoopT1280X800Rev7.jpg

                            • 11. Re: Timing in OOP programming
                              kglad Adobe Community Professional & MVP

                              i would create a movieclip in flash cc and add a textfield (eg, tf) with yellow font (to display 'your word') and assign a class (eg, WordBox).  i would use something like:

                               

                              package{

                               

                              import flash.display.MovieClip;

                              import flash.text.TextField;

                              import com.greensock.TweenLite;  //<-i like to use this for tweens

                               

                              public class WordBox extends MovieClip{

                               

                              public function WordBox(s:String){

                              this.tf.text=s;

                              this.tf.autoSize="left";

                              this.addEventListener(Event.ADDED_TO_STAGE,init);

                              }

                              private function init(e:Event):void{

                              // position this and start its tween if that's what you want

                              this.x=(stage.stageWidth-this.width)/2;

                              this.y = 0;

                              TweenLite.to(this,1,{y:stage.stageHeight-this.height});

                              }

                              public function textF():String{

                              return this.tf.text;

                              }

                               

                              }

                              }

                               

                              // i would also create a movieclip in flash cc with a textfield (eg, tf) for your tiles and create a tile class (eg, Tile) that will handle the tile display, movement and click:

                               

                              package{

                              import...

                              public class Tile extends MovieClip{

                               

                              public function Tile(s:String){

                              this.tf.text=s;

                              this.addEventListener(Event.ADD_TO_STAGE,init);

                              this.addEventListener(MouseEvent.MOUSE_DOWN,downF);

                              }

                              private function init(e:Event):void{

                              // position and assign an sinusoidal vertical motion

                              }

                              private function downF(e:MouseEvent):void{

                              dispatchEvent(new Event("tileclickE"));

                              }

                              pubic function textF():String{

                              return this.tf.text;

                              }

                               

                              }

                              }

                               

                              // and then in your controller class you can retrieve the WordBox text, split into an array and, when a tileclickE event is detected, determine if that was a correct tile click or incorrect tile click and take appropriate action.

                              • 12. Re: Timing in OOP programming
                                Charine -FB buggoop Level 1

                                Hi kglad,

                                 

                                Thank you so much for the suggestions. I'm going to use it as is, but I need to ensure that I understand everything.

                                 

                                My first question:The text box in the movie clip that I created in Flash cc

                                1. Must I give it the instance name of eg. tf?
                                2. Must I specify input text?

                                 

                                Thanks Charine

                                • 13. Re: Timing in OOP programming
                                  kglad Adobe Community Professional & MVP

                                  the textfield must have a name if you're going to use actionscript to reference it.  the textfield should be input if you want user's to enter text into the textfield.

                                   

                                  from my understanding, that textfield should be dynamic because you'll want to assign text to it during run-time and, should not be input type because you won't have user's enter text into it.

                                  • 14. Re: Timing in OOP programming
                                    Charine -FB buggoop Level 1

                                    Ok thanks kglad,

                                     

                                    I gave it an instance name and exported the .as file. I'll also change the text to dynamic bercause I definately want to assign text during run time.

                                     

                                    Next I'm going to change to code to match yours.

                                    Charine

                                    • 15. Re: Timing in OOP programming
                                      kglad Adobe Community Professional & MVP

                                      you're welcome.