Make a checkbox act like a radio button

Actionscript 3 checkbox act liek a radio button

Page 1 of 1

2 Replies - 5763 Views - Last Post: 23 January 2010 - 12:35 PM

#1 vicodon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-December 09

Make a checkbox act like a radio button

Posted 22 January 2010 - 02:44 PM

Hello,

I'm trying to make a checkbox act like a radio (yes i know its better to use radio) but for now i need to do this. The code im attaching is throwing an error and not sure why. I'm somewhat new to action script. Here is the code and the error i get...

ERROR

1118: Implicit coercion of a value with static type Object to a possibly unrelated type fl.controls:CheckBox.

It happened on this line

latest = e.currentTarget;

setupCheckBoxesSolv();

function setupCheckBoxesSolv()
{
		for (var m:Number = 0; m < solvent.length; m++)
		{
				this["cb" + m] = new CheckBox();
				this["cb" + m].label = solvent[m].dispName;
				this["cb" + m].name = "Solv" + m;
				this["cb" + m].addEventListener(MouseEvent.CLICK, deselectBox);
				addChild(this["cb" + m]);
				this["cb" + m].x = 280;
				this["cb" + m].y = offset + m * 30;

		}
}
var latest:CheckBox;
function deselectBox(e:Event):void
{
if(latest != null) latest.selected = false;
latest = e.currentTarget;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Make a checkbox act like a radio button

#2 longbar  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 12
  • Joined: 24-November 09

Re: Make a checkbox act like a radio button

Posted 23 January 2010 - 11:16 AM

Here's the thing: you've declared the variable latest as type CheckBox. But when you try to assign a new object to that variable in your event handler, there's no guarantee that the currentTarget of your event is of type CheckBox. If you've coded things properly, you know it's going to be right, but there's nothing in the code that requires the types to match up.

If you're confident that the type of the event target is always going to be CheckBox, and you want to override Flash's error and basically tell the compiler, "No really, I know this is a CheckBox, trust me," then you can do something like this:

var latest:CheckBox;
function deselectBox(e:Event):void
{
if(latest != null) latest.selected = false;
latest = e.currentTarget as CheckBox;
}



By typing "as CheckBox" you essentially typecast e.currentTarget as a CheckBox, and Flash should be happy (as long as it really is getting a CheckBox!).

(When you have a moment, read up a little on type in ActionScript 3, using "as" and so forth. Understanding how type is handled in AS3 will help a lot.)
Was This Post Helpful? 0
  • +
  • -

#3 vicodon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-December 09

Re: Make a checkbox act like a radio button

Posted 23 January 2010 - 12:35 PM

thanks a ton
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1