16 Replies Latest reply on Sep 29, 2010 4:59 PM by jagguy999

    as3 image load position

    jagguy999 Level 1

      q1)where do i set screen dimensions?
      q2)how can i set the images position on screen without using the imageLoaded function because I need to display many images and dont want to create  this imageLoaded for every image.

       

      package
      {
          import flash.display.Sprite;
         
          public class atest1 extends Sprite
          {
              import flash.display.Bitmap;
              import flash.display.Loader;
              import flash.display.Sprite;
              import flash.events.Event;
              import flash.net.URLRequest;

       

             
              public function atest1()
              {
                  var loader:Loader = new Loader;
                 
                  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
                  loader.load(new URLRequest("ladybug.png"));
                  //loader.x=200;
                  //loader.y=100;
              }
             
              private function imageLoaded(event:Event):void
              {
                  var image:Bitmap = new Bitmap(event.target.content.bitmapData);
                  addChild(image);
                  //image.x=200;
                  //image.y=10;
              }

       

          }
      }

       

      var li:atest1= new atest1 ()

        • 1. Re: as3 image load position
          Hakan KIRIK Level 4

          You can use a double loop  and increase

          x and y positions for every loader.

          • 2. Re: as3 image load position
            jagguy999 Level 1

            NOt sure wheat you mean
            can you give me an exampe?

            • 3. Re: as3 image load position
              Hakan KIRIK Level 4

              here is an example (with just one loop):

               

              var array:Array=["url1","url2","url3"]; // u should fill that array somehow with more data...

               

              var wid:Number=150;

              var hei:Number=100;

              var x_counter:int=0;

              var y_counter:int=0;

              var columns:int=4;

               

               

              function loaderFunc():void{

               

              for (var i:Number = 0; i < array.length; i++){

               

              var thumb_url = array[i].toString();

               

              var thumb_loader = new Loader();

               

              thumb_loader.load(new URLRequest(thumb_url));

               

              thumb_loader.x = (wid+10)*x_counter;

               

              thumb_loader.y = (hei+10)*y_counter;

               

              addChild(thumb_loader);

               

              if (x_counter+1 < columns){

               

              x_counter++;

               

              } else {

               

              x_counter = 0;

               

              y_counter++;

               

              }

               

              }

               

              }

              • 4. Re: as3 image load position
                jagguy99 Level 1

                An easier way is using paramters in the constructors but it doesnt work??

                why does this code fail to load anything without an error?

                 

                package

                 

                {

                 

                import flash.display.Sprite;

                 

                 

                public class atest1 extends Sprite

                {

                 

                import flash.display.Bitmap;

                 

                import flash.display.Loader;

                 

                import flash.display.Sprite;

                 

                import flash.events.Event;

                 

                import flash.net.URLRequest;

                 

                import flash.text.*;

                 

                 

                private var myy:int;

                 

                private var myx:int;

                 

                private var txt1:TextField = new TextField();

                 

                 

                public function atest1(x:int,y:int)

                {

                 

                var loader:Loader = new Loader;

                myx=x;

                myy=y;

                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);

                loader.load(

                new URLRequest("ladybug.png"));

                txt1.text =

                "Text1";

                addChild(txt1);

                 

                import flash.text.*;

                 

                }

                 

                 

                private function imageLoaded(event:Event):void

                {

                 

                var image:Bitmap = new Bitmap(event.target.content.bitmapData);

                 

                image.x=myx;

                image.y=myy;

                addChild(image);

                }

                 

                }

                }

                 

                var

                 

                 

                li:atest1= new atest1(10,200); //doesnt work

                • 5. Re: as3 image load position
                  rkbkbvrkbqfvvkjbqfvkbkqfj Level 2

                  Scene 1, Layer 'Layer 1', Frame 1, Line 1    1198: Attributes are not allowed on package definition.
                  Scene 1, Layer 'Layer 1', Frame 1, Line 2    1084: Syntax error: expecting identifier before mult.
                  Scene 1, Layer 'Layer 1', Frame 1, Line 4    1037: Packages cannot be nested.
                  Scene 1, Layer 'Layer 1', Frame 1, Line 6    1084: Syntax error: expecting identifier before mult.
                  Scene 1, Layer 'Layer 1', Frame 1, Line 9    1071: Syntax error: expected a definition keyword (such as function) after attribute *, not public.
                  Scene 1, Layer 'Layer 1', Frame 1, Line 9    1084: Syntax error: expecting rightbrace before leftbrace.
                  Scene 1, Layer 'Layer 1', Frame 1, Line 50    1071: Syntax error: expected a definition keyword (such as function) after attribute *, not private.

                  • 6. Re: as3 image load position
                    Hakan KIRIK Level 4

                    You will need again a loop if you wanna use more than one image (and calculate each time x and y positions)

                    • 7. Re: as3 image load position
                      jagguy99 Level 1

                      I thought AS3 was OOP?

                      I cant pass paramters in constructors so I need to pass them when an imge is loaded .

                      If I need to load 3 images how do i do this in as3 without constructors????

                       

                       

                      var li:atest1= new atest1("img1");

                      var li:atest1= new atest1("img2");

                      var li:atest1= new atest1("img3");

                      • 8. Re: as3 image load position
                        Hakan KIRIK Level 4

                        it works.

                        You need to use:

                         

                        var li:atest1= new atest1(10,200);

                        addChild(li);

                        • 10. Re: as3 image load position
                          jagguy99 Level 1

                          no it doesnt work.

                           

                          Here is my whole code and I get1 error with addchild

                           

                          package

                          {

                          [

                          SWF(backgroundColor="blue", width="400",height="300")]

                           

                          import flash.display.Sprite;

                           

                           

                          public class atest1 extends Sprite

                          {

                          [

                          SWF(backgroundColor="0x00ff00", width="400",height="300")]

                           

                          import flash.display.Bitmap;

                           

                          import flash.display.Loader;

                           

                          import flash.display.Sprite;

                           

                          import flash.events.Event;

                           

                          import flash.net.URLRequest;

                           

                          import flash.text.*;

                           

                           

                          private var myy:int;

                           

                          private var myx:int;

                           

                          private var txt1:TextField = new TextField();

                           

                          private var img1:String;

                           

                           

                          public function atest1(myimage:String)

                          {

                           

                          var loader:Loader = new Loader;

                          img1=myimage;

                          myx=100;

                          myy=100;

                          loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);

                          loader.load(

                          new URLRequest(img1));

                           

                           

                           

                          }

                           

                           

                          private function imageLoaded(event:Event):void

                          {

                           

                          var image:Bitmap = new Bitmap(event.target.content.bitmapData);

                           

                          image.x=myx;

                          image.y=myy;

                          addChild(image);

                          }

                           

                          }

                          }

                           

                           

                          var li:atest1= new atest1("ladybug.png");

                           

                           

                          var addChild(li);

                           

                           

                           

                          • 11. Re: as3 image load position
                            Hakan KIRIK Level 4

                            it works.

                            Please download my example (fla+as) and test it.

                            • 12. Re: as3 image load position
                              jagguy99 Level 1

                              I have flashbuilder and

                              my code has changed a little since then as you can see.

                              Could you try my current code where I want to load a file by passing the filename in the constructor?

                               

                              Might be flashbuilder is no good.

                              • 13. Re: as3 image load position
                                jagguy999 Level 1

                                i cant open fla files so could you paste it?

                                • 14. Re: as3 image load position
                                  Hakan KIRIK Level 4

                                  I did it before:

                                   

                                  codes in fla file:

                                   

                                  var aa:atest1=new atest1(33,44);
                                  addChild(aa);

                                   

                                   

                                  The .as file is in the Rar file and with Flashbuilder you can open it.

                                  • 15. Re: as3 image load position
                                    jagguy999 Level 1

                                    Hi,

                                    no no that is not the file that is old code and I get an error with addchild in flashbuilder

                                     

                                    What I wanted  to do is pass the string of the image to display

                                     

                                    package


                                    {
                                       
                                        import flash.display.Sprite;
                                       
                                        public class as2 extends Sprite
                                        {
                                           
                                            import flash.display.Bitmap;
                                            import flash.display.Loader;
                                            import flash.display.Sprite;
                                            import flash.events.Event;
                                            import flash.net.URLRequest;
                                            import flash.text.*;
                                           
                                            private var myy:int;
                                            private var myx:int;
                                            private var txt1:TextField = new TextField();
                                            private var img1:String;
                                           
                                            public function as2(myimage:String="")
                                            {
                                               
                                                var loader:Loader = new Loader;
                                                this.img1=myimage;
                                                myx=100;
                                                myy=100;
                                                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
                                                loader.load(new URLRequest(img1));
                                               
                                               
                                               
                                            }
                                           
                                            private function imageLoaded(event:Event):void
                                            {
                                                var image:Bitmap = new Bitmap(event.target.content.bitmapData);
                                               
                                                image.x=myx;
                                                image.y=myy;
                                                addChild(image);
                                            }
                                           
                                        }
                                    }

                                     

                                    var li:as2= new as2("explorer.png");
                                    this.addChild(li);
                                    • 16. Re: as3 image load position
                                      jagguy999 Level 1

                                      I suspect my flashbuilder isnt working so could some one try my code on the next post above  and just load any image.

                                       

                                       

                                      I spent hours on this and all I need is someone to verify if this does work.
                                      Then I can look at my installation.