Skip to main content
October 17, 2011
Answered

Change Color on mouse click

  • October 17, 2011
  • 1 reply
  • 651 views

Hi Guys.  I have followed Ned's code from another post, whereby if a text button is clicked, the color will change.  The code is like

var clicked:Boolean = false;

var clicked2:Boolean = false;

btn1.addEventListener(MouseEvent.CLICK, btn1click);

function btn1click(event:MouseEvent):void {

    clicked = true;

    var newColorTransform:ColorTransform = btn1.transform.colorTransform;

    if(clicked){

    newColorTransform.color = 0xc97f22;

    } else {

    newColorTransform.color = 0x000000;

    }

    btn1.transform.colorTransform = newColorTransform;

    gotoAndStop(5);

}

   

btn2.addEventListener(MouseEvent.CLICK, btn2click);

function btn2click(event:MouseEvent):void {

    clicked2 = true;

    var newColorTransform:ColorTransform = btn2.transform.colorTransform;

    if(clicked2){

    newColorTransform.color = 0xc97f22;

    } else {

    newColorTransform.color = 0x000000;

    }

    btn2.transform.colorTransform = newColorTransform;

    gotoAndStop(6);

}

Now if I click on the first button, it changes color and stays like that, which is perfect.  If I click on btn2, this changes color and stays like it.  The problem is that if btn2 is pressed, it should change color and stay like it (as it is doing), but btn1 should return back to its normal color (black).  I am not sure how to achieve this.


Any advise appreciated

Cheers

This topic has been closed for replies.
Correct answer Ned Murphy

When you mention something is from another post it is helpful if you provide a link so that the context of the code offered can be understood. What you choose as a solution might not be the optimum choice.

Use the following instead:

var newColorTransform:ColorTransform = new ColorTransform();

var btns:Array = new Array({btn:btn1, frameNum:5},{btn:btn2, frameNum:6});

for(var i:uint=0; i< btns.length; i++){
    btns.btn.addEventListener(MouseEvent.CLICK, btnclick);
}

function btnclick(evt:MouseEvent):void {
    var frameToGoTo:uint;
    for(var i:uint=0; i< btns.length; i++){
        if(evt.currentTarget == btns.btn){
            newColorTransform.color = 0xc97f22;
            frameToGoTo = btns.frameNum;
        } else {
            newColorTransform.color = 0x000000;
        }
        btns.btn.transform.colorTransform = newColorTransform;
    }
}

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
October 17, 2011

When you mention something is from another post it is helpful if you provide a link so that the context of the code offered can be understood. What you choose as a solution might not be the optimum choice.

Use the following instead:

var newColorTransform:ColorTransform = new ColorTransform();

var btns:Array = new Array({btn:btn1, frameNum:5},{btn:btn2, frameNum:6});

for(var i:uint=0; i< btns.length; i++){
    btns.btn.addEventListener(MouseEvent.CLICK, btnclick);
}

function btnclick(evt:MouseEvent):void {
    var frameToGoTo:uint;
    for(var i:uint=0; i< btns.length; i++){
        if(evt.currentTarget == btns.btn){
            newColorTransform.color = 0xc97f22;
            frameToGoTo = btns.frameNum;
        } else {
            newColorTransform.color = 0x000000;
        }
        btns.btn.transform.colorTransform = newColorTransform;
    }
}