Skip to main content
Known Participant
October 30, 2016
Answered

How to use Radio Buttons to make an object change colours?

  • October 30, 2016
  • 1 reply
  • 1482 views

I need to make a bouncing ball animation for a uni assignment, and then have 3 radio buttons that you can click to make the ball change to either blue, red or green.

I have put three Radio Button components on the stage with the instance names of "red" "blue" and "green", and the instance name of the object I want to change is "ball".

I know to transform an object's colour you use this code:

var myColorTransform = new ColorTransform ();

myColorTransform.color = 0xFF0000;

ball.transform.colorTransform = myColorTransform;

but how do I "attach" this to the radio button?

This topic has been closed for replies.
Correct answer kglad

Yeah that makes the red one work perfectly fine, but I need two more buttons that changes that ball to green and blue aswell, and when I use that code it says duplicate function.


or you could use:

red.addEventListener(MouseEvent.CLICK,ColorTransformF);

green.addEventListener(MouseEvent.CLICK,ColorTransformF);

blue.addEventListener(MouseEvent.CLICK,ColorTransformF);

function ColorTransformF(e:MouseEvent): void{

var myColorTransform = ball.transform.colorTransform;

if(e.currentTarget.name=='red'){

myColorTransform.color = 0xFF0000;

} else if(e.currentTarget.name=='green'){

myColorTransform.color=0x00ff00;

} else {

myColorTransform.color=0x0000ff;

}

ball.transform.colorTransform = myColorTransform;

}

1 reply

kglad
Community Expert
Community Expert
October 30, 2016

create radio button listens and add your transforms to the listener functions.

Known Participant
October 30, 2016

Is this okay?

red.addEventListener(MouseEvent.CLICK,ColorTransform);

function ColorTransform(e:MouseEvent): void{

}

What do I put in for the rest?

kglad
Community Expert
Community Expert
October 31, 2016

red.addEventListener(MouseEvent.CLICK,ColorTransformF);

function ColorTransformF(e:MouseEvent): void{

var myColorTransform = ball.transform.colorTransform

myColorTransform.color = 0xFF0000;

ball.transform.colorTransform = myColorTransform;

}