Skip navigation
Currently Being Moderated

Timing in OOP programming

Jul 16, 2013 4:08 PM

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

 
Replies
  • kglad
    72,235 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 16, 2013 4:21 PM   in reply to Charine -FB buggoop

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

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 17, 2013 7:24 AM   in reply to Charine -FB buggoop

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 17, 2013 11:49 AM   in reply to Charine -FB buggoop

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 18, 2013 8:41 AM   in reply to Amy Blankenship

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 18, 2013 2:59 PM   in reply to sinious

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

     
    |
    Mark as:
  • kglad
    72,235 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 18, 2013 4:32 PM   in reply to Charine -FB buggoop

    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;

            }

        }

     

    }

     
    |
    Mark as:
  • kglad
    72,235 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 18, 2013 9:30 PM   in reply to Charine -FB buggoop

    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?

     
    |
    Mark as:
  • kglad
    72,235 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 19, 2013 7:35 AM   in reply to Charine -FB buggoop

    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.

     
    |
    Mark as:
  • kglad
    72,235 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 19, 2013 2:55 PM   in reply to Charine -FB buggoop

    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.

     
    |
    Mark as:
  • kglad
    72,235 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 19, 2013 3:59 PM   in reply to Charine -FB buggoop

    you're welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points