Current ActionScript 3.0 feature, plus any previous

Ok, I don't know how I did it. Probably the gods of AS3 got mad at me when my scrollbar worked right after I coded it, no error. But I have a very strange problem. I have three different buttons, they are not nested, and they have event listeners for mouse clicks that call a function. Whenever one of these three buttons is pressed, it calls the function it should use, plus any previous ones from swf's life. I have no idea how this happens, to be honest, I'm in shock, I, I'm in shock. How does this happen? Is there something with eventListeners? Perhaps something with the URLLoader? I do not know, I have followed every step of the path and I have nothing, so I throw myself before the court of smart coders in the hope that one of you respectable and gentleladies only know what I am doing wrong.please take pity on the bad soul of the coder.

Pastie Document Class File

package {

    import gs.TweenMax;
    import gs.easing.*;
    import flash.display.*;
    import flash.events.*;
    import flash.ui.Keyboard;
    import flash.net.*;
    import flash.external.ExternalInterface;
    import FullScreenScrollBar;

    public class Gessner extends MovieClip {

        //====================================Declare Variables================================//
        var objectsArray:Array = ["whatwedo_bkg", "whoweare_bkg", "howmuch_bkg", "needsome_bkg"];
        var images:Array = [];
        var instances:Array = [];
        var imageInstances:Array = [];
        var paginationButtons:Array =[];
        //=====================================================================================//

    private var xmlLoader:URLLoader = new URLLoader();
    private var xmlData:XML = new XML();

    //======================================================================================
    public function initXML(e:Event) {
    //======================================================================================
        var target:String = e.currentTarget.name;
        if (e.currentTarget.name == "recent_projects" || e.currentTarget.name == "whoweare") {
            xmlLoader.addEventListener(Event.COMPLETE, onCompleteXML);
            xmlLoader.load(new URLRequest("http://www.gessnerengineering.com/projects"));
        } else if (e.currentTarget.name == "our_staff") {
            xmlLoader.addEventListener(Event.COMPLETE, onCompleteXML);
            xmlLoader.load(new URLRequest("http://www.gessnerengineering.com/profiles"));
        }

        //========================================================================
         function onCompleteXML(e:Event):void {
        //========================================================================
            xmlData = new XML(e.currentTarget.data);
            makePagination();
            trace("onCompleteXML() target: " + target);
            if (target == "recent_projects" || target == "whoweare") {
                trace("loadProjectXML() got called from onCompleteXML();");
                loadProjectsXML(0, xmlData);
            } else if (target == "our_staff") {
                loadStaffXML(0, xmlData);
            }
        }

        //========================================================================
         function makePagination():void {
        //========================================================================
            var pages:Number;
            trace("makePagination() target: " + target);
            if (target == "recent_projects" || target == "whoweare") {
                pages = Math.ceil(xmlData.project.length() / 13);
            } else if (target == "our_staff") {
                pages = Math.ceil(xmlData.profile.length() / 13);
            }
            for (var i:Number = 0; i < pages; i++) {
                var tmp:pageNum = new pageNum();
                var startX:Number = -(((pages * 50) - 10) / 2);
                tmp.Id = i * 13;
                tmp.x = startX;
                startX += 50;
                tmp.y = 0;
                tmp.label.text = String(i+1);
                tmp.buttonMode = true;
                tmp.mouseChildren = false;
                tmp.useHandCursor = true;
                tmp.addEventListener(MouseEvent.CLICK, onChangePage);
                Gessner(root).pages.whoweare_bkg.pagination.addChild(tmp);
                paginationButtons.push(Gessner(root).pages.whoweare_bkg.pagination.getChildByName(tmp.name));
            }
        }

        //========================================================================
        function onChangePage (e:MouseEvent):void {
        //========================================================================
            if (target == "recent_projects" || target == "whoweare") {
                trace("loadProjectXML() got called from onChangePage();");
                loadProjectsXML(e.currentTarget.Id as Number, xmlData);
            } else {
                loadStaffXML(e.currentTarget.Id as Number, xmlData);
            }
        }
    }


    //======================================================================================
    public function loadProjectsXML(startNum:Number, xmlData:XML):void {
    //======================================================================================
        removeAll();
        var index:int = 1;
        var endNum:Number = startNum + 13;
        if (endNum > xmlData.project.length()) {
            endNum = xmlData.project.length();
            //trace("projectENDNUM" + endNum);
        }
        for(startNum; startNum < endNum; startNum++) {
            var instance:String = "cont" + index;
            var imageURL:String = "http://www.gessnerengineering.com/system/files/" + xmlData.project[startNum].assets.asset[0]["id"] + "/thumb/" + xmlData.project[startNum].assets.asset[0]["file-file-name"];

            loadImage(imageURL, instance);
            Gessner(root).pages.whoweare_bkg[instance].title.text = xmlData.project[startNum].name;
            Gessner(root).pages.whoweare_bkg[instance].description.text = xmlData.project[startNum].description;
            instances.push(instance);

            index++;
        }
    }

    //=======================================================================================
    public function loadStaffXML(startNum:Number, xmlData:XML):void {
    //=======================================================================================
        removeAll();
        var index:int = 1;
        var endNum:Number = startNum + 13;
        if (endNum > xmlData.profile.length()) {
            endNum = xmlData.profile.length();
            ////trace("endNUM" + endNum);
        }
        for(startNum; startNum < endNum; startNum++) {
            var instance:String = "cont" + index;
            var imageURL:String = "http://www.gessnerengineering.com/system/avatars/" + xmlData.profile[startNum]["id"] + "/thumb/" + xmlData.profile[startNum]["avatar-file-name"];

            loadImage(imageURL, instance);
            Gessner(root).pages.whoweare_bkg[instance].title.text = xmlData.profile[startNum]["user-id"];
            Gessner(root).pages.whoweare_bkg[instance].description.text = xmlData.profile[startNum]["bio"];
            instances.push(instance);
            index++;
        }
    }

    //=======================================================================================
    private function loadImage(imageURL:String, instance:String):void {
    //=======================================================================================
            var imageLoader:Loader = new Loader();
            var imageRequest = new URLRequest(imageURL);

            imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
            imageLoader.load(imageRequest);

            function onComplete(e:Event) {
                var image:Bitmap = (Bitmap)(e.currentTarget.content);
                Gessner(root).pages.whoweare_bkg[instance].thumb.addChild(image);
                images.push(Gessner(root).pages.whoweare_bkg[instance].thumb.getChildByName(image.name));
                imageInstances.push(instance);
                //imageLoader.unload();
            }
    }

    //=======================================================================================
    private function removeAll():void {
    //=======================================================================================
        var lengthOfIndex:uint = instances.length;
        /*for each(var button:DisplayObject in paginationButtons) {
            Gessner(root).pages.whoweare_bkg.pagination.removeChild(button);
        }*/
        for (var i:uint = 0; i < lengthOfIndex; i++) {              
            Gessner(root).pages.whoweare_bkg[instances[i]]["title"].text = "";
            Gessner(root).pages.whoweare_bkg[instances[i]]["description"].text = "";
            try {
            Gessner(root).pages.whoweare_bkg[imageInstances[i]].thumb.removeChild(images[i]);
            } catch (ArgumentError) {
                //trace("error: " + images[i].name + "::" + instances[i]);
            }
        }
        paginationButtons.splice(0, paginationButtons.length);
        images.splice(0, lengthOfIndex);
        imageInstances.splice(0, lengthOfIndex);
        instances.splice(0, lengthOfIndex);         
    }
}

      

Document timeline action

this.pages.whoweare_bkg.recent_projects.addEventListener(MouseEvent.CLICK, initXML);
this.pages.whoweare_bkg.our_staff.addEventListener(MouseEvent.CLICK, initXML);
this.whoweare.addEventListener(MouseEvent.CLICK, initXML);

      

Exit Pastie

##first click
makePagination() target: whoweare
onCompleteXML() target: whoweare
loadProjectXML() got called from onCompleteXML();
projectENDNUM3

##second click
makePagination() target: whoweare
onCompleteXML() target: whoweare
loadProjectXML() got called from onCompleteXML();
projectENDNUM3
makePagination() target: recent_projects
onCompleteXML() target: recent_projects
loadProjectXML() got called from onCompleteXML();
projectENDNUM3

##third click
makePagination() target: whoweare
onCompleteXML() target: whoweare
loadProjectXML() got called from onCompleteXML();
projectENDNUM3
makePagination() target: recent_projects
onCompleteXML() target: recent_projects
loadProjectXML() got called from onCompleteXML();
projectENDNUM3
makePagination() target: recent_projects
onCompleteXML() target: recent_projects
loadProjectXML() got called from onCompleteXML();
projectENDNUM3

##fourth click
makePagination() target: whoweare
onCompleteXML() target: whoweare
loadProjectXML() got called from onCompleteXML();
projectENDNUM3
makePagination() target: recent_projects
onCompleteXML() target: recent_projects
loadProjectXML() got called from onCompleteXML();
projectENDNUM3
makePagination() target: recent_projects
onCompleteXML() target: recent_projects
loadProjectXML() got called from onCompleteXML();
projectENDNUM3
makePagination() target: recent_projects
onCompleteXML() target: recent_projects
loadProjectXML() got called from onCompleteXML();
projectENDNUM3

and so on and so on

      

0


a source to share


3 answers


Oh, I see. He's here in this part:

xmlLoader.addEventListener(Event.COMPLETE, onCompleteXML);
xmlLoader.load(new URLRequest("http://www.gessnerengineering.com/projects"));

      



Each time this code is called, you add a new event listener to the xmlLoader. So every subsequent time you call it, it will call onCompleteXML +1 times.

+2


a source


It doesn't look like you ever remove event listeners - if you want to. Also, what is your target variable? I can't see where this is declared, do you want to use e.currentTarget or something else?



+1


a source


Check what you are doing in your timeline and make sure you have a command stop()

and don't add event repeaters again.

Each time you enter a keyframe in the timeline, your event listeners are added. So I am assuming that you move to another frame and then return to the same frame, which will add event listeners.

+1


a source







All Articles