2014-09-10 69 views
1

幀時保持複選框狀態所以昨天我問了一下如何控制這裏的一些複選框的狀態的問題:Getting Checkboxes to retain their state on return to frameAS3:返回與複選框

然而,這引起了另一個問題。雖然複選框的值保留,但它們與它們表示的對象之間的關係不是..例如,在返回到主屏幕時,複選框值將填充爲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(); 
+0

需要一些額外的信息。 1.發佈代碼的上下文是什麼?主時間軸的第一幀?別的地方?你的複選框在哪裏住?主時間線(所有幀?)主時間軸上的特定幀或幀集? – BadFeelingAboutThis 2014-09-10 18:17:29

+0

該腳本位於主時間軸的第一幀,用於定義「地圖」上的可點擊圖片(按鈕)的可見性,然後鏈接到其他幀,並提供有關所點擊圖片的更多信息。 目前,複選框位於所有框架上,但在第一幀之後不可見。這樣做是爲了保持他們的狀態。如果我可以使用變量來包裝頭部以保持按鈕/圖像可見性的狀態,那麼我完全打算將該代碼添加到複選框。 – nicebikemate 2014-09-10 18:23:31

+0

所有的複選框應該在第一幀顯示嗎? – BadFeelingAboutThis 2014-09-10 18:30:08

回答

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

非常感謝!總而言之,我一直試圖讓這最後一點工作的時間比完成剩下的代碼所花費的時間更長。 很多apreciated – nicebikemate 2014-09-10 19:01:06