19 Replies Latest reply on Jul 23, 2007 12:19 PM by Newsgroup_User

    Moving objects

    Lucy4 Level 1
      Is it possible to have objects (a MovieClip containing a button) move on their own without anybody using their directional arrows ir mouse? I've only just started to build it so am happy to redo it if I've got anything wrong.
      thanks
      Lucy
      By the way I'm using Flash 8
        • 1. Re: Moving objects
          nonybd Level 1
          Where exactly do you want them to move? in a pattern? On occurances?
          • 2. Re: Moving objects
            Level 7
            Hi Lucy4,

            Is it ever! That's very rudimentary stuff in Flash...it's called
            ActionScript controlled motion. I'm sure you're familiar with motion on
            the timeline but the same motion can easily be mimicked using code. To
            accomplish this you will need to update the X/Y position of the
            MovieClip object either in an onEnterFrame loop or via an interval. I
            also have a robust API that creates processing threads...basically fancy
            versions of intervals...which makes it easy to control your loops.

            Regards,
            Patrick
            BNM

            Lucy4 wrote:
            > Is it possible to have objects (a MovieClip containing a button) move on their
            > own without anybody using their directional arrows ir mouse? I've only just
            > started to build it so am happy to redo it if I've got anything wrong.
            > thanks
            > Lucy
            > By the way I'm using Flash 8
            >
            • 3. Re: Moving objects
              Lucy4 Level 1
              I just want them to flot around the window smoothly. I've got three of them and want them to flot around their own third, bouncing away if they hit each other.
              thanks
              • 4. Re: Moving objects
                Level 7
                Lucy,

                > Is it possible to have objects (a MovieClip containing a button) move
                > on their own without anybody using their directional arrows ir mouse?

                Yes.

                > I've only just started to build it so am happy to redo it if I've got
                > anything
                > wrong.

                Well, what have you done so far? :)

                To animate an object on the Stage, you'll have to update its x and y
                coordinates repeatedly over time. If you look up the "MovieClip class"
                entry of the ActionScript 2.0 Language Reference, you'll see all the
                properties (characteristics), methods (things the object can do), and events
                (things the object can react to) of any MovieClip instance, which is to say
                any movie clip symbol. Give your movie clip an instance name (see the
                Property inspector), then update its MovieClip._x and MovieClip._y
                properties via that instance name. Doing this repeatedly causes animation.
                You might, for example, make use of the MovieClip.onEnterFrame event for
                this reason -- like this:

                myMovieClip.onEnterFrame = function():Void {
                this._x += 5;
                this._y += 2;
                }

                The above assigns a function to the MovieClip.onEnterFrame event of the
                myMovieClip instance (the instance name given to a movie clip symbol). This
                process is called event handling. As you can see the _x and _y properties
                of the same clip are updated by 5 and 2 pixels per iteration, respectively.
                You can use if() statements in there to make decisions, and delete the
                function if you feel like stopping the action (see the "delete" entry of the
                ActionScript 2.0 Language Reference).

                That should give you a start. :)


                David Stiller
                Co-author, Foundation Flash CS3 for Designers
                http://tinyurl.com/2k29mj
                "Luck is the residue of good design."


                • 5. Re: Moving objects
                  nonybd Level 1
                  Ooooo. That is going to be tough. I, personaly don't know enough to do that, so I can't really help in this. Sorry. (understood, you want them to bounce around the screen at random, and bounce if they hit the edge, or each other)
                  • 6. Re: Moving objects
                    Lucy4 Level 1
                    Oh well, thanks anyway
                    • 7. Re: Moving objects
                      Level 7
                      Hi Lucy,

                      That doesn't sound too complex. Are these square shapes or are the odd
                      shapes? Square(ish) shapes are much easier to work with.

                      Patrick

                      Lucy4 wrote:
                      > I just want them to flot around the window smoothly. I've got three of them and want them to flot around their own third, bouncing away if they hit each other.
                      > thanks
                      • 8. Re: Moving objects
                        nonybd Level 1
                        There is probably someone out there who can, though. HEY!!! ANYBODY OUT THERE? HELP NEEDED HERE! PEOPLE PEOPLE, THERE ARE 68 LOGGED IN USERS HERE.
                        • 10. Re: Moving objects
                          Lucy4 Level 1
                          I would have thought that check is true and unchecked is false, but I don't know for sure though
                          • 11. Re: Moving objects
                            Lucy4 Level 1
                            Thanks everybody I'll have a look at that and give it ago.
                            By the way the shapes are more oval shape than square.
                            Thanks
                            • 12. Re: Moving objects
                              Lucy4 Level 1
                              I read about MovieClip classes and added:

                              buttonONE.onEnterFrame = function() {
                              this._x += 5;
                              this._y += 2;
                              };

                              buttonTWO.onEnterFrame = function() {
                              this._x += 5;
                              this._y += 2;
                              };

                              buttonTHREE.onEnterFrame = function() {
                              this._x += 5;
                              this._y += 2;
                              };

                              to the timeline, but is it possible to get them to move more randomly?
                              • 13. Re: Moving objects
                                Level 7
                                Hi Lucy,

                                This should make it much more random...plus or minus 50 pixels. The
                                Math.random() function returns a random value between 0 and 1 so you
                                have to compensate negatively by 0.5 and then multiply by a much larger
                                number to notice movement.

                                buttonONE.onEnterFrame = function() {
                                var maxMoveValue=50;
                                this._x += (Math.random()-0.5)*maxMoveValue;
                                this._y +=(Math.random()-0.5)*maxMoveValue
                                };

                                Regards,
                                Patrick

                                Lucy4 wrote:
                                > I read about MovieClip classes and added:
                                >
                                > buttonONE.onEnterFrame = function() {
                                > this._x += 5;
                                > this._y += 2;
                                > };
                                >
                                > buttonTWO.onEnterFrame = function() {
                                > this._x += 5;
                                > this._y += 2;
                                > };
                                >
                                > buttonTHREE.onEnterFrame = function() {
                                > this._x += 5;
                                > this._y += 2;
                                > };
                                >
                                > to the timeline, but is it possible to get them to move more randomly?
                                >

                                --
                                http://www.baynewmedia.com
                                Faster, easier, better...ActionScript development taken to new heights.
                                Download the BNMAPI today. You'll wonder how you ever did without it!
                                Available for ActionScript 2.0/3.0.
                                • 14. Re: Moving objects
                                  Lucy4 Level 1
                                  Thanks for that but it doesn't move very smoothly. I'd like it to look like it's floating around the screen. Am I right in thinking that I just change the numbers? And then put:

                                  no (this._x= 550; this._y=550){
                                  }

                                  so that it doesn't go outside of the screen?
                                  • 15. Re: Moving objects
                                    Level 7
                                    > This should make it much more random...plus or minus 50 pixels.
                                    > The Math.random() function returns a random value between 0
                                    > and 1 so you have to compensate negatively by 0.5 and then
                                    > multiply by a much larger number to notice movement.

                                    Good stuff! Yes, this is one of dozens (heck, hundreds) of routes you
                                    might take. My example was about as basic as it gets, and I did that to
                                    introduce the idea of the mechanics involved. Patrick's improvement is even
                                    better, and the ultimate result depends on your aptitude for geometry and
                                    trig.

                                    You may want to pick up a bopy of Keith Peters' "Foundation Actionscript
                                    Animation: Making Things Move!" (friends of ED). He goes into all sorts of
                                    useful techniques for what you're after. There's also an ActionScript 3.0
                                    newer edition, but in either version of ActionScript, the book really is a
                                    bucketful of knowledge.


                                    David Stiller
                                    Adobe Community Expert
                                    Dev blog, http://www.quip.net/blog/
                                    "Luck is the residue of good design."


                                    • 16. Re: Moving objects
                                      Level 7
                                      Hi Lucy4,

                                      First of all, reduce the random number multiplier and increase your
                                      frame rate. This will make the animations much smoother. To ensure that
                                      they don't go outside the screen, do something like this:

                                      var rightLimit:Number=Stage.width;
                                      var bottomLimit:Number=Stage.height;
                                      if (this._x<=0) { this._x=0;}
                                      if ((this._x+this._width)>=rightLimit) { this._x=rightLimit-this._width;}
                                      if (this._y<=0) { this._y=0;}
                                      if ((this._y+this._height)>=bottomLimit) {
                                      this._y=bottomLimit-this._height;}

                                      This assumes that you don't want the images going off at all. If you
                                      don't mind the edges of the images going off the right/bottom edges of
                                      the stage, you can add some value to the rightLimit and bottomLimit
                                      values. For example, if you're okay with have up to 50 pixels of the
                                      image disappearing of the edges, you would have:

                                      var rightLimit:Number=Stage.width+50;
                                      var bottomLimit:Number=Stage.height+50;

                                      This also assumes you're aligning the whole show to the stage. If
                                      you're aligning to other elements, this won't necessarily work.

                                      Patrick

                                      Lucy4 wrote:
                                      > Thanks for that but it doesn't move very smoothly. I'd like it to look like
                                      > it's floating around the screen. Am I right in thinking that I just change the
                                      > numbers? And then put:
                                      >
                                      > no (this._x= 550; this._y=550){
                                      > }
                                      >
                                      > so that it doesn't go outside of the screen?
                                      >

                                      --
                                      http://www.baynewmedia.com
                                      Faster, easier, better...ActionScript development taken to new heights.
                                      Download the BNMAPI today. You'll wonder how you ever did without it!
                                      Available for ActionScript 2.0/3.0.
                                      • 17. Moving objects
                                        Lucy4 Level 1
                                        I've added the code, but it still doesn't move smoothly and just float around the screen. I added:

                                        var rightLimit:Number=Stage.width;
                                        var bottomLimit:Number=Stage.height;
                                        if (this._x<=0) { this._x=0;}
                                        if ((this._x+this._width)>=rightLimit) { this._x=rightLimit-this._width;}
                                        if (this._y<=0) { this._y=0;}
                                        if ((this._y+this._height)>=bottomLimit) {
                                        this._y=bottomLimit-this._height;}

                                        to the actual movieclip, but it gave my a page full of errors:
                                        **Error** Symbol=Menu, layer=MenuMCs, frame=1:Line 1: Statement must appear within on/onClipEvent handler
                                        var rightLimit:Number=Stage.width;

                                        **Error** Symbol=Menu, layer=MenuMCs, frame=1:Line 2: Statement must appear within on/onClipEvent handler
                                        var bottomLimit:Number=Stage.height;

                                        **Error** Symbol=Menu, layer=MenuMCs, frame=1:Line 3: Statement must appear within on/onClipEvent handler
                                        if (this._x<=0) { this._x=0;}

                                        **Error** Symbol=Menu, layer=MenuMCs, frame=1:Line 4: Statement must appear within on/onClipEvent handler
                                        if ((this._x+this._width)>=rightLimit) { this._x=rightLimit-this._width;}

                                        **Error** Symbol=Menu, layer=MenuMCs, frame=1:Line 5: Statement must appear within on/onClipEvent handler
                                        if (this._y<=0) { this._y=0;}

                                        **Error** Symbol=Menu, layer=MenuMCs, frame=1:Line 6: Statement must appear within on/onClipEvent handler
                                        if ((this._y+this._height)>=bottomLimit) {

                                        Total ActionScript Errors: 6 Reported Errors: 6
                                        is repeated 12 times then:
                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 1: Statement must appear within on/onClipEvent handler
                                        var rightLimit:Number=Stage.width;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 2: Statement must appear within on/onClipEvent handler
                                        var bottomLimit:Number=Stage.height;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 3: Statement must appear within on/onClipEvent handler
                                        if (this._x<=0) { this._x=0;}

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 4: Statement must appear within on/onClipEvent handler
                                        if ((this._x+this._width)>=rightLimit) { this._x=rightLimit-this._width;}

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 5: Statement must appear within on/onClipEvent handler
                                        if (this._y<=0) { this._y=0;}

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 6: Statement must appear within on/onClipEvent handler
                                        if ((this._y+this._height)>=bottomLimit) {

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 1: Statement must appear within on/onClipEvent handler
                                        var rightLimit:Number=Stage.width;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 2: Statement must appear within on/onClipEvent handler
                                        var bottomLimit:Number=Stage.height;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 3: Statement must appear within on/onClipEvent handler
                                        if (this._x<=0) { this._x=0;}

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 4: Statement must appear within on/onClipEvent handler
                                        if ((this._x+this._width)>=rightLimit) { this._x=rightLimit-this._width;}

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 5: Statement must appear within on/onClipEvent handler
                                        if (this._y<=0) { this._y=0;}

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 6: Statement must appear within on/onClipEvent handler
                                        if ((this._y+this._height)>=bottomLimit) {

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 1: Statement must appear within on/onClipEvent handler
                                        var rightLimit:Number=Stage.width;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 2: Statement must appear within on/onClipEvent handler
                                        var bottomLimit:Number=Stage.height;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 3: Statement must appear within on/onClipEvent handler
                                        if (this._x<=0) { this._x=0;}

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 4: Statement must appear within on/onClipEvent handler
                                        if ((this._x+this._width)>=rightLimit) { this._x=rightLimit-this._width;}

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 5: Statement must appear within on/onClipEvent handler
                                        if (this._y<=0) { this._y=0;}

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 6: Statement must appear within on/onClipEvent handler
                                        if ((this._y+this._height)>=bottomLimit) {

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 1: Statement must appear within on/onClipEvent handler
                                        var rightLimit:Number=Stage.width;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 2: Statement must appear within on/onClipEvent handler
                                        var bottomLimit:Number=Stage.height;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 4: '(' expected
                                        if (this._x<=0) { this._x=0;}

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 8: Syntax error.
                                        this._y=bottomLimit-this._height;}

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 1: Statement must appear within on/onClipEvent handler
                                        var rightLimit:Number=Stage.width;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 2: Statement must appear within on/onClipEvent handler
                                        var bottomLimit:Number=Stage.height;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 3: '(' expected
                                        onClipEvent handler{

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 8: Syntax error.
                                        this._y=bottomLimit-this._height;}

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 1: Statement must appear within on/onClipEvent handler
                                        var rightLimit:Number=Stage.width;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 2: Statement must appear within on/onClipEvent handler
                                        var bottomLimit:Number=Stage.height;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 3: '(' expected
                                        onClipEvent {

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 8: Syntax error.
                                        this._y=bottomLimit-this._height;}

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 1: Statement must appear within on/onClipEvent handler
                                        var rightLimit:Number=Stage.width;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 2: Statement must appear within on/onClipEvent handler
                                        var bottomLimit:Number=Stage.height;

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 3: '(' expected
                                        onClipEvent {

                                        **Error** Scene=Scene 1, layer=MenuMCs, frame=1:Line 8: Syntax error.
                                        this._y=bottomLimit-this._height;}

                                        **Error** Symbol=Menu, layer=MenuMCs, frame=1:Line 1: Statement must appear within on/onClipEvent handler
                                        var rightLimit:Number=Stage.width;

                                        **Error** Symbol=Menu, layer=MenuMCs, frame=1:Line 2: Statement must appear within on/onClipEvent handler
                                        var bottomLimit:Number=Stage.height;

                                        **Error** Symbol=Menu, layer=MenuMCs, frame=1:Line 3: Statement must appear within on/onClipEvent handler
                                        if (this._x<=0) { this._x=0;}

                                        **Error** Symbol=Menu, layer=MenuMCs, frame=1:Line 4: Statement must appear within on/onClipEvent handler
                                        if ((this._x+this._width)>=rightLimit) { this._x=rightLimit-this._width;}

                                        **Error** Symbol=Menu, layer=MenuMCs, frame=1:Line 5: Statement must appear within on/onClipEvent handler
                                        if (this._y<=0) { this._y=0;}

                                        **Error** Symbol=Menu, layer=MenuMCs, frame=1:Line 6: Statement must appear within on/onClipEvent handler
                                        if ((this._y+this._height)>=bottomLimit) {
                                        is at the bottom.

                                        I don't know if it will help, but I've uploaded the swf file and that is at http://www.lucyohara.talk.net/menu.swf
                                        Thanks
                                        Lucy
                                        • 18. Moving objects
                                          Lucy4 Level 1
                                          • 19. Re: Moving objects
                                            Level 7
                                            Lucy4,

                                            > ... gave my a page full of errors:
                                            > **Error** Symbol=Menu, layer=MenuMCs, frame=1:Line 1: Statement must
                                            > appear
                                            > within on/onClipEvent handler
                                            > var rightLimit:Number=Stage.width;

                                            Any time you see the "must appear within on/onClipEvent handler" error,
                                            it means you've typed or pasted code directly to an object (rather than to a
                                            frame) without using either the on() or onClipEvent() event handler
                                            function. If your objects have instance names, and if you intend to use
                                            those instance names, you'll want to forego on() and onClipEvent()
                                            altogether and put your code into a frame.

                                            Here's a bit more info on the on() and onClipEvent() functions in
                                            general.

                                            http://www.quip.net/blog/2006/flash/museum-pieces-on-and-onclipevent

                                            In this case, just select the object desired, open the Actions panel,
                                            cut, then paste into a keyframe that holds that object -- ideally into a
                                            layer dedicated just to scripts.

                                            The sorts of motion you're talking about are going to be determined by
                                            the math you use in your various functions. Aimless wandering can be
                                            achieved by something called Brownian motion (search the Net for that term
                                            and ActionScript), and that's one of many, many approaches you might take.


                                            David Stiller
                                            Adobe Community Expert
                                            Dev blog, http://www.quip.net/blog/
                                            "Luck is the residue of good design."