Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Oct 30, 2016 Oct 30, 2016

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?

TOPICS
ActionScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 01, 2016 Nov 01, 2016

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 = myCol

...
Translate
Community Expert ,
Oct 30, 2016 Oct 30, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 30, 2016 Oct 30, 2016

Is this okay?

red.addEventListener(MouseEvent.CLICK,ColorTransform);

function ColorTransform(e:MouseEvent): void{

}

What do I put in for the rest?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 30, 2016 Oct 30, 2016

red.addEventListener(MouseEvent.CLICK,ColorTransformF);

function ColorTransformF(e:MouseEvent): void{

var myColorTransform = ball.transform.colorTransform

myColorTransform.color = 0xFF0000;

ball.transform.colorTransform = myColorTransform;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 30, 2016 Oct 30, 2016

It gives me an output error:

TypeError: Error #1010: A term is undefined and has no properties.

  at Untitled_fla::MainTimeline/ColorTransformF()

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 31, 2016 Oct 31, 2016

click file>publish settings>and tick 'permit debugging'.  retest.

the line number with the undefined term will be in the error message allowing you to pinpoint the problem in your code.  if that doesn't make the solution to the problem obvious to you, copy and paste the line number mentioned in the error message and copy/paste the error message.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 31, 2016 Oct 31, 2016

It doesn't give me any Compiler Errors, just the Output error that I posted above.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 31, 2016 Oct 31, 2016

tick 'permit debugging' and retest (though if the only code you have is the code i suggested there are only two possiblities: you either don't have anything named red or you don't have anything named ball when the code executes).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 31, 2016 Oct 31, 2016

I did click on it, here is the Output code:

TypeError: Error #1010: A term is undefined and has no properties.

  at SphereRadioButtons_fla::MainTimeline/ColorTransformF()[SphereRadioButtons_fla.MainTimeline::frame1:5]

I did have problems with finding the objects and right instance names so I restarted, the ball is now Sphere, and the radio button is still red, so I changed the instance name for the code:

red.addEventListener(MouseEvent.CLICK,ColorTransformF);

function ColorTransformF(e:MouseEvent):void{

  var myColorTransform = Sphere.transform.colorTransform

  myColorTransform.color = 0xFF0000;

  Sphere.transfrom.colorTransform = myColorTransform;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 31, 2016 Oct 31, 2016

that error message indicates your error is on frame 1, line 5.

if that's:

Sphere.transfrom.colorTransform = myColorTransform;

it means Sphere is undefined.  actionscript is case-sensitive so sphere is not the same as Sphere.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 31, 2016 Oct 31, 2016

Okay, I redid the whole thing again and it finally works, but now when I do the same for the other two buttons it says duplicate function definition, and error "5000: The class 'fl.core.ComponentShim' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type." and "The class 'fl.controls.RadioButton' must sublass 'flash.display.MovieClip' since it is linked to a library symbol of that type."

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2016 Nov 01, 2016

you have a component problem (eg, you're using a non-as3 component or you edited one or more radio button symbols in your library) that's going to be difficult to pinpoint via a forum.

you should start over and create a new as3 fla, drag a radio button from the component panel to the stage and give it an instance name (in the properties panel) of red.  create a new movieclip, add it to the stage and assign an instance name of ball.  open the actionscript panel and copy and paste the code i suggested.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 01, 2016 Nov 01, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2016 Nov 01, 2016

your functions need different names.

(p.s when using the adobe forums, please mark helpful/correct responses, if there are any.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 01, 2016 Nov 01, 2016

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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 02, 2016 Nov 02, 2016

Thanks man, it works perfectly now

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 02, 2016 Nov 02, 2016
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines