Can't dispatch event until I add child?

I have a Shell.as document class loading several visual sub-classes which extend Sprite. One of those (the most basic one) is Background.as. This sub-class is responsible for loading the FLA’s various graphical background elements (logos, etc.). The function in Shell.as responsible for instantiating this sub-class is:

// function to instantiate the Background.as class
private function loadBackground():void
{
	_background = new Background(
		_configData.market_logo_url,
		_configData.background_url,
		_configData.team_logo_url,
		_configData.sponsorship_logo_url,
		_configData.sponsorship_link
	);
}

In Background.as, I use BulkLoader to manage loading up the assets, and I fire off an onAllComplete event handler when BulkLoader finishes loading what I need it to load:

private function onAllComplete(e:Event):void
{
	trace(":: Background.onAllComplete() ::");
	// instantiate content background sprite
	_bgContent = new Sprite();
	// position the content background sprite
	_bgContent.x = BG_CONTENT_X;
	_bgContent.y = BG_CONTENT_Y;
	// draw content background sprite
	with (_bgContent)
	{
		graphics.beginFill(BG_CONTENT_COLOR);
		graphics.drawRect(0, 0, BG_CONTENT_WIDTH, BG_CONTENT_HEIGHT);
		graphics.endFill();
	}
	// get background bitmap
	_bgBitmap = e.target.getBitmap("background");
	// get team logo bitmap
	_teamLogoBitmap = e.target.getBitmap("team_logo");
	_teamLogoBitmap.x += BG_MARGIN_X;
	// get market logo bitmap
	_marketLogoBitmap = e.target.getBitmap("market_logo");
	_marketLogoBitmap.x = BG_WIDTH - _marketLogoBitmap.width - BG_MARGIN_X;
	_marketLogoBitmap.y += BG_MARGIN_Y;
	// set instance names
	_bgContent.name = "background_content";
	// add children to display list
	addChild(_bgBitmap);
	addChild(_teamLogoBitmap);
	addChild(_marketLogoBitmap);
	addChild(_bgContent);
	**[COLOR="Red"]// dispatch BG_COMPLETE event to Shell.as; settle bubbling to true
	dispatchEvent(new Event(BG_COMPLETE, true));[/COLOR]**
	// remove BulkLoader event listeners
	bgLoader.removeEventListener(BulkLoader.COMPLETE, onAllComplete);
	bgLoader.removeEventListener(BulkLoader.PROGRESS, onAllProgress);
}

That item in red NEVER gets dispatched to the event listener I have set up in Shell.as – UNLESS I change the earlier function I mentioned as follows:

// function to instantiate the Background.as class
private function loadBackground():void
{
	_background = new Background(
		_configData.market_logo_url,
		_configData.background_url,
		_configData.team_logo_url,
		_configData.sponsorship_logo_url,
		_configData.sponsorship_link
	);
	**[COLOR="red"]addChild(_background);[/COLOR]**
}

Why does _background have to be added to the document display list before the event can be dispatched? I’d like to have Shell.as listen for when everything is ready in Background.as and then tween it in or do something visual in the “onBackgroundComplete” event handler.

Thanks,
IronChefMorimoto

Events bubble up in their hierarchy, so for shell.as to listen to events dispatched within _background, background needs to be a child of shell.as. Just make background have everything load into an invisible container, then on the complete function change them around so that you can tween it all in. Or make it’s visibility 0 in shell.as and then when it’s all ready tween away.

[QUOTE=Iamthejuggler;2343404]Events bubble up in their hierarchy, so for shell.as to listen to events dispatched within _background, background needs to be a child of shell.as.[/QUOTE]

Cool, thanks. I’ll set the _background object alpha or visibility to something that keeps it from showing until the onBackgroundComplete dispatched event fires off.

Thanks,
IronChefMorimoto

Well – now I’ve run into another snag. Same scenario above, but this time, I’m adding an instance of Navigation.as to the stage:

// load navigation
private function loadNav():void
{
	// create new instance of Navigation.as
	// class w/ configuration XML parameters
	_navigation = new Navigation(
		[_configData.game_day_tab_label, _configData.team_info_tab_label, _configData.news_tab_label],
		_configData.tab_active_color_top,
		_configData.tab_active_color_bottom,
		_configData.tab_inactive_color_top,
		_configData.tab_inactive_color_bottom
	)
	// set _navigation visibility to false
	_navigation.visible = false;
	// add _background to document display list
	addChild(_navigation);
}

In Navigation.as, I dispatch a “navigation is complete” event to Shell.as, same as Background.as letting Shell.as know that it’s done loading. But for some reason, I cannot get the event handler in Shell.as to see the event from Navigation.as. I’ve checked my code – it’s basically the same sort of the thing described above. What am I missing? I know I’m adding the Navigation.as child to the document display list in Shell.as, so that’s not the same problem as I was having with Background.as:

private function renderNav():void
{
	trace(":: Navigation.renderNav() ::");
	var _nav_tab:NavTab;
	var _nav_shift:Number = 0;
	for (var i:uint = 0; i < _labelData.length; i++)
	{
		// create new instance of tab graphic sprite
		_nav_tab = new NavTab(_labelData*.toUpperCase(), _actColorTop, _actColorBottom, _inactColorTop, _inactColorBottom);
		// give each instance an incremental instance name
		_nav_tab.name = "nav_tab_" + i;
		// shift each new instance to the right to create overlapping tabs
		_nav_tab.x += _nav_shift;
		// incremement the cumulative x shift until you reach the last tab
		if (i < _labelData.length)
		{
			_nav_shift += TAB_SPACING_X;
		}
		// set the sprite button mode to true
		_nav_tab.buttonMode = true;
		_nav_tab.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
		_nav_tab.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		_nav_tab.addEventListener(MouseEvent.CLICK, onMouseClick);
		// add the tab instance to the display list
		addChild(_nav_tab);
	}
	**[COLOR="Red"]// dispatch NAV_COMPLETE event to Shell.as; set bubbling to true
	dispatchEvent(new Event(NAV_COMPLETE, true));[/COLOR]**
}

The event listener in Shell.as is set up as follows:

// event handler - "onNavComplete" custom event
private function onNavComplete(e:Event):void
{
	trace(":: Shell.onNavComplete() ::");
	// make _navigation visible when it is complete
	_navigation.visible = true;
}

I NEVER see this trace…?

IronChefMorimoto

Anyone have any idea WHY the Navigation.as dispatched event (NAV_COMPLETE constant) never reaches Shell.as? I’ve triple checked my code and cannot figure out why I can get Background.as to work properly the same way but not Navigation.as.

Thanks,
IronChefMorimoto