views:

122

answers:

2

I am trying to make button panel. each button have two type btn_home and btn_home_white I am trying to reach those buttons.It is work if I write for each button their own methods like

btn_home.addEventListener(MouseEvent.MOUSE_OVER,overEffect);
btn_home_white.addEventListener(MouseEvent.MOUSE_OUT,outEffect);

   function overEffect(e:MouseEvent)
{
var myTweenHight:Tween = new Tween( btn_home,"height",Bounce.easeOut,25,0,3,true);

var myTweenHight2:Tween = new        Tween(btn_home_white,"height",Bounce.easeOut,0,25,3,true);
var myTweenAlpha:Tween = new Tween(btn_home_white,"alpha",Strong.easeOut,0,1,2,true);

}

function outEffect(e:MouseEvent)
{
var myTweenHight:Tween = new Tween btn_home,"height",Bounce.easeOut,0,25,3,true);

var myTweenHight2:Tween = new Tween(btn_home_white,"height",Bounce.easeOut,25,0,3,true);
var myTweenAlpha:Tween = new Tween(btn_home_white,"alpha",Strong.easeOut,1,0,2,true);

}

But I have 10 buttons as btn_buttonname and btn_buttonname_white . I tryed to create Event listener on stage for all.It works for firts type buttons btn_buttonname but How can I get second type buttons? I tryed e.target["_white"] but it does not work .

stage.addEventListener(MouseEvent.MOUSE_OVER , overEffect);
stage.addEventListener(MouseEvent.MOUSE_OUT , outEffect);

function overEffect(e:MouseEvent)
{
var myTweenHight:Tween = new Tween(e.target,"height",Bounce.easeOut,25,0,3,true);
trace("height");
var myTweenHight2:Tween = new        Tween(e.target["_white"],"height",Bounce.easeOut,0,25,3,true);
var myTweenAlpha:Tween = new Tween(e.target["_white"],"alpha",Strong.easeOut,0,1,2,true);

}

function outEffect(e:MouseEvent)
{
var myTweenHight:Tween = new Tween(e.target,"height",Bounce.easeOut,0,25,3,true);

var myTweenHight2:Tween = new Tween(e.target["_white"],"height",Bounce.easeOut,25,0,3,true);
var myTweenAlpha:Tween = new Tween(e.target["_white"],"alpha",Strong.easeOut,1,0,2,true);

}

EDIT: I made it work as Daniel Carvalho sad adding for each buttons event listener.Now it works but if I move out mouse on button to other button previous button does not turn original form .

btn_home.addEventListener(MouseEvent.MOUSE_OVER,overEffect);
btn_home_white.addEventListener(MouseEvent.MOUSE_OUT,outEffect);
btn_contact.addEventListener(MouseEvent.MOUSE_OVER,overEffect);
btn_contact_white.addEventListener(MouseEvent.MOUSE_OUT,outEffect);
btn_menu.addEventListener(MouseEvent.MOUSE_OVER,overEffect);
btn_menu_white.addEventListener(MouseEvent.MOUSE_OUT,outEffect);
btn_restaurant.addEventListener(MouseEvent.MOUSE_OVER,overEffect);
btn_restaurant_white.addEventListener(MouseEvent.MOUSE_OUT,outEffect);
btn_rezervation.addEventListener(MouseEvent.MOUSE_OVER,overEffect);
btn_rezervation_white.addEventListener(MouseEvent.MOUSE_OUT,outEffect);
function overEffect(e:MouseEvent)
{
switch (e.target)
{
    case ( btn_home) :
        {
            var HomeTweenHight:Tween = new Tween(btn_home,"height",Bounce.easeOut,25,0,0.5,true);

            var HomeTweenHight2:Tween = new Tween(btn_home_white,"height",Bounce.easeOut,0,25,0.5,true);
            var HomeTweenAlpha:Tween = new Tween(btn_home_white,"alpha",Strong.easeOut,0,1,2,true);
            break;

        };
    case ( btn_contact) :
        {
            var ContactTweenHight:Tween = new Tween(btn_contact,"height",Bounce.easeOut,25,0,0.5,true);
            var ContactTweenHight2:Tween = new Tween(btn_contact_white,"height",Bounce.easeOut,0,25,0.5,true);
            var ContactTweenAlpha:Tween = new Tween(btn_contact_white,"alpha",Strong.easeOut,0,1,2,true);
            break;

        };
    case ( btn_menu) :
        {
            var MenuTweenHight:Tween = new Tween(btn_menu,"height",Bounce.easeOut,25,0,0.5,true);
            var MenuTweenHight2:Tween = new Tween(btn_menu_white,"height",Bounce.easeOut,0,25,0.5,true);
            var MenuTweenAlpha:Tween = new Tween(btn_menu_white,"alpha",Strong.easeOut,0,1,2,true);
            break;

        };
    case ( btn_restaurant) :
        {
            var RestourantTweenHight:Tween = new Tween(btn_restaurant,"height",Bounce.easeOut,25,0,0.5,true);
            var RestourantTweenHight2:Tween = new Tween(btn_restaurant_white,"height",Bounce.easeOut,0,25,0.5,true);
            var RestourantTweenAlpha:Tween = new Tween(btn_restaurant_white,"alpha",Strong.easeOut,0,1,2,true);
            break;

        };
    case (btn_rezervation) :
        {
            var RezervationTweenHight:Tween = new Tween(btn_rezervation,"height",Bounce.easeOut,25,0,0.5,true);
            var RezervationTweenHight2:Tween = new Tween(btn_rezervation_white,"height",Bounce.easeOut,0,25,0.5,true);
            var RezervationTweenAlpha:Tween = new Tween(btn_rezervation_white,"alpha",Strong.easeOut,0,1,2,true);
            break;

    }
}
};

function outEffect(e:MouseEvent)
{
switch (e.target)
{

    case (btn_home_white) :
        {
            var HomeTweenHight:Tween = new Tween(btn_home,"height",Bounce.easeOut,0,25,0.5,true);
            var HomeTweenHight2:Tween = new Tween(btn_home_white,"height",Bounce.easeOut,25,0,0.5,true);
            var HomeTweenAlpha:Tween = new Tween(btn_home_white,"alpha",Strong.easeOut,1,0,2,true);
            break;


        };
    case (btn_contact_white) :
        {
            var ContactTweenHight:Tween = new Tween(btn_contact,"height",Bounce.easeOut,0,25,0.5,true);
            var ContactTweenHight2:Tween = new Tween(btn_contact_white,"height",Bounce.easeOut,25,0,0.5,true);
            var ContactTweenAlpha:Tween = new Tween(btn_contact_white,"alpha",Strong.easeOut,1,0,2,true);
            break;


    }
    case (btn_menu_white) :
        {
            var MenuTweenHight:Tween = new Tween(btn_menu,"height",Bounce.easeOut,0,25,0.5,true);
            var MenuTweenHight2:Tween = new Tween(btn_menu_white,"height",Bounce.easeOut,25,0,0.5,true);
            var MenuTweenAlpha:Tween = new Tween(btn_menu_white,"alpha",Strong.easeOut,1,0,2,true);
            break;


    };
    case (btn_restaurant_white) :
        {
            var RestourantTweenHight:Tween = new Tween(btn_restaurant,"height",Bounce.easeOut,0,25,0.5,true);
            var RestourantTweenHight2:Tween = new Tween(btn_restaurant_white,"height",Bounce.easeOut,25,0,0.5,true);
            var RestourantTweenAlpha:Tween = new Tween(btn_restaurant_white,"alpha",Strong.easeOut,1,0,2,true);
            break;


    };
    case (btn_rezervation_white) :
        {
            var RezervationTweenHight:Tween = new Tween(btn_rezervation,"height",Bounce.easeOut,0,25,0.5,true);
            var RezervationTweenHight2:Tween = new Tween(btn_rezervation_white,"height",Bounce.easeOut,25,0,0.5,true);
            var RezervationTweenAlpha:Tween = new Tween(btn_rezervation_white,"alpha",Strong.easeOut,1,0,2,true);
            break;


    };

}
};
+1  A: 

Your e.target should work, though you don't (as far as I can see) need the [_"white"] after e.target for your _white buttons. The reason the e.targets aren't working is because you're just adding event listeners to the stage, and not the individual buttons.

I see two choices (perhaps there are others): manually go through writing event listeners for each button, or put all the buttons that will use the overEffect function into one Array, and all the buttons using the outEffect function into another Array.

Then create two for loops, and go through those Arrays adding the event listeners to each child of the array. Your e.target code should work at that point.

Let me know if you have any trouble with that, hope it helps.

debu

debu
I agree with this, but really, I recommend just adding event listeners to each button. There are only 10 buttons, so it's really unnecessary to make it any more complicated. Do it very much like your first code block Meko, buttonname.addEventListener etc...
Daniel Carvalho
Yeah I agree, it's probably overkill to use Arrays for this number of objects. Becomes more concise if you're using a lot more than 10 though.
debu
A: 

Hi,

Here is a quick example that may help you:

http://wonderfl.net/c/vhay

function createButton():MovieClip{
    var button:MovieClip=new MovieClip();
    var stateA:Shape=new Shape();
    var g:Graphics = stateA.graphics;
    g.beginFill(0xFF0000);
    g.drawRect(0, 0, 10, 10);
    g.endFill();
    button["stateA"] = stateA;
    button.addChild(stateA);
    var stateB:Shape=new Shape();
    g = stateB.graphics;
    g.beginFill(0x00FF00);
    g.drawRect(0, 0, 10, 10);
    g.endFill();
    button["stateB"] = stateB;
    button.addChild(stateB);
    stateB.visible=false;
    return button;
}
var nbButtons:int=10;
var buttons:Array=[];
var selectButton:MovieClip;
for (var i : int = 0;i < nbButtons; i++) {
    var button:MovieClip=createButton();
    addChild(button);
    button.y=10;
    button.x=i*12+10;
    addChild(button);
    buttons.push(button);
    setUpButton(button);
}
function setUpButton(button:MovieClip):void {
    button.buttonMode=true;
    button.addEventListener(MouseEvent.ROLL_OVER, buttonRollOver);
    button.addEventListener(MouseEvent.ROLL_OUT, buttonRollOut);
    button.addEventListener(MouseEvent.CLICK, buttonClick);
}
function buttonClick(e:Event):void{
    switchButtonStateToNotSelected();
    switchButtonStateToSelected(e.currentTarget as MovieClip);
}
function switchButtonStateToSelected(button:MovieClip):void{
    button.buttonMode=true;
    button.removeEventListener(MouseEvent.ROLL_OVER, buttonRollOver);
    button.removeEventListener(MouseEvent.ROLL_OUT, buttonRollOut);
    button.removeEventListener(MouseEvent.CLICK, buttonClick);
    button["stateA"]["visible"]=false;
    button["stateB"]["visible"]=true;
    selectButton=button;
}
function switchButtonStateToNotSelected():void{
    if(selectButton) {
        setUpButton(selectButton);
        selectButton["stateA"]["visible"]=true;
        selectButton["stateB"]["visible"]=false;
        selectButton=null;
    }
}
function buttonRollOver(e:Event):void{
    e.currentTarget["stateA"]["visible"]=false;
    e.currentTarget["stateB"]["visible"]=true;
}
function buttonRollOut(e:Event):void{
    e.currentTarget["stateA"]["visible"]=true;
    e.currentTarget["stateB"]["visible"]=false;
}
OXMO456