16 Replies Latest reply on Dec 8, 2008 12:17 PM by Ned Murphy

    attach movieclip problems

    shintashi Level 1
      lets say I have a johnny_appleseed_mc and I make some seeds that over a series of events will grow into apple trees.

      the seed_mc is located in my library, and I am loading it like this:


      if(Key.isDown(Key.SPACE)){
      this.createEmptyMovieClip("holder" , "100");
      holder.attachMovie("seed_mc", "tree_mc", holder.getNextHighestDepth(), {_x:johnny_appleseed_mc._x, _y:johnny_appleseed_mc._y -50});
      holder.x=johnny_appleseed_mc._x;
      holder.y=johnny_appleseed_mc._y;
      }


      when i movie the johnny MC, the seed_mc doesn't follow (good), but when I plant again (another seed) the original seed_mc in the first position disappears and reappears in the new position of johnny, as if the first seed was never planted. How do I make the old seeds stick in place, and still plant new seeds?
      (edited for clarity)
        • 1. Re: attach movieclip problems
          kglad Adobe Community Professional & MVP
          try giving them distinct names.
          • 2. attach movieclip problems
            clbeech Level 3
            you need two things here:

            a) the second parameter in the createEmptyMovieClip() should not be in quotes. this is most likely the cause of the new seed 'replacing' the old one.

            b) in order for the seed to update to johnny's position as he is moved you need to continuously 'move' the position, like within an onEnterFrame event or within the operation of johnny's movement (eg. when johnny moves the seed moves as well) however in that case (the latter) you will also need to know 'which' seed to move - so you'll have to store a reference to the current seed.

            EDIT: LMAO! kg you beat me every time :)
            • 3. Re: attach movieclip problems
              kglad Adobe Community Professional & MVP
              i didn't even check the createEmptyMovieClip(), but i think the bigger problem is not the quotes (flash will often convert strings to numbers when needed - like in this situation). the bigger problem is all the holder movieclips have the same name and depth.

              you need to change that, too.
              • 4. Re: attach movieclip problems
                clbeech Level 3
                that's just what i was thinking at first too kg (in fact wrote a whole paragraph on using a var there) - but then was thinking that - shouldn't the getNextHighestDepth() take care of that?

                OH! i see it - he's creating a new "holder" clip every time and it's overwriting the previous one.
                • 5. Re: attach movieclip problems
                  shintashi Level 1
                  ok, now I'm confused...

                  to reply 1.
                  how am I going to give potentially 10,000+ seeds individual names?


                  to reply 2.
                  a. I tried it without quotes, and nothing changed

                  b) I need new seeds to be planted where ever Johnny's new position is, without the old seeds moving from their current positions. I thought creating a "holder" with coordinates equal to where ever Johnny was at the time, would work, but I'm certain I didn't do something right. The holder sticks in place, but if I'm holding the space bar, the seed follows without planting, and if I press the space bar after planting, the old seed disappears and a new seed appears where ever I am. How do I change it so it stores a reference for each seed?

                  to reply 3.
                  Aside from Swap depth and setting depth at start, I've seen some kind of i++ depth code before, but don't quite understand it. Is there a help/tutorial/site place for attached depths? Even more than arrays and Collision detection, depths are my Nemesis :(

                  to reply 4.
                  How do I keep my holder from being overwritten?
                  • 6. attach movieclip problems
                    Ned Murphy Adobe Community Professional & MVP
                    To give things potentially 10000+ names, you can use a counter and increment it each time a new holder is added, then you name your holder with the counter value appended to it...

                    var counterValue = 0;

                    if(Key.isDown(Key.SPACE)){
                    var holderName = "holder"+String(counterValue);
                    this.createEmptyMovieClip(holderName , 100);
                    counterValue++;
                    etc...

                    the constant 100 depth will end up removing instances of holders as kGlad & clbeech mentioned.

                    You could use the counterValue to assign new depths as you go, start it at 100 if that's what's needed... there are a few ways to approach it.
                    • 7. Re: attach movieclip problems
                      shintashi Level 1
                      so I tried this, but it doesn't even load the attached movie clip:



                      • 8. attach movieclip problems
                        Ned Murphy Adobe Community Professional & MVP
                        In this case, holderName is now a string, not an instance name that you can target directly, so you need to have it evaluated as an instance name...

                        this[holderName].attachMovie("seed_mc", "tree_mc", this[holderName].getNextHighestDepth(), {_x:Johnny_Appleseed_mc._x, _y:Johnny_Appleseed_mc._y -50});
                        this[holderName].x=Johnny_Appleseed_mc._x;
                        this[holderName].y=Johnny_Appleseed_mc._y;
                        • 9. Re: attach movieclip problems
                          shintashi Level 1
                          This actually puts me back to where I started. The seed is planted to location, and when Johnny is moved and space bar is pressed, the previous seed moves to the new location.

                          Has anyone tried ammo code or something?
                          • 10. attach movieclip problems
                            Ned Murphy Adobe Community Professional & MVP
                            Your depthjump value should remain a number, not a string, but that's not likely the cause. Try tracing the holderName string each time it is defined and see if it is incrementing the appended numeric part.

                            I'm curious, why are you using a movieclip to house each seed?
                            • 11. attach movieclip problems
                              shintashi Level 1
                              my depth always ends up being whatever I put in the line
                              this.createEmptyMovieClip(holderName ,DEPTH);

                              even if I create something like blah++ or a var depthbonus = depth bonus +1;

                              it doesn't seem to acknowledge the value substituting depth is variable, keeping it constant (100, 1, 0, etc.).

                              I do not know enough about depths to know what to do to fix it.

                              Edit: To answer the question, I'm working on Sandbox style programming.
                              • 12. Re: attach movieclip problems
                                Ned Murphy Adobe Community Professional & MVP
                                I am playing around with this because I tried to recreate a scenario for your code. All I can say for the moment is I am finding an issue using "this" as a reference... when I change it to _root, it starts planting seeds. I am not currently using the holder, so I need to backup and fit that back in. But I think there may be something to do with the keyboard/event somehow being "this" (or some weirdness I have yet to understand).

                                I have to head off for other matters, but I'm going to get back on it later on.
                                • 13. Re: attach movieclip problems
                                  shintashi Level 1
                                  Well, I tried making a backup called file 102 (instead of file 101)

                                  and experimented creating this variation on the code:

                                  ------

                                  i = 1;
                                  k = 2;


                                  onEnterFrame = function(){


                                  itxt.text = Number(i);
                                  ktxt.text = Number(k);


                                  Key.addListener(myListener);
                                  var myListener:Object = new Object();
                                  myListener.onKeyUp = function() {
                                  if (Key.getCode() == Key.SPACE) {
                                  i = i + 0.1;
                                  itxt.text = Number(i);
                                  k = k + 0.1;
                                  ktxt.text = Number(k);
                                  }
                                  };
                                  Key.addListener(myListener);


                                  onEnterFrame function() ={
                                  if(Key.isDown(Key.SPACE)){
                                  this.createEmptyMovieClip("holder" , i);
                                  holder.attachMovie("seed_mc", "tree_mc", k, {_x:Johnny_Appleseed_mc._x, _y:Johnny_Appleseed_mc._y -50});
                                  holder.x=Johnny_Appleseed_mc._x;
                                  holder.y=Johnny_Appleseed_mc._y;
                                  trace("Holder Depth "+holder.getDepth());
                                  //yields 100

                                  trace("Tree Depth "+holder.tree_mc.getDepth());
                                  //yields 101



                                  }

                                  }

                                  ----

                                  well it works and it doesn't. For one, the depth jumps a lot more than I thought it would - instead of +2 per unclick the of the spacebar, it jumps in some kind of ratio, first 20 or thirty, then 40-50, then later by the thousands. The hold depth stays 1, even though the seed depth keeps going up.

                                  The seeds "plant", but their's this peculiar pause between each seed for other actionscript. I put in a "move" actionscript for the seeds to see if they were interacting, (a +y = 5) and annihilate (remove clip) when getting to the other side of the screen. The seeds would load, and wait for the first seed to travel, annihilate, then move. You could load 20 or thirty seeds in one spot, they would all wait patiently like divers in line, then in succession, fly up and disappear.

                                  While this is useful for making incremental "complete the loop" scripts, it's entirely useless for simultaneous events. If I set up my seeds to grow over a week period for example, it would take a year for 52 seeds to work.
                                  • 14. attach movieclip problems
                                    Ned Murphy Adobe Community Professional & MVP
                                    I didn't head back and refit the holder into the picture, but I did create a variety of scenarios for planting seeds. I apologize if this is getting off topic for you at all, but it may hold some clue to a resolution.

                                    I have to confess that I have little experience using keyboard interaction, and even worse, until AS3 came along I was solely a code-on-object user (on(release)-type stuff)... and your issue has led me to an opportunity for new understanding because I'm seeing similar code work/fail for different scenarios, using both the keyboard and buttons.

                                    The failings occur when I try to use "this" as a target reference in a couple of the scenarios. Rather than try to explain all of what I did, here's a link to the resulting file that demonstrates the different approaches (there are some general descriptions shown):

                                    SWF Sample

                                    And here is a link to two versions of the fla (one is CS3 and the other is Flash 8, in case you're interested in seeing the code and aren't using CS3).

                                    Zipped FLAs

                                    Maybe kGlad or clbeech can cue me/us in as to what differentiates the ones that work from those that don't, or maybe you already know?

                                    And here is the code except for that which lives on one of the buttons...


                                    • 15. Re: attach movieclip problems
                                      shintashi Level 1
                                      you may be right. Kglad has a lot of experience with ammo related questions. I think If I try to "force" it to work any further it will only produce a mess later on. How does a game programmer make it so when you tap the attack button a single shot fires, but if you hold down, multiple shots fire? You know, this stuff
                                      http://www.youtube.com/watch?v=giAMDx5FEzI
                                      • 16. attach movieclip problems
                                        Ned Murphy Adobe Community Professional & MVP
                                        If you look at my key-related code, with the listener object, that is one way to get a tap versus a holding to work for a key. Check out the swf sample and use the shift button in that manner (you may have to click on the swf in the browser to get its attention.

                                        I'm just wondering in your case if having the "this" involved with your holder is related to the problem you're having. Maybe if you write your code in a function separately as I did for the CONTROL button it may work better? The external function seems to work in any case.