AS3:返回带复选框的框架时保持复选框状态

时间:2014-09-10 16:12:40

标签: actionscript-3 flash checkbox

所以昨天我问了一个关于如何控制一些复选框状态的问题:Getting Checkboxes to retain their state on return to frame

然而,这引起了另一个问题。虽然保留了复选框的值,但它们与它们所代表的对象的关系不是..例如,在返回主屏幕时,复选框值将填入为true,但该复选框表示的按钮将不可见直到我取消选中并重新检查该框。

我一直在尝试将复选框的布尔值存储在一个变量中,然后在返回到屏幕时重新使用它,但我只是不太了解语法以使其工作。

查看下面的代码我想知道是否因为我将代码开头的按钮可见性的默认状态设置为false?我是否需要获取Area_1_Btn.visible以检查布尔状态?

任何帮助都非常感激,因为我对我缺乏理解感到越来越沮丧。

import flash.events.Event;

/* Ensures that all checkboxes begin in the off state.*/
Area_1_Btn.visible = false;
Area_1_Chk.visible = true;
Area_2_Btn.visible = false;
Area_2_Chk.visible = true;
ShowAll_Chk.visible = true;

/* Defines the Show All Checkbox and sets states to true/false*/
ShowAll_Chk.addEventListener(Event.CHANGE, toggleMulti, false, 0, true);
function toggleMulti(e:Event):void
{
    var SAC:Boolean = e.target.selected;
    if (SAC)
    {
        Area_1_Chk.selected = true;
        Area_1_Btn.visible = true;
        Area_2_Chk.selected = true;
        Area_2_Btn.visible = true;
    }
    else
    {
        Area_1_Chk.selected = false;
        Area_1_Btn.visible = false;
        Area_2_Chk.selected = false;
        Area_2_Btn.visible = false;
    }
}

/* Toggles the button state*/
function toggleArea_1_Btn(e:Event):void
{
    var A1B:Boolean = e.target.selected;
    if (A1B)
    {
        Area_1_Btn.visible = true;
    }
    else
    {
        Area_1_Btn.visible = false;
    }
    /* previous method for toggling button state*/
}
function toggleArea_2_Btn(e:Event):void
{
    Area_2_Chk.selected ? Area_2_Btn.visible = true:Area_2_Btn.visible = false;
}

/* Listens to the state of the checkbox and switches the button on*/
Area_1_Chk.addEventListener(Event.CHANGE, toggleArea_1_Btn, false, 0, true);
Area_2_Chk.addEventListener(Event.CHANGE, toggleArea_2_Btn, false, 0, true);

/* Listens for a mouse click and then instructions function below*/
Area_1_Btn.addEventListener(MouseEvent.CLICK, A1_ClickToGoToAndStopAtFrame);
Area_2_Btn.addEventListener(MouseEvent.CLICK, A2_ClickToGoToAndStopAtFrame);

function A1_ClickToGoToAndStopAtFrame(event:MouseEvent):void
{
    gotoAndStop(2);
}
function A2_ClickToGoToAndStopAtFrame(event:MouseEvent):void
{
    gotoAndStop(3);
}

stop();

1 个答案:

答案 0 :(得分:0)

以下是一些可以解决问题的优化和补充:

import flash.events.Event;
import flash.events.MouseEvent;

//a method you can call to set the visibility of all checkbox related items. Pass in false to hide them all
function showCheckBoxes(val:Boolean = true):void {
    Area_1_Chk.visible = val;
    Area_2_Chk.visible = val;
    ShowAll_Chk.visible = val;
    Area_1_Btn.visible = val;
    Area_2_Btn.visible = val;

    if(val) setButtonVisibility();  //if showing, show/hide the buttons based off the check box value
}

//this will update the button visibility based off the checkbox selected value.
function setButtonVisibility(e:Event = null):void {
    Area_1_Btn.visible = Area_1_Chk.selected;
    Area_2_Btn.visible = Area_2_Chk.selected;
}

function toggleMulti(e:Event):void {
    Area_1_Chk.selected = ShowAll_Chk.selected;
    Area_2_Chk.selected = ShowAll_Chk.selected;
    setButtonVisibility();
}

function buttonClick(e:Event):void {
    showCheckBoxes(false); //hide everything since we're moving to a new frame

    switch(e.currentTarget) {   //e.currentTarget is the button that was clicked
        case Area_1_Btn:
            gotoAndStop(2);
            break;

        case Area_1_Btn:
            gotoAndStop(3);
            break;
    }
}

//add all the listeners
ShowAll_Chk.addEventListener(Event.CHANGE, toggleMulti, false, 0, true);
Area_1_Chk.addEventListener(Event.CHANGE, setButtonVisibility, false, 0, true);
Area_2_Chk.addEventListener(Event.CHANGE, setButtonVisibility, false, 0, true);
Area_1_Btn.addEventListener(MouseEvent.CLICK, buttonClick);
Area_2_Btn.addEventListener(MouseEvent.CLICK, buttonClick);

showCheckBoxes(); //call this right now so the visibility updates when the frame loads.

stop();
相关问题