6 Replies Latest reply on Oct 14, 2008 4:27 AM by fargond

    Movement with actionscript

    BobMelnyk
      Hi,

      I am using action script to do some simple movement of text. I created some text, made it a movie clip and gave it an instance name of q1. I put it on the stage and did a right click on it, and selected "actions." I added the following actionscript code for the movement:

      onClipEvent(enterFrame) {
      if (!cancelled) {
      speed = 4;
      this._y -= speed;
      if (this._y < 100) {
      this.cancelled=true;}
      }
      }

      This worked great and moved the text up the screen to a certain point and stopped. Just what I wanted. But now, when the text stops moving, I want the entire movie to go to frame 2. This seems like it should be easy to do, but I can not figure out how to recognize that the motion has stopped and to go to frame 2. I tried this,but it had no effect:

      onClipEvent(enterFrame) {
      if (!cancelled) {
      speed = 4;
      this._y -= speed;
      if (this._y < 100) {
      this.cancelled=true;}
      }
      gotoAndPlay(2);
      }

      Anyone know how to do this? Thanks!

      Bob
        • 1. Re: Movement with actionscript
          Rothrock Level 5
          Well it has an effect, just not the one you want! You've got a few things going wrong.

          First, you only want the entire clip to go to frame 2 when the text has stopped moving. So the statement should go inside the conditional if(this._y<100)){}. As it currently stands it looks like it is out side the outer condtional and that would mean that every frame it is going to and playing 2.

          But that brings us to our second problem. You aren't being very clear about what you want to go to frame 2. Since there is no movie clip instance there Flash will assume that you want the current object to move it's timeline to frame 2. In this case since the code is on your text movieclip -- and I'm guessing it doesn't have a frame 2 -- the code is actually trying to send the movie clip to frame 2. I'm guessing you want to send the text movieclip's _parent to frame 2? In that case the code would be:

          onClipEvent(enterFrame) {
          if (!cancelled) {
          speed = 4;
          this._y -= speed;
          if (this._y < 100) {
          this.cancelled=true;
          _parent.gotoAndPlay(2);
          }
          }
          }

          But there is still something that is bad about this. Since the clip still exists every frame is still going to have to check if cancelled is true or not. Not a problem if this is the only clip you have doing stuff, but it is still stloppy wasteful design. Instead....

          Don't put the code directly on the clip. Instead put it on a frame of the main timeline, by that I mean the timeline the movie is sitting on. Give your text movieclip and instance name like, myTextClip or some such and then -- and remember this is frame code:

          myTextClip.onEnterFrame=function(){
          this._y-=4;
          if(this._y<100){
          delete this.onEnterFrame;
          _parent.gotoAndPlay(2);
          }
          }

          That is better style.
          • 2. Re: Movement with actionscript
            BobMelnyk Level 1
            Hi,

            Thanks for the quick reply! That actually did not work. It moved the text fine, but when the text stopped, the movie did not go to frame 2. But, I replace this:
            _parent.gotoAndPlay(2);
            with this:
            gotoAndPlay(2);

            and it works great now! Thanks for your help!

            Bob
            • 3. Re: Movement with actionscript
              Rothrock Level 5
              Interesting I would have thought the scope at that point was the myTextClip. Live and learn.
              • 4. Re: Movement with actionscript
                Level 7
                >>Interesting I would have thought the scope at that point was the
                >>myTextClip. Live and learn.
                >>_parent.gotoAndPlay(2);

                It's because you didn't use this, as in: this._parent.gotoAndPlay(2);

                If you trace(_parent); you'll get undefined. If you trace(this._parent)
                you'll get _level0. When you don't specify the scope (this) and assign a
                function, the scope is where you're at... if you were 'in' the clip then
                _parent would be _root but since you're at _root when you assign the
                function, _parent alone is not possible. Best to always specify and use
                this.


                --
                Dave -
                www.offroadfire.com
                Head Developer
                http://www.blurredistinction.com
                Adobe Community Expert
                http://www.adobe.com/communities/experts/


                • 5. Re: Movement with actionscript
                  Rothrock Level 5
                  D'oh! Live and learn more. Thanks.
                  • 6. Re: Movement with actionscript
                    fargond Level 1
                    Thanks. This was very helpful.