15 Replies Latest reply on Mar 31, 2015 6:44 AM by kglad

    How do I replace whats displayed on a frame in ActionScript

    dkardell

      I have a flash program that I'm updating that needs to dynamically load in 10 images and replace the 10 frames of the existing movieclip.

       

      The program uses gotoAndStop to display and image (frames 1 - 10).  I want to load the images from a folder (which I already have), but now that they are loaded at bitmaps, how do I change the image on frame1 with Bitmap1, then frame2 with bitmap2 and so on.

        • 1. Re: How do I replace whats displayed on a frame in ActionScript
          kglad Adobe Community Professional & MVP

          use addChild to add the bitmaps when you want to change the display.

          • 2. Re: How do I replace whats displayed on a frame in ActionScript
            dkardell Level 1

            well the problem is, is that the program starts off by issuing a gotoAndStop(1).  Then it shows the image in frame 2 with gotoAndStop(2).  I need to change the image in frame 2, yet if I issue gotoAndStop(2) then addChild and the code calls gotoAndStop(1) the child is still shown.  From what I read add Child adds to the timeline and not a frame.  I need to change the individual frames dynamically.

            • 3. Re: How do I replace whats displayed on a frame in ActionScript
              kglad Adobe Community Professional & MVP

              you can add objects to parent movieclips (that only exist) in particular frames.  then you don't have to keep track of what needs to be removed.

              • 4. Re: How do I replace whats displayed on a frame in ActionScript
                dkardell Level 1

                That's my question.  How do you do that.

                 

                I have 10 frames.  Each frame has a different image that is called in code with gotoAndStop(x).  Rather than static images on those 10 frames, I want to dynamically add an image to each frame so that the actionScript that calls gotoAndStop(x) will show the loaded images rather than the embedded ones.

                • 5. Re: How do I replace whats displayed on a frame in ActionScript
                  kglad Adobe Community Professional & MVP

                  eg, add a parent movieclip in the ide to frame 6 (eg, parent6_mc)

                  in frame 6 add script, parent6_mc.addChild(yourloader)

                  • 6. Re: How do I replace whats displayed on a frame in ActionScript
                    dkardell Level 1

                    Hey kglad, still trying to solve this.  so my library object is called box.  and looks like this:

                     

                    (each frame just has a bitmap on it so I selected the image and created a movie clip out of it (class called frame_graphic).  I then named the instance in frame 2 to be frame2_graphic.

                    Screen Shot 2015-03-30 at 2.13.38 PM.png

                    so in my main I'm trying this and its not working: (program does not run.. says attempt to set a null property)

                     

                     

                                   var bitmap:Bitmap = loaders[0].content;

                                    var myBox:Box = touchedBody.GetUserData().box;

                                    bitmap.x = -50;

                                    bitmap.y = -50;

                                   

                                    //myBox.gotoAndStop(touchedBody.GetUserData().boxValue);

                                    myBox.gotoAndStop(touchedBody.GetUserData().boxValue);

                                    myBox.frame1_graphic.addChild(bitmap);

                    • 7. Re: How do I replace whats displayed on a frame in ActionScript
                      kglad Adobe Community Professional & MVP

                      where is loaders defined?

                      where is touchedBody defined?

                      what's GetUserData and where's it defined and what's its box property?

                       

                      but if myBox is a movieclip that you wanted to direct to its second frame you would use:

                       

                      myBox.gotoAndStop(2);

                       

                      if you then wanted to reference something on myBox'es frame 2, you would use:

                       

                      myBox.gotoAndStop(2);

                      myBox.something;  // but with same fp versions you need to use the render event to ensure something exists

                       

                      p.s. that looks like it says frame1_graphic in frame 2, to me.

                      • 8. Re: How do I replace whats displayed on a frame in ActionScript
                        dkardell Level 1

                        Sorry I didn't explain the last one very well. (think of this whole clip as a deck of cards).  The box(aka Box) class in the image above is all of the cards in the deck, with the first frame being the back of the card.  When called to gotoAndStop(2), it should display card2 and then gotoAndStop(1) should return it back to the back of the card.

                         

                        With that being said, let me answer your other questions.

                         

                        Loaders is defined above (this is all in my Main.as).  It loads the 10 bitmaps and once completes then calls the code I posted above.

                         

                        touchedBod.boxValue  is defined above and is nothing more than a reference to the frame (or image in reality) that is to be displayed (so think of it as 1, 2, 3, 4 where the code later on will call gotoAndStop(boxValue) and display the bitmap that is in that frame.

                         

                        myBox is a box (aka Box) is the framed movie clip in the image I posted earlier which contains the frames of each image.

                         

                        So I can direct and go to frame 2 by using myBox.gotoAndStop(2), and I can add a child to that frame with addChild, however when I then issue a gotoAndStop(1) which again is the back of the card, the back is not shown as the  addedChild does not appear to be removed by going back to frame 1

                        • 9. Re: How do I replace whats displayed on a frame in ActionScript
                          kglad Adobe Community Professional & MVP

                          again, if you use addChild the easiest way to ensure the added object is removed when you change frames is to

                           

                          add a parent movieclip in the ide to frame 2 (eg, parent2_mc)

                          in frame 2 use script, parent6_mc.addChild(yourloader)

                          • 10. Re: How do I replace whats displayed on a frame in ActionScript
                            dkardell Level 1

                            I am so sorry I'm being so dense on this.

                             

                            When you say "in frame 2 use script, parent6_mc.addChild(yourloader)

                             

                            do you mean do that in my Main.as

                             

                            I tried that as I posted above:

                             

                                             var bitmap:Bitmap = loaders[2].content;

                                            var myBox:Box = 6;

                                            bitmap.x = -50;

                                            bitmap.y = -50;               

                                            myBox.gotoAndStop(2); // so this would go to frame 2 right?

                                            myBox.frame2_graphic.addChild(bitmap);  // Would add a graphic to the movie clip frame2_graphic which is a movie clip inside of frame 2.

                             

                              is this the same as your solution of "in frame 2 use script, parent2_mc.addChild(yourloader)".  I tried just frame2_graphic which is a movie clip in frame 2 of box.  It does not resolve at run time.

                            • 11. Re: How do I replace whats displayed on a frame in ActionScript
                              kglad Adobe Community Professional & MVP

                              you're displaying different frames of myBox, correct.

                               

                              and you're also loading different bitmaps when displaying different myBox frames, correct?

                               

                              so you want to display whatever is in myBox and a loaded bitmap at the same time, correct?

                              1 person found this helpful
                              • 12. Re: How do I replace whats displayed on a frame in ActionScript
                                dkardell Level 1

                                Actually I want to "replace" whats there with a loaded image from the loader.

                                • 13. Re: How do I replace whats displayed on a frame in ActionScript
                                  kglad Adobe Community Professional & MVP

                                  then why use frames, at all?

                                   

                                  just use one loader to load what you want.  the previously loaded content will automatically be removed from a loader that is reused to load another bitmap.

                                  • 14. Re: How do I replace whats displayed on a frame in ActionScript
                                    dkardell Level 1

                                    kgland, thank you so much for your insight. I should have just rewrote the code.  We have 5 programs that we inherited that use 10 static images in a movie clip on separate frames, and I simply wanted to dynamically load images into those 10 frames and be done with it.

                                     

                                    I seriously thought it would be about an hour worth of work and about 10 lines of code but its looking like that is not the case.  I wanted to use this approach in this app and be able to apply it to the other 5 that I have.