views:

50

answers:

2

Hi iam trying to load a few external jpegs in As3. It works fine locally in Flash, but it dosen't work at all ion my server. My app also loads a youtube video simultaneously.

function drawResult(index,videoID,song_title,thumbnail:String=null)
{
    var theClip:resultRowClip=new resultRowClip ();
    _clip.addChild(theClip);
    myArray[index] = new Array(videoID,theClip);
    theClip.y=0+(43*(index-1));
    theClip.rowText.text = song_title;
    theClip.rowBack.visible = false;
    if (thumbnail != ""){
        theClip.tHolder.visible=true;
        loadImage(thumbnail,index);
    }
}

function loadImage(url:String,index):void
{
    //this.myClip.myText.text += "load image";
    // Set properties on my Loader object
    var imageLoader:Loader = new Loader();
    imageLoader.load(new URLRequest(url));
    imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function (evt:Event){imageLoaded(evt,index)});
}

function imageLoaded(evt,id):void
{
    //this.myClip.myText.text += "id : evt : " + evt.status;
    // Load Image
    var image:Bitmap = new Bitmap(evt.target.content.bitmapData);
    myArray[id][1].tHolder.addChild(image);
    myArray[id][1].tHolder.width=myArray[id][1].tHolder.width*0.35;
    myArray[id][1].tHolder.height=myArray[id][1].tHolder.height*0.35;
}

Does anyone knows what the problem is ?

** I added two Evenet listeners from io Error :

imageLoader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);

This is the function for handling the errors :

private function ioErrorHandler(event:IOErrorEvent):void {
    this.myClip.myText.text +=("ioErrorHandler: " + event);
}

anyway, I got no errors...

I also tried to move the listeners before imageLoader.load but it's still the same...no errors and no data loaded..

I change my code to patrikS suggestion :

function loadImage(url:String,index):void
        {   

            //this.myClip.myText.text += "load image";
            // Set properties on my Loader object
            //if (index != 1) return;
            var imageLoader:Loader = new Loader();
            imageLoader.name = index.toString();

            //myArray[index][1].addChild(imageLoader);
            //imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function (evt:Event){imageLoaded(evt,index)});
            imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);     
            imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
            imageLoader.load(new URLRequest(url));


        }

My current completeHandler function(tnx patrikS ) :

private function completeHandler(evt:Event):void{
            //this.myClip.myText.text += "id : evt : " + evt.status;
            // Load Image
            trace("evt target loader name : "+ evt.target.loader.name );
            evt.target.removeEventListener(Event.COMPLETE, completeHandler );
            var image:Bitmap = new Bitmap(evt.target.content.bitmapData);
            myArray[evt.target.loader.name][1].tHolder.addChild(image);
            myArray[evt.target.loader.name][1].tHolder.width=myArray[evt.target.loader.name][1].tHolder.width*0.35;
            myArray[evt.target.loader.name][1].tHolder.height=myArray[evt.target.loader.name][1].tHolder.height*0.35;
            //trace (hadar.y + "Y / X" + hadar.x);
        }

It still work only on flash IDE and dosent work on any browser...

A: 

Listeners should be added before calling the load() method. Also there's no real advantage in using a closure for the complete event listener & think of removing the event listeners!

function loadImage(url:String, index:int):void
{
    //this.myClip.myText.text += "load image";

    // Set properties on my Loader object
    var imageLoader:Loader = new Loader();
    imageLoader.name = index.toString();

    //make sure you to add your listeners here!
    imageLoader.contentLoaderInfo.addEventListener(
                     IOErrorEvent.IO_ERROR,ioErrorHandler);

    imageLoader.contentLoaderInfo.addEventListener(
                     Event.COMPLETE, completeHandler );

    imageLoader.load(new URLRequest(url));

}

function completeHandler(event:Event ):void
{
    //imageLoaded(evt,index);
    trace( event.target.loader.name );
    event.target.removeEventListener(Event.COMPLETE, completeHandler );
}
PatrickS
Thank you,but moving the listeners before imageLoader.load didn't change anything. anything else wrong with my code ?
Hadar
imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, completeHandler ); --> if I'm using this I lose the Id of the object I want to update...
Hadar
not necessarily... use the loader.name property to identify your object
PatrickS
on drawResult() I am trying to draw a few movieclips, one for each result, than i want to draw the bitmap on same MovieClip. how can I use loader.name to get the MovieClip Object.? Thank you again!
Hadar
look at my edited code ;)
PatrickS
I'll try, tnx :)
Hadar
Works fine, but only on Flash IDE...still not working in browser...
Hadar
A: 

try urlstream and check crossdomain.xml :)

    urlstream = new URLStream();
    urlstream.addEventListener(Event.COMPLETE, onLoad);
    urlstream.addEventListener(IOErrorEvent.IO_ERROR, onErr);
    urlstream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onErr);
    urlstream.load(req);
       private function onLoad(e:Event):void {
            var ba:ByteArray = new ByteArray();
            urlstream.readBytes(ba, 0, urlstream.bytesAvailable);
            _loader.contentLoaderInfo.addEventListener(Event.INIT, onBytesLoad);
            _loader.loadBytes(ba);
        }
www0z0k