6 Replies Latest reply on Apr 27, 2008 5:06 PM by _peabulls_

    Help loading an animation in a target MC

    sportoafc
      Hi there,

      I'm working on a bit of flash that has some actionscript in it the randomly generates different snowflakes that move upwards on the stage. Problem is I need them to load into a target mc rather than on the top level of the main timeline and I don't know how to amend the code.

      The code I'm using is:

      snowNumber = 60;
      var flakes:Array = new Array("flake1", "flake2", "flake3","flake4","flake5","flake6")
      for (i=0; i<snowNumber; i++) {
      newSnow = _root[flakes[random(flakes.length)]].duplicateMovieClip("snow"+i, i);
      newSnow._x = Math.random()*Stage.width;
      newSnow._y = Stage.height+Math.random()*300;
      newSnow.maxSnowSpeed = Math.random()*4;
      newSnow.wind = Math.random()*6;
      newSnow._xscale = newSnow._yscale = Math.random()*60+40
      newSnow.swapDepths(newSnow._xscale)
      prevSnow = newSnow;
      newSnow.onEnterFrame = function() {
      //this._rotation -= this.maxSnowSpeed+1;
      if(this._y<-50 || this._x>Stage.width+50 || this._x<-50){
      this._y = Stage.height+Math.random()*300;
      this._x = Math.random()*Stage.width;
      } else {
      this._y -= this.maxSnowSpeed+1;
      this._x += this.wind-4;
      }

      //this._alpha = this.alpha;
      };
      }

      Can anyone help?


      Jamie
        • 1. Re: Help loading an animation in a target MC
          kglad Adobe Community Professional & MVP
          what do you mean by, "..I need them to load into a target mc .."?

          what's "them"? the snowflakes? if so, what do you mean by loading the snowflakes into a movieclip?

          or it's probably better if you just explain what effect you're trying to achieve.
          • 2. Re: Help loading an animation in a target MC
            _peabulls_
            if i understand this question correctly .. your snowflakes are loading into _root (on top of everything) .. and you want them to load into a different MovieClip (so you can have them appear on top of some things and under other things)

            if this assumption is correct ..


            it should be noted that duplicateMovieClip() will always create the duplicate clip in the same timeline that the original was in .. so if the original snowflakes are on the _root timeline all of your duplicate snowflakes will also be on the _root timeline.

            2 options come to mind:

            both of these options require you to place a MovieClip (at author time) at the depth you would like the snowflakes to be constrained to .. for the sake of the following examples .. we will call this MovieClip ''snowTarget".

            1: instead of using duplicateMovieClip() .. you could try using attachMovie():

            attachMovie (id, name, depth) : MovieClip

            attachMovie() looks in the library for a symbol with the linkage identifier that is the same as 'id' .. calls the new movie 'name' .. and places it at the depth 'depth' .. it attaches it to the MovieClip from which you made the attachMovie() call .. in the case of our example your code might look like this:

            snowTarget.attachMovie ("flake1", "snow" + i, i);

            and you would need to have a clip in your library with the linkage identifier "flake1" .. if you do not know how to do this look in the flash help -> Learning ActionScript 2.0 in Adobe Flash -> Attaching a movie clip symbol to the Stage .. there is a pretty good example there


            2: for a possibly easier but messier solution you could just place the original snowflakes in snowTarget .. that way all of the duplicate snowflakes would also be in snowTarget


            let me know if these options don't fit your requirements and we'll see if we can't come up with something else .. also feel free to ask for clarification.
            • 3. Re: Help loading an animation in a target MC
              sportoafc Level 1
              Thanks very much for getting back to me and your presumptions are correct. I'm happy to go with the 2nd of your suggestions (the easiest!) but I'm afraid I'm having a bit of bother with that.

              I've created a target movie clip called snowTarget... stuck it on the main timeline, named the instance ' snowTarget ' and placed the snowflakes in it. I've also amended the line of code that read:

              newSnow = _root[flakes[random(flakes.length)]].duplicateMovieClip("snow"+i, i);

              to the following:
              newSnow = _root[flakes[random(flakes.length)]].snowTarget.duplicateMovieClip("snow"+i, i);

              ....and it's still not working. I'm unsure as to what I'm doing wrong? Have I missed something out?

              Jamie
              • 4. Re: Help loading an animation in a target MC
                kglad Adobe Community Professional & MVP
                place that code on snowTarget's timeline and replace

                _root

                with

                this
                • 5. Re: Help loading an animation in a target MC
                  _peabulls_ Level 1
                  ok .. lets take a look at your code .. first we will break it up into multiple statements to make it more readable


                  //-- the original code --//
                  newSnow = _root[flakes[random(flakes.length)]].duplicateMovieClip("snow"+i, i);

                  //-- can be stated --//
                  var flakeIndex :Number = random(flakes.length);
                  var flakeName :String = flakes[flakeIndex];
                  var flakeClip :MovieClip = _root[flakeName];

                  newSnow = flakeClip.duplicateMovieClip ("snow" + i, i);


                  this does what we might expect it to .. makes a duplicate of a snowflake located on _root.


                  //-- the updated code --//
                  newSnow = _root[flakes[random(flakes.length)]].snowTarget.duplicateMovieClip("snow"+i, i);

                  //-- can be stated --//
                  var flakeIndex :Number = random(flakes.length);
                  var flakeName :String = flakes[flakeIndex];
                  var flakeClip :MovieClip = _root[flakeName];

                  newSnow = flakeClip.snowTarget.duplicateMovieClip("snow"+i, i);


                  this code tries to duplicate the non-existant clip _root.flakeN.snowTarget


                  //-- you might rather try --//
                  var flakeIndex :Number = random(flakes.length);
                  var flakeName :String = flakes[flakeIndex];
                  var flakeClip :MovieClip = _root.snowTarget[flakeName];

                  newSnow = flakeClip.duplicateMovieClip("snow"+i, i);

                  this code makes a duplicate of a snowflake located in _root.snowTarget

                  //--- --- - - -

                  hope this helps
                  • 6. Re: Help loading an animation in a target MC
                    _peabulls_ Level 1
                    quote:

                    Originally posted by: kglad
                    place that code on snowTarget's timeline and replace _root with this


                    kglad's solution will also work .. keep in mind, however, that placing code in many locations throughout your .fla might make it more difficult to find/debug your code if you run into problems or need to add more functionality in the future.