【问题标题】:AS3: Maintain Checkbox state when returning to a frame with checkboxesAS3:返回带有复选框的框架时保持复选框状态
【发布时间】:2014-09-10 16:12:39
【问题描述】:

所以我昨天问了一个关于如何控制某些复选框状态的问题: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. 贴出的代码的上下文是什么?主时间线的第一帧?别的地方?你的复选框在哪里?主时间轴(所有帧?)主时间轴上的特定帧或一组帧?
  • 脚本位于主时间线的第一帧,用于定义“地图”上可点击图像(按钮)的可见性,然后链接到其他帧,并提供有关点击图像的更多信息.目前,复选框存在于所有帧上,但在第一帧后不可见。这样做是为了保持他们的状态。如果我可以使用变量来保留按钮/图像可见性的状态,那么我完全打算将该代码添加到复选框中。
  • 所有的复选框都应该在第一帧可见吗?
  • 是的。基本上,复选框会打开地图上的所有图像/按钮。然后,用户单击这些按钮将被带到一个单独的框架,该框架包含有关他们单击的地图区域的信息。当您第一次运行该文件时,您会看到大量未选中的复选框和一个空白背景图。选中复选框会覆盖地图上的区域(图像形式的按钮)。

标签: actionscript-3 flash checkbox


【解决方案1】:

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

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();

【讨论】:

  • 非常感谢!总而言之,我一直试图让这最后一点工作的时间比提出整个其余代码的时间要长。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-14
  • 2012-08-25
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多