Skip to main content
Inspiring
July 6, 2010
Question

Help with buttons loading sections. Code looks awkward!

  • July 6, 2010
  • 2 replies
  • 373 views

Hi everyone!

I have a class that creates a navBar with some buttons. Each button loads an external .swf and everything is working just fine BUT the code is really awkward. I also wanted to use TweenLite to add transitions from section to section.

I also wanted to handle the "section change" process from yet another Class (SectionChange) but whenever I try it I fail miserably.

Here's what I have (including the vars), without the SectionChange Class, that's working.

private var _app:Sprite;
private var _navData:Array;
private var _loader:LoadDisplayObject;
private var _container:Sprite = new Sprite();
private var _currentSection:String = "";

public function NavBar(app:Sprite, navData:Array) {
     _app = app;
     _navData = navData;
     _navData.pop();
     addChild(_container);
     build();
}

private function build():void {
     for (var i:uint;i < _navData.length;i++) {
          var menuBtn:MenuBtn = new MenuBtn(_navData);
          menuBtn.name = _navData;
          menuBtn.x = 420 + (menuBtn.width + 3) * i;
          menuBtn.y = 60;
          menuBtn.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
          addChild(menuBtn);
     }
}

private function onClick(e:MouseEvent):void {
     if (_currentSection == e.target.name + ".swf") {
          trace("Section Already Loaded");
     } else {
          _currentSection = e.target.name + ".swf";
          _loader = new LoadDisplayObject(_currentSection);
          _loader.addEventListener("displayObjectLoaded", checkChildrenNum, false, 0, true);
          _container.addChild(_loader);
          trace("Another Section were Loaded");
     }
}

private function checkChildrenNum(e:Event):void {
     if (_container.numChildren > 1) {
          _container.removeChildAt(0);
     }
}

As I said, this is working, but it's going to be a mess when I start adding the Tweens.

So here is the same code now using the SectionChange class that I tryed to implement.

//Inside NavBar class (same as above)

private function onClick(e:MouseEvent):void {

//edit: ADDING THE _sectionChange TO THE DISPLAY LIST HELPS A LOT WITH THE ERRORS and returns only one error.

     addChild(_sectionChange);

     _sectionChange = new SectionChange(e.target.name + ".swf");
}

And here's the SectionChange class:

public class SectionChange extends Sprite {
     private var _loader:LoadDisplayObject;
     private var _container:Sprite = new Sprite();
     private var _currentSection:String = "";

     public function SectionChange(currentSection:String = "") {
          if (_currentSection == currentSection) {
               trace("Section Already Loaded");
          } else {
               _currentSection = currentSection;
               _loader = new LoadDisplayObject(_currentSection);
               _loader.addEventListener("displayObjectLoaded", checkChildrenNum, false, 0, true);
               _container.addChild(_loader);
               trace("Another Section were Loaded");
          }
     }
    
     private function checkChildrenNum(e:Event):void {
          if (_container.numChildren > 1) {
               _container.removeChildAt(0);
          }
     }
}

I don't know what I'm doing wrong here but I get tons of errors with this code.

edit: Now that I'm adding the _sectionChange to the display list I only get the following error:

TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/addChild()
    at br.com.publishyours::NavBar/onClick()

Thanks again for all the patience!

Message was edited by: newToAS3

This topic has been closed for replies.

2 replies

newToAS3Author
Inspiring
July 7, 2010

I'm just trying to mark this thread as answered... editing my last post won't do.

newToAS3Author
Inspiring
July 6, 2010

I finaly understood the error and got the SectionChange to work.