No "enabled" property for Sprite - how to disable?

I’ve made a Navigation.as class that generates a tabbed nav bar. The class builds itself out using a graphical tab class, NavTab.as. I have everything working, except for disabling the mouse events on an active tab.

Here are the tab creation function and event handlers for the NavTab.as instances inside Navigation.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);
	}
	// dispatch NAV_COMPLETE event to Shell.as; settle bubbling to true
	dispatchEvent(new Event(NAV_COMPLETE, true));
}

public function onMouseOver(e:MouseEvent):void
{
	trace(":: Navigation.onMouseOver() ::");
	// show the active background on the NavTab instance
	e.currentTarget.getChildByName("background_active").visible = true;
}

public function onMouseOut(e:MouseEvent):void
{
	trace(":: Navigation.onMouseOut() ::");
	// hide the active background on the NavTab instance
	e.currentTarget.getChildByName("background_active").visible = false;
}
		
public function onMouseClick(e:MouseEvent):void
{
	trace(":: Navigation.onClick() ::");
			
	if (currentTab)
	{
		lastTab = currentTab;
	}
	currentTab = e.currentTarget.name;
	trace(":: Navigation.onClick() : currentTab : " + currentTab + " ::");
	trace(":: Navigation.onClick() : lastTab : " + lastTab + " ::");
	dispatchEvent(new Event(NAV_CLICK, true));
}

How do I disable a given tab once I’ve clicked on it? With MovieClips, I had the “enabled” property, but NavTab.as extends Sprite, as does Navigation.as. I tried doing this in the onMouseClick event handler, but I still got rollover events:

e.currentTarget.mouseEnabled = false

Suggestions are welcome and appreciated.

Regards,
IronChefMorimoto

Why not just remove all the eventlisteners when you click on the tab?

I found this thread, but I’m not sure how I would apply mouseChildren in my case. I tried applying it to the onClick currentTarget, to no avail.

Anyone?

IronChefMorimoto

[QUOTE=Iamthejuggler;2343523]Why not just remove all the eventlisteners when you click on the tab?[/QUOTE]

I need to re-visit this code, now that I think more about it. I have no way to disable one of the tabs and then re-enable the remaining 2 tabs.

Is it better practice to remove event listeners and re-add them when re-enabling a Sprite vs. using the mouseChildren property?

IronChefMorimoto

I don’t know anything about that mouseChildren property to be honest. But it would be pretty simple to remove the eventlisteners on click of that tab, and then just add them again if you want to return functionality to the tab. Also removing eventListeners is good practice as they are one of the main causes for memory leaks.

e.currentTarget.mouseEnabled = e.currentTarget.mouseChildren = false

Yeah, Felixz has the answer, you got it half right, you just needed to make sure that [FONT=“Courier New”]mouseChildren = false[/FONT] too.

And I can just turn this on/off as needed without having to add/remove event listeners dynamically?

IronChefMorimoto

Yup. Just make sure you turn both settings on and off.

[QUOTE=Anogar;2343700]Yup. Just make sure you turn both settings on and off.[/QUOTE]

Well – this works for disabling the mouse functionality of the NavTab.as classes inside Navigation.as, but…

…the moment I click, I can’t set the “active” background from the NavTab.as graphic class instance to be visible:


private function onMouseClick(e:MouseEvent):void
{
	trace(":: Navigation.onClick() ::");
			
	e.currentTarget.mouseChildren = e.currentTarget.mouseEnabled = false;
	e.currentTarget.getChildByName("background_active").visible = true;
	
	if (currentTab)
	{
		lastTab = currentTab;
	}
	currentTab = e.currentTarget.name;
	trace(":: Navigation.onClick() : currentTab : " + currentTab + " ::");
	trace(":: Navigation.onClick() : lastTab : " + lastTab + " ::");
	dispatchEvent(new Event(NAV_CLICK, true));
}

The “active” background sprite in the NavTab.as instance remains visible = false.

Any suggestions? BTW – thanks to all who have chimed in here.

IronChefMorimoto

I found a solution to the problem noted above. I would appreciate any critique of the method I used to resolve it.

Basically, I threw all the repetitive NavTab.as instances into an array – soooo much easier to reference things that way (thanks to those who asked that question elsewhere). Then I came up with this onMouseClick event handler:

private function onMouseClick(e:MouseEvent):void
{
	trace(":: Navigation.onClick() ::");
	// had to do all this to make the buttons NOT cancel out the visibility of the "background_active" sprite
	e.currentTarget.mouseEnabled = e.currentTarget.mouseChildren = false;
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
	e.currentTarget.getChildByName("background_active").visible = true;
			
	for (var i:uint = 0; i < _navItems.length; i++)
	{
		if (e.currentTarget.name != _navItems*.name)
		{
			_navItems*.mouseEnabled = _navItems*.mouseChildren = true;
			_navItems*.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			_navItems*.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_navItems*.getChildByName("background_active").visible = false;
		}
	}
			
	if (currentTab)
	{
		lastTab = currentTab;
	}
	currentTab = e.currentTarget.name;
	trace(":: Navigation.onClick() : currentTab : " + currentTab + " ::");
	trace(":: Navigation.onClick() : lastTab : " + lastTab + " ::");
	dispatchEvent(new Event(NAV_CLICK, true));
}

Basically – had to do BOTH the removal of the event listeners AND the mouse disabling. Why is that?

Thanks,
IronChefMorimoto