Skip to main content
Inspiring
March 16, 2010
Answered

Trouble moving externally loaded image

  • March 16, 2010
  • 2 replies
  • 804 views

I'm new to AS3 and I'm having a problem moving an externally loaded image within a Class. I'm just not sure how to move the externally loaded image.  I thought of creating a Sprite and then moving an added child Sprite, to which I would then load the image.  But I'm getting this error:

1119: Access of possibly undefined property spHolder through a reference with static type flash.display:Sprite.

I commented below where the error is being triggered:

==========================================

package {

   
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.display.Loader;
    import flash.net.URLRequest;
   
    public class SnapShot extends MovieClip {
       
        public var strLink:String;
        public var strImage:String;
        private var spPolaroid:Sprite = new Sprite();
        private var ldPhoto:Loader;
       
        function SnapShot(strImg:String,strURL:String):void {
           
                strLink = strURL;
                strImage = strImg;
               
                trace("New SnapShot");
               
                spPolaroid.graphics.beginFill(0xCCCCCC);
                spPolaroid.graphics.drawRect(0,0,120,140);
                addChild(spPolaroid);
               
                var spHolder:Sprite = new Sprite();
                spHolder.x = 10;
                spHolder.y = 10;
                spPolaroid.addChild(spHolder);
                               
                loadImage(strImage);
           
        };       
       
        private function loadImage(strUrl:String):void {
                   
            ldPhoto = new Loader();
            ldPhoto.load(new URLRequest(strUrl));
            ldPhoto.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
           
        };
               
        private function imageLoaded(evt:Event):void {
           
            spPolaroid.spHolder.addChild(ldPhoto); // Here's where the error is hitting
           
        };
       
    };   
   
};

==========================================

This topic has been closed for replies.
Correct answer Ned Murphy

I can honestly say that I don't know why the problem exists... and it is probably because I am not well versed in class matters.  But since you have a handle on the spHolder object, you don't need to target it thru the parent...

        private function imageLoaded(evt:Event):void {
  
            spHolder.addChild(ldPhoto);
           
        };

2 replies

Participating Frequently
March 16, 2010

Hi,

I have fixed your error message and now images is loading . Please use this code

package {
  
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.display.Loader;
    import flash.net.URLRequest;
  
    public class SnapShot extends MovieClip {
      
        public var strLink:String;
        public var strImage:String;
        private var spPolaroid:Sprite = new Sprite();
        private var spHolder:Sprite = new Sprite();
        private var ldPhoto:Loader;
      
        function SnapShot1(strImg:String,strURL:String):void {
          
                strLink = strURL;
                strImage = strImg;              
                trace("New SnapShot");              
                spPolaroid.graphics.beginFill(0xCCCCCC);
                spPolaroid.graphics.drawRect(0,0,120,140);
                addChild(spPolaroid);
                spHolder.x = 10;
                spHolder.y = 10;
                spPolaroid.addChild(spHolder);
                              
                loadImage(strImage);
          
        };      
      
        private function loadImage(strURL:String):void {
                  
            ldPhoto = new Loader();
            ldPhoto.load(new URLRequest(strURL));
            ldPhoto.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
          
        };
              
        private function imageLoaded(evt:Event):void {
          
            spHolder.addChild(ldPhoto);
          
        };
      
    };  
  
};

Note: This line of code pass in your fla file. SnapShot1('magazine.png','magazine.png');

Hopefully it will help you.

Please make this post as solved and closed.

Thanks

- ActionScript Developer

Ned Murphy
Legend
March 16, 2010

Try declaring the spHolder along with the rest of the object variables outside of the functions.... haing var spHolder:Sprite = new Sprite(); inside the function is limiting its scope to within the function, so it is likely not recognized in the imageLoaded function

0degreesKAuthor
Inspiring
March 16, 2010

Modified as follows, but I get the same error:

==========================================

package {
   
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.display.Loader;
    import flash.net.URLRequest;
   
    public class SnapShot extends MovieClip {
       
        public var strLink:String;
        public var strImage:String;
        private var spPolaroid:Sprite = new Sprite();
        private var spHolder:Sprite = new Sprite();
        private var ldPhoto:Loader;
       
        function SnapShot(strImg:String,strURL:String):void {
           
                strLink = strURL;
                strImage = strImg;
               
                trace("New SnapShot");
               
                spPolaroid.graphics.beginFill(0xCCCCCC);
                spPolaroid.graphics.drawRect(0,0,120,140);
                addChild(spPolaroid);
                spHolder.x = 10;
                spHolder.y = 10;
                spPolaroid.addChild(spHolder);
                               
                loadImage(strImage);
           
        };       
       
        private function loadImage(strUrl:String):void {
                   
            ldPhoto = new Loader();
            ldPhoto.load(new URLRequest(strUrl));
            ldPhoto.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
           
        };
               
        private function imageLoaded(evt:Event):void {
           
            spPolaroid.spHolder.addChild(ldPhoto);
           
        };
       
    };   
   
};

==========================================

Ned Murphy
Ned MurphyCorrect answer
Legend
March 16, 2010

I can honestly say that I don't know why the problem exists... and it is probably because I am not well versed in class matters.  But since you have a handle on the spHolder object, you don't need to target it thru the parent...

        private function imageLoaded(evt:Event):void {
  
            spHolder.addChild(ldPhoto);
           
        };