manually set check box selected in as3 - actionscript-3

I have a CheckBox in as3 that i need to set as checked with code. Do I dispatch an event? If so, how do I create an event, and give it a target (target is read only). Or is there another way to do it? thanks.

my_checkbox.selected = true;
And of course:
my_checkbox.selected = false;
To handle the box being checked/unchecked:
my_checkbox.addEventListener(MouseEvent.CLICK, _selected);
function _selected(e:MouseEvent):void
{
var bool:Boolean = e.target.selected;
if(bool)
{
trace("was checked");
// more code
}
else
{
trace("was unchecked");
// more code
}
}

Related

Animate CC HTML5 canvas: how do I create a private/local variable in Actionscript?

I'm using a variable to create a toggle-effect on a function. The only problem is that this code changes the variable globally, and not locally for each object using the function. I know what I have to do, just not how :)
Function:
var count;
function animateTarget($target, $rwFrame) {
this.count = false;
if(!count == true)
{
$target.gotoAndPlay(2);
count=true;
}
else
{
$target.gotoAndPlay($rwFrame); //playback from a different frame
count=false;
}
}
Call:
this.Foo_Btn.addEventListener("click", animateTarget.bind(this, this.Foo_target, 34));
Actually, I got the answer from a friend (here with some updated variable names). Since it's JS, it reads the "clicked" as false, even though this is not stated. And by binding it to the object targetit will check for each separate object that uses this function.
var clicked;
function animateTarget(target, rwFrame) {
if(!target.clicked)
{
target.gotoAndPlay(2);
target.clicked = true;
}
else
{
target.gotoAndPlay(rwFrame);
}
}

Convert code sniplet gotoandstop to if else coondition

i just want to ask how can i make a code without needing the code sniplet gotoandstop when clicking button, i want to make if else statement where if he click this called "answerb.btn" he will go to the destined frame.
Well im thinking this code
If (answer.btn = onPress) {
gotoAndStop(2);
}
But somehow ita wrong code i dont know what code to use for pressing button. Pls enlighten me ty
You should have an event listener. Here is an example of the way you could to this:
button.addEventListener(MouseEvent.CLICK, onClick);
private function onClick(e:MouseEvent):void
{
//your logic here
this.gotoAndStop(2);
};
Here is an example from one of my projects which required text to be entered into blocks before proceeding, but it used if statements with buttons presses so you could modify it to suit your needs as you wish!
stop();
var names:String;
var id:String;
var supervisor:String;
namein.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
idin.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
supervisorin.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
beginbutton.enabled = false;
function paramChanged(event:TextEvent):void
{
if (namein.text != "" && idin.text != "" && supervisorin.text != "" &&
namein.length >=5 &&
idin.length >=5 &&
supervisorin.length >=5)//add your other fields here
{
beginbutton.enabled = true;
beginbutton.addEventListener(MouseEvent.CLICK,
fl_ClickToGoToAndPlayFromFrame);
}
else
{
beginbutton.enabled = false;
//If something changes that means we now fail the test you will want to disable the button again
}
}
function fl_ClickToGoToAndPlayFromFrame(event:MouseEvent):void
{
names = namein.text;
id = idin.text;
supervisor = supervisorin.text;
gotoAndPlay(15);
}

How do you stop a function from being executed and only execute on click in mootools

I'm new to Mootools and I have found that I have to use the click element but I'm not 100% sure where I am meant to put it in the below code:
function setInStockOption (labels, e) {
active = false;
labels.some (function (item,index) {
if(item.hasClass ('selected')) {
if(item.hasClass ('unavailable')) {
item.removeClass('selected');
item.addClass ('unselected');
active = true;
} else {
return true;
}
}
if(active) {
if (!item.hasClass ('unavailable')) {
e.target = this;
item.fireEvent ('click', e);
active = false;
return true;
}
}
});
}
window.addEvent('load', function(e) {
var labels = $$('div.option-radios label.radio');
setInStockOption(labels, e);
});
I basically need to add the class selected on click instead. At the moment this script is adding the selected class to the first child of Radio in the html and then when you click on others it'll add the class selected. I basically want all the classes to be unselected when the page loads
.
Any ideas?
You'll want something like this:
window.addEvent('domready', function(e) {
$$('div.option-radios label.radio').each(function(label, i) {
label.addEvent('click', function(event) {
event.target.toggleClass('selected');
});
});
});
Note that this uses the Array.each method instead of Array.some. The latter doesn't do what you expect. It then registers a click event on every label which simply toggles the selected class on the event target.
Then you can add other initialization code to the each loop and more logic to the click event handler.
I also used the domready event which is usually preferred over load since it fires earlier.
Here's a fiddle to play around with.

How to create two buttons: when one is pushed other goes up

How to create two buttons: one button is pushed other goes up. I tried using this:
//Create male/female button objects
var maleButton:maleButtonObejct = new maleButtonObject();
var femaleButton:femaleButtonObject = new femaleButtonObject();
//Add evnet listeners to both buttons
maleButton.addEventListener(MouseEvent.CLICK, setToMale);
femaleButton.addEventListener(MouseEvent.CLICK, setToFemale);
//Create isMale variable
var isMale:Boolean;
//Male/Female button functions
function setToMale(event:MouseEvent):void {
isMale = true;
maleButton.stop(3);
femaleButton.stop(1);
}
function setToFemale(event:MouseEvent):void {
isMale = false;
femaleButton.stop(3);
maleButton.stop(1);
}
I get runtime error: TypeError: Error #1006: stop is not a function.
at Untitled_fla::MainTimeline/setToFemale()
Rename your classes starting with a capital letter, such as MaleButton.
Here is an easy way to accomplish what you want:
var maleButton:MaleButton = new MaleButton();
var femaleButton:FemaleButton = new FemaleButton();
maleButton.addEventListener(MouseEvent.CLICK, clicked);
maleButton.addEventListener(MouseEvent.CLICK, clicked);
function clicked(event:MouseEvent):void
{
switch(event.target)
{
case maleButton:
maleButton.gotoAndStop(3);
femaleButton.gotoAndStop(1);
break;
case femaleButton:
maleButton.gotoAndStop(1);
femaleButton.gotoAndStop(3);
break;
default:
return;
}
}

Actionscript 3 drop down menu link error

I'm having issues with the following menu. I've been adapting a script from kirupa into actionscript 3.
When I get to the last level of the menu it won't link properly. It always takes the last url of the bunch as the url for all links in that branch of the tree.
Can anyone help me get it to link properly? A zip with the fla and the xml can be found at the following link.
http://www.jdviz.com/projects/xmlmenu.zip
Thanks,
There's a problem with the closures at the end of the code. The current button is not identified properly.
if (node_xml.childNodes[i].nodeName != "resource") {
//cleared the code for clarity...
} else {
curr_item.arrow.visible = false;
curr_item.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void {
trace(curr_item.urlLink);
});
}
change the above to:
var currentButton:MenuItem_mc = new MenuItem_mc();
function mouseOverHandler(e:MouseEvent ):void
{
currentButton = e.currentTarget as MenuItem_mc;
currentButton.addEventListener( MouseEvent.CLICK , clickHandler );
}
function clickHandler(e:MouseEvent):void
{
var btn:MenuItem_mc = event.currentTarget as MenuItem_mc;
trace( btn.urlLink );
}
if (node_xml.childNodes[i].nodeName != "resource") {
//cleared the code for clarity...
} else {
curr_item.arrow.visible = false;
curr_item.addEventListener(MouseEvent.MOUSE_DOWN, mouseOverHandler );
}