views:

525

answers:

3

Alrighty, I don't know how I did this, probably the AS3 gods became angry with me when my scrollbar worked right after I coded it, with no bugs. But I have a very weird problem. I have three different buttons, they aren't nested within each other, and they have event listeners for mouse clicks that call a function. Whenever one of these 3 buttons is clicked, it calls the function it is supposed to plus all the previous ones from the life of the swf. I have no idea how this is happening, to be honest I'm shocked, I, I feel shocked. How is this happening? Is something with eventListeners? Perhaps something with URLLoader? I don't know, I've traced every step of the way and I have nothing, so I throw myself before the court of smarter coders in hopes that one of you esteemed gentlemen and gentleladies knows just what I'm doing wrong, please take pity on a poor coder's soul.

Document class file Pastie

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's timeline actions

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

Output 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
+2  A: 

Oh I see. It's here in this part:

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

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

CookieOfFortune
Sweet Jebus! Thanks mate, just to let you know I had 56 traces in there before I read your answer here. I was in IRC and had a lot of Firefox tabs open. And you just come along and see it, I guess sometimes you just need a second pair of eyes.
Zach
+1  A: 

It doesn't look like you're ever removing the event listeners - if you want to be doing that. Also, what is your target variable? I'm not seeing where it's declared, do you mean to be using e.currentTarget or something?

quoo
You are correct sir, I wasn't removing the event listener for XMLLoader, thanks to you and CookieOfFortune, it works swimmingly now.
Zach
yeah, he answered while I was posting! damn CookieOfFortune for typing faster than me. (also... I'm not a sir!)
quoo
My goodness a lady on the internet, surely you jest. Regardless of who got here first, I'm in your debt ma lady.
Zach
yeah, I hate it when that happens.
CookieOfFortune
+1  A: 

Check what you are doing on your timeline, and make sure you have a stop() command somewhere, and avoid adding the event listeners again.

Everytime you enter a keyframe on the timeline, your event listeners are added. So I'm guessing you are going to a different frame and then coming back to the same frame that adds the event listeners.

Kekoa