9 Replies Latest reply on Aug 2, 2006 6:07 AM by Rothrock

    Moving an image with actionscript

    ward-ie Level 1
      I'm trying to move an image from position Y:0 to Y:-100 using actionscript when the .swf file loads, as this is smoother than conventional tweening??
        • 1. Re: Moving an image with actionscript
          Sim-Enzo
          I'm assuming you mean you want to animate its motion, right?

          You could do it by having the image be a MovieClip and use code something like this:

          onEnterFrame = function()
          {
          if(image._y > -100)
          image._y -= 10;
          }
          • 2. Re: Moving an image with actionscript
            Rothrock Level 5
            Well Sim-Enzo is almost correct. You will have some problems though because it is not really complete.

            When you want to do things over time (move, fade, scale, etc.) with actionscript you have two main choices – onEnterFrame and setInterval. For the example given by Sim-Enzo I would rewrite it like this:

            myImageClip.onEnterFrlame=function(){
            if(this._y>-100){
            this._y-=10;
            }else{
            delete this.onEnterFrame;
            this._y=-100
            }
            }

            Here are why I would make these changes.

            First the onEnterFrame is poorly named and confuses a lot of folks. So remember onEnterFrame means "do this next bit of code at the frame rate of the movie – even if the timeline is stopped or there is only one frame."

            Next. Each movie clip instance can only have one onEnterFrame handler defined for it. So it is a good idea to assign each one to an appropriate clip to avoid problems. By attaching this one to the clip you want to move, myImageClip, you will also be able to assign other ones to other clips and do whatever they need to do.

            Then inside the event handler, notice how I refer to the clip by "this" since the onEnterFrame is scoped to the myImageClip.

            Finally, and this is probably most important, I have added an else to the conditional. This makes sure that once your clip has move to where it is supposed to go that the onEnterFrame is stopped. Otherwise the onEnterFrame would just keep going – it wouldn't move the clip anymore, but it would be using up processor time. One errant onEnterFrame like this wouldn't probably bring down your swf, but if you didn't do it with many different ones you would notice.

            I also added a little bit there which put the clip exactly at the place I wanted. There are times in Flash where rounding errors or other issues can make for surprises. I think it is good practice to set the value to the exact required value.
            • 3. Re: Moving an image with actionscript
              ward-ie Level 1
              Wicked Rothrock, works a treat!!
              Just one more thing how would I make this loop??
              • 4. Re: Moving an image with actionscript
                Rothrock Level 5
                Make it loop? Do you mean jump it back to where it started and then move off to -100 again? In that case, don't delete the onEnterFrame and instead of setting it to this._y=-100, set it to something like this._y=20 or whatever the starting point is.
                • 5. Re: Moving an image with actionscript
                  ward-ie Level 1
                  OK I've changed the _y=20 and it brings back the image to the starting point but doesn't move again, so it runs through the cycle once then stops?
                  • 6. Re: Moving an image with actionscript
                    ward-ie Level 1
                    OK sorted it, just took out 'delete' before this.onEnterFrame; so it looks like this:

                    myImageClip.onEnterFrame=function(){
                    if(this._y>-100){
                    this._y-=10;
                    }else{
                    this.onEnterFrame;
                    this._y=-100
                    }
                    }
                    • 7. Re: Moving an image with actionscript
                      Patrick110007
                      Hello,I'm a new comer,I have a problem of flash,ohw can help me?
                      My dad wanted me to make a flash game to study geography.I got it after his long explanation.That is a game like this:When you click the start button, one country's name will appear in a red frame,and if you click on right position in the world map,a dialog box appears,the following words will appear in the box at random :"yes""perfect""well done""you are right".If the wrong position, the dialog shows:"sorry""no""wrong""think it over" at random.And there are two bottons in the box.One is "continue" the other is "exit".
                      I made one sample of that.But there are two problems:the first is I can't make the names appear in the red frame at random.The second is I don't know how to make the dialog box properly.
                      You can get my work here.
                      http://iask.sina.com.cn/browse/download.php?path=/22/57/65/1222225765.1415530304.fla&filen ame=map%20game.fla
                      • 8. Re: Moving an image with actionscript
                        Patrick110007 Level 1
                        My e-mail:zqy110007@sina.com
                        • 9. Re: Moving an image with actionscript
                          Rothrock Level 5
                          Patrick, two things: Don't hijack other people's threads. What you are asking has nothing to do with the original post. So start a new thread with your question and a topic title about it. You will get more help that way.

                          Second, people don't generally respond via e-mail. The point of the forums is that others can be helped by reading through the various posts and seeing what goes on.

                          As for ward-le – You should actually remove everything that was on that line. "this.onEnterFrame" doesn't mean anything just by itself. I'm surprised it didn't generate an error message. But yes that line was the reason that it only went through once. Basically the code said, "When the _y is less than -100, delete this repeating thing (the onEnterFrame) and reposition it at a _y of 20." When you removed the delete part it didn't delete the onEnterFrame and it will just keep going and going and going…