13 Replies Latest reply: Mar 15, 2011 5:20 PM by barpos RSS

    LOADER CONTAINER VS SPRITE

    barpos Community Member

      Is there an advantage to store loaded images into a sprite?

       

      var container:Sprite = new Sprite();
      addChild(container);
      var pictLdr:Loader = new Loader();
      var pictURL:String = "banana.jpg"
      var pictURLReq:URLRequest = new URLRequest(pictURL);
      pictLdr.load(pictURLReq);
      pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
      function imgLoaded(event:Event):void
      {
           container.addChild(pictLdr.content);
      }

        • 1. Re: LOADER CONTAINER VS SPRITE
          Rob Dillon CommunityMVP

          Yes, a sprite has much greater flexability than a loader. A loader can contain only the one thing that it loads. So you can only ever animate this one thing. If you need to have the loader content work in conjunction with other objects on the stage, then you'll have to address each one individually. Using a sprite, or a movieClip, object, you can animate that object alone, or, you can add additional content to that sprite and animate or operate on the collection of objects in the sprite.

          • 2. Re: LOADER CONTAINER VS SPRITE
            barpos Community Member

            Thanks Rob,

             

            I was wondering if dumping loaded assets into a sprite would be more efficient (speedwise, or otherwise) than using several loaders.

             

            I'll go with the sprite to store all my loaded images.

             

            Thanks,

             

            Ron

            • 3. Re: LOADER CONTAINER VS SPRITE
              barpos Community Member

              Ok, once I move the loaded bitmap image into a sprite container, there's no word on how to reference that bitmap image.  How does one create an instance of it (so as to be used with __animFactory_Wheel.AnimatorFactory.addTarget(<instance name>, 0))?  I presume I can no longer use the contentLoaderInfo property of the loader object as it is now empty.

               

              Regards,

               

              Ron

              • 4. Re: LOADER CONTAINER VS SPRITE
                Rob Dillon CommunityMVP

                Just refer to the sprite, for all intents and purposes it IS the bitmap.

                 

                I'm guessing it would be:

                __animFactory_Wheel.AnimatorFactory.addTarget(container, 0));

                 

                although I have no idea what that code does.

                • 5. Re: LOADER CONTAINER VS SPRITE
                  barpos Community Member

                  .addTarget is for associating a display object to a tween.  I don't doubt the container name can be utilized as an instance to the bitmap.

                   

                  See http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WSE5F3FFE0-9ED7-4122-9DF6-35BF 597E41E1.html

                   

                      // Call the addTarget function on the AnimatorFactory

                   

                      // instance to target a DisplayObject with this Motion.

                   

                      // The second parameter is the number of times the animation

                   

                      // will play - the default value of 0 means it will loop.

                   

                      // FrontWheel.addTarget(<instance name goes here>, 0);

                  • 6. Re: LOADER CONTAINER VS SPRITE
                    Rob Dillon CommunityMVP

                    Yes, I know what addTarget is, its the rest of the code, the object that's adding the target, that may or may not impinge on the effectiveness of the sprite reference.

                    • 7. Re: LOADER CONTAINER VS SPRITE
                      barpos Community Member

                      Don't have a clue what that means.

                       

                      Anyhow, how about the following code:

                       

                      var penguinsBmd:BitmapData = new BitmapData(loader.content);
                      var penguins:Bitmap = new Bitmap(penguinsBmd);
                      mySpriteContainer.addChild(penguins);

                      • 8. Re: LOADER CONTAINER VS SPRITE
                        barpos Community Member

                        I've since discovered the addTarget() function requires a display object type as the first parameter (according to the language reference).

                         

                        So, I'll be trying the following:

                         

                                                // initialize the display object instances

                         

                                                var tractorBodyObj:DisplayObject = tractorBodyLoader.content;

                         

                                                var frontWheelObj:DisplayObject = frontWheelLoader.content;

                         

                                                var backWheelObj:DisplayObject = backWheelLoader.content;

                         

                                              

                         

                                                // store the loaded iamges into a group container

                         

                                                tractorContainer.addChild(tractorBodyLoader.content);

                         

                                                tractorContainer.addChild(frontWheelLoader.content);

                         

                                                tractorContainer.addChild(backWheelLoader.content);

                         

                                              

                         

                                                // get rid of the loaders (NOT SURE IT IS NEEDE3D)

                         

                                                tractorBodyLoader = undefined;

                         

                                                frontWheelLoader = undefined;

                         

                                                backWheelLoader = undefined;

                         

                                                // initialize the motion tweens


                                                tractorBody();
                                                backWheel();
                                                frontWheel();
                                               
                                                // start the tween animations
                                                TractorBody.addTarget(tractorBodyObj, 0);
                                                BackWheel.addTarget(backWheelObj, 0);
                                                FrontWheel.addTarget(frontWheelObj, 0);

                         

                        Does that make any sense?

                         

                        Ron

                        • 9. Re: LOADER CONTAINER VS SPRITE
                          Rob Dillon CommunityMVP

                          Sort of. Your ordering of the events is correct, but you need to work on the actual objects. Instead of:

                          var tractorBodyObj:DisplayObject = tractorBodyLoader.content;

                          use something like this:

                          var tractorBodyObj:Sprite = tractorBodyLoader.content;

                           

                          You don't want to define something as a DisplayObject, but as a type of DisplayObject. You'll also need to define that group container.

                          It could also be a sprite. You don't need to dispose of the Loaders, Flash will do that for you. I have no idea what you are doing with those tweens. Are you using a custom tween library?

                          • 10. Re: LOADER CONTAINER VS SPRITE
                            barpos Community Member

                            >You  don't want to define something as a DisplayObject, but as a type of  DisplayObject. <

                             

                            Since the bitmap images I want to load externally are of the bitmap class, wouldn't initializing the vars as bitmap type be better than of sprite (container) type??

                             

                            >You'll also need to define that group container.<

                             

                            That's already done.  I only posted partial code.  Yeah, I have over 200 lines of code for that custom class. <g>

                             

                            >I have no idea what you are doing with those  tweens.<

                             

                            Well, those tweens were originally in a movie clip, but running the movie clip at runtime, I've noticed that I cannot make it match the different screen sizes involved upon browser resize.   Still images or buttons are easily resizeable, but MC's don't work because they are too complex.  So, I'm copying the actionscript code generated by each of the tweens of the MC and instead of using the library images (inflating the SWF file for nothing) I've opted to externally load the bitmap images before executing the tween code within my custom class (RollingTractor).

                             

                            >Are you using a custom tween library?<

                             

                            I've never heard of custom tween libraries.  I'm still a novice.  I've been studying AS for the past month or two.

                             

                            Regards,

                             

                            Ron

                            • 11. Re: LOADER CONTAINER VS SPRITE
                              Rob Dillon CommunityMVP

                              As you did earlier, you use the bitmap data as the content of sprite. The sprite is the display object that "shows" the bitmap.

                               

                              There is a tween class, it is a subclass of the transition class. You can use the tween class in actionscript to animate any property or group of properties of any display object. The tween class in Actionscript is pretty rudimentary. There are a bunch of people who have written custom tween class Actionscript. One of the best, and one that I use all the time, is(are) the ones by Greensock, http://www.greensock.com/tweenmax/

                               

                              I admire your enthusiasm, and I love that you're willing to dive in and try and get Flash to do what you want. I hope this project works well for you. Please try and use the on-line help as much as you can. There are a lot of useful examples in there that can show you how to work within the limits of Flash.

                              • 12. Re: LOADER CONTAINER VS SPRITE
                                barpos Community Member

                                >As you did earlier, you use the bitmap data as the content of sprite. The sprite is the display object that "shows" the bitmap.<

                                 

                                Please remind me what I did earlier.  Are you talking about this code:

                                 

                                var penguinsBmd:BitmapData = new BitmapData(loader.content);
                                var penguins:Bitmap = new Bitmap(penguinsBmd);
                                mySpriteContainer.addChild(penguins);

                                 

                                >There is a tween class, it is a subclass of the transition class. You can use the tween class in actionscript to animate any property or group of properties of any display object. The tween class in Actionscript is pretty rudimentary. There are a bunch of people who have written custom tween class Actionscript. One of the best, and one that I use all the time, is(are) the ones by Greensock, http://www.greensock.com/tweenmax/<

                                 

                                I'll check it out.

                                 

                                >I admire your enthusiasm, and I love that you're willing to dive in and try and get Flash to do what you want.<

                                 

                                I have no choice.  My MC gets out of whack everything time the browser is resized.  That makes me look like an idiot. <g>

                                 

                                >Please try and use the on-line help as much as you can. <

                                 

                                Trust me, I do extensively and via Google as well.  But, I sometimes hit a wall of bricks as manuals often waste your time with what you don't need, either too basic info or too complicated to the reader at the current stage of the game.  I've researched how to instantiate an instance of a loaded bitmap graphics quite a bit (over 20 hours on it) and I could find absolutely nothing that comes close to an answer to my question.  It's like no one else has ever used it.

                                 

                                >There are a lot of useful examples in there that can show you how to work within the limits of Flash.<

                                 

                                I'll read some more on tweening in Flash .... But, then again the thing I like about loading bitmaps externally is to allow for the SWF to load faster upon opening the webpage.

                                 

                                Thanks!

                                 

                                Ronald

                                • 13. Re: LOADER CONTAINER VS SPRITE
                                  barpos Community Member

                                  Never mind.  I just tested my code in a test .FLA and was able to figure it out in 10 minutes.