收录日期:2021/01/26 17:32:35 时间:2010-03-31 04:10:57 标签:actionscript-3,loops

I have a gallery where it will load an image after a previous image is loaded, so every time 'i' will +1 so that it could move to the next image. This code works fine on my other files, but I dunno why it doesn't work for the current file.

Normally if I trace 'i' the correct will be

0,1,2,3,4,5,6... etc

adding on till the limit, but this files its 'i' repeat the 1st number twice, only it continues to add

0,0,1,2,3,4,5,6...etc

The code is completely the same with the other file I'm using, but I don't know why it just doesn't work here. The code does not seems to have any problem. Anyway i can work around this situation?

private var i:uint=0;

private function loadItem():void {

        if (i<myXMLList.length()) {
            loadedPic=myXMLList[i].thumbnails;

            galleryLoader = new Loader();
            galleryLoader.load(new URLRequest(loadedPic));
            galleryLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,picLoaded);
        } else {
            adjustImage();
        }
    }

    private function picLoaded(event:Event):void {
        var bmp=new Bitmap(event.target.content.bitmapData);
        bmp.smoothing=true;
        bmpArray.push(bmp);
        imagesArray[i].addChild(bmp);

        i++;
        loadItem();
    }

That code looks fine, it's really very difficult to tell what might be causing that. The best advice I can give you would be to set some break points; for example set one at the point where you add the event listener to the galleryLoader object, and then run Flash's debugger. You run it by going to Debug > Debug Movie.

The debugger will stop at that line, and you will be able to look in the Variables window to see your i variable as you step through the code line-by-line. You step through by pressing the step in button, inside the Debug Console window. I've put in an image below showing the debugger's specific windows (that you won't see at any other time in Flash):

alt text

I think that by doing that (if you haven't already tried, that is) you should pretty quickly see what's going wrong. Hope it helps anyway.

debu

I've finally found out whats the problem.

public function grid():void {
        addEventListener(Event.ADDED_TO_STAGE, add2Stage);
    }

    private function add2Stage(event:Event):void {
        myLoader=new URLLoader();
        myLoader.load(new URLRequest(galleryXML));
        myLoader.addEventListener(Event.COMPLETE,processXML);
    }

Its because I'm using adding the functions to stage. After I remove the add2stage function then everything's fine. But why will it affect and run the function twice at 1st? I'm just adding it to the stage to get the stage width.