Skip navigation
Currently Being Moderated

as3 image load position

Sep 29, 2010 1:38 AM

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 ()

 
Replies
  • Currently Being Moderated
    Sep 29, 2010 1:57 AM   in reply to jagguy999

    You can use a double loop  and increase

    x and y positions for every loader.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2010 3:35 AM   in reply to jagguy999

    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++;

     

    }

     

    }

     

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2010 4:05 AM   in reply to Hakan KIRIK

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2010 4:21 AM   in reply to jagguy99

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2010 4:24 AM   in reply to jagguy99

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

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2010 4:32 AM   in reply to Hakan KIRIK

    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");

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2010 5:04 AM   in reply to jagguy99

    it works.

    You need to use:

     

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

    addChild(li);

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2010 5:10 AM   in reply to Hakan KIRIK
     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2010 5:16 AM   in reply to Hakan KIRIK

    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);

     

     

     

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2010 6:01 AM   in reply to jagguy99

    it works.

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

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2010 6:15 AM   in reply to Hakan KIRIK

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 29, 2010 6:43 AM   in reply to jagguy999

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points