How to select the right button?

Hi all,

Does anyone know how to keep track of which button is selected?
I have 3 buttons and what I want to do is to change color button while selected it.
For now on, when clicked is selected the whole 3 buttons.

I made this code but couldn’t make it selected to right button. Any helps might appreciated.


package 
{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import caurina.transitions.Tweener;
	
	public class selectedButton extends Sprite
	{
		private var tabBtn:Array;
		private var button:buttonMC = new buttonMC();
		
		public function selectedButton()
		{
			tabBtn = new Array(button, button, button);
			initButton();
		}
		
		
		private function initButton():void
		{
			var xPos:uint = 0;
			var count:uint =0;
			
			for(var i=0;i<tabBtn.length;i++)
			{
				var button:buttonMC = new buttonMC();
				button.buttonMode = true;
				button.mouseChildren = false;
				button.name = "btn"+i;
				button.x = xPos;
				xPos += button.width+5;
				
				button.addEventListener(MouseEvent.MOUSE_DOWN, handleButton);
				button.addEventListener(MouseEvent.MOUSE_OUT, handleButton);
				button.addEventListener(MouseEvent.MOUSE_OVER, handleButton);

				addChild(button);
			}
		}
		
		
		private function handleButton(eventType:MouseEvent):void
		{
			var mc:MovieClip = eventType.target as MovieClip;
			switch(eventType.type)
			{
				case MouseEvent.MOUSE_DOWN :
					if(eventType.currentTarget.name == 'btn0') 
					{
						Tweener.addTween(mc, {_color:0x0000ff});
						mc.removeEventListener(MouseEvent.MOUSE_OUT, handleButton);
						mc.removeEventListener(MouseEvent.MOUSE_OVER, handleButton);
					} else if(eventType.currentTarget.name == 'btn1')
					{
						Tweener.addTween(mc, {_color:0x0000ff});
						mc.removeEventListener(MouseEvent.MOUSE_OUT, handleButton);
						mc.removeEventListener(MouseEvent.MOUSE_OVER, handleButton);
						
					} else if(eventType.currentTarget.name == 'btn2') 
					{
						Tweener.addTween(mc, {_color:0x0000ff});
						mc.removeEventListener(MouseEvent.MOUSE_OUT, handleButton);
						mc.removeEventListener(MouseEvent.MOUSE_OVER, handleButton);
					}
					return;
				case MouseEvent.MOUSE_OUT : 
					mc.buttonMode = false;
					Tweener.addTween(eventType.target, {_color:null, time:0.5, transition:"easeOutQuad"});
					return;
				case MouseEvent.MOUSE_OVER : 
					mc.buttonMode = false;
					Tweener.addTween(eventType.target, {_color:0xcccccc, time:0.5, transition:"easeOutQuad"});
					return;
			}
		}


U mean right Mouse button? It is impossible except contextMenu.
And one tip:

button.addEventListener(MouseEvent.MOUSE_DOWN, handleButtonDown);
				button.addEventListener(MouseEvent.MOUSE_OUT, handleButtonOut);
				button.addEventListener(MouseEvent.MOUSE_OVER, handleButtonOver);

Splitting functions is better since u do not need to use switch.
If u would create a dictionary or a local variable on buttons, ur handleButtonDown could be just

Tweener.addTween(mc, {mc.color});
//or Tweener.addTween(mc, {dict[mc]});
mc.removeEventListener(MouseEvent.MOUSE_OUT, handleButton);
mc.removeEventListener(MouseEvent.MOUSE_OVER, handleButton);

No, not the right Mouse Button.

I want to keep track of which button of my 3 buttons have been selected by assigning it with a different color. Like a currentButtonSelected and LastButtonSelected

[QUOTE=Felixz;2329040]U mean right Mouse button? It is impossible except contextMenu.
And one tip:

button.addEventListener(MouseEvent.MOUSE_DOWN, handleButtonDown);
				button.addEventListener(MouseEvent.MOUSE_OUT, handleButtonOut);
				button.addEventListener(MouseEvent.MOUSE_OVER, handleButtonOver);

Splitting functions is better since u do not need to use switch.
If u would create a dictionary or a local variable on buttons, ur handleButtonDown could be just

Tweener.addTween(mc, {mc.color});
//or Tweener.addTween(mc, {dict[mc]});
mc.removeEventListener(MouseEvent.MOUSE_OUT, handleButton);
mc.removeEventListener(MouseEvent.MOUSE_OVER, handleButton);

[/QUOTE]

Thanks a lot for this link, it’s very helpful!

Do you have an example applying with AS2?

[QUOTE=Felixz;2329581]http://www.kirupa.com/forum/showthread.php?t=297842[/QUOTE]

okay,

here’s what I did with my buttons, I also added a text field to it. For some reason, while clicking on each button the text field dissappears. The new color has been changed but the text field is now gone.

Does anyone know how to fix that?


package 
{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.geom.ColorTransform;
	
	public class ButtonManager extends Sprite
	{
		private var buttonCurrentSelected:buttonMC;

		private static const SPACE:uint = 1;

		
		public function ButtonManager()
		{
			createButton();
		}
		
		private function createButton():void
		{
			for(var i:uint=0;i < 4;i++)
			{
				buttonCurrentSelected = new buttonMC();
				buttonCurrentSelected.buttonMode = true;
				buttonCurrentSelected.mouseChildren = false;
				
				buttonCurrentSelected.x = (buttonCurrentSelected.width + SPACE) * i;
				buttonCurrentSelected.addChild(createTextField(buttonCurrentSelected, "Button"));
				
				
				buttonCurrentSelected.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);

				addChild(buttonCurrentSelected);
			}
		}
		
		private function createTextField(button:MovieClip, buttonText:String):TextField 
		{
			var btnText:TextField = new TextField();
			btnText.autoSize = "left";
			btnText.selectable = false;
			btnText.defaultTextFormat = initFormat();
			btnText.text = buttonText;
			
			btnText.x = (button.width - btnText.width) / 2;
			btnText.y = (button.height - btnText.height) / 2;
			return btnText;
		}
		
		
		private function initFormat():TextFormat
		{
			var textFormat:TextFormat = new TextFormat();
			textFormat.font = "Arial";
			textFormat.size = 12;
			textFormat.color = 0xffffff;
			return textFormat;
		}
		
		private function changeButtonColor(color:uint, button:buttonMC):void
		{
			var colorType:ColorTransform = new ColorTransform();
			colorType.color = color;
			button.transform.colorTransform = colorType;
		}
		
		
		private function handleMouseDown(eventType:MouseEvent):void
		{
			trace(buttonCurrentSelected);
			if (buttonCurrentSelected) 
			{
				buttonCurrentSelected.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
				changeButtonColor(0x999999, buttonCurrentSelected);
			}
			buttonCurrentSelected = buttonMC(eventType.currentTarget);
			buttonCurrentSelected.removeEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);
			changeButtonColor(0x0195fd, buttonCurrentSelected);
		}
	}
}