2015-12-01 19 views
0

我有7個按鈕(btn_one,btn_two,btn_three,...),當它們全部被點擊時(不是連續的,只是隨機的),我想先去第一個在下一個場景中的幀。我的代碼應該是什麼樣子?點擊了7個按鈕後的ActionScript 3轉到下一個場景

謝謝!

+0

你到目前爲止有什麼? – DonO

+0

只是每個按鈕的事件監聽器和btn_one的第一個函數(gotoAndPlay(1,「Scene2」)。我不知道我是怎麼開始的,到現在爲止我做了簡單的函數;-) – 3iskalt

回答

0
const buttons:Array = [btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_seven]; 

// dictionary to keep clicks state 
const clicked:Dictionary = new Dictionary(); 

function buttonClickHandler(event:Event):void { 
    // record specific button's click to a dictionary 
    clicked[event.currentTarget] = true; 

    // true, if all the dict keys have true values 
    const allButtonsClicked:Boolean = buttons.every(
     function(button:Object, ...rest):Boolean { 
      return clicked[button]; 
     }); 

    if (allButtonsClicked) { 
     // remove all listeners 
     for each (var button:DisplayObject in buttons) { 
      button.removeEventListener(MouseEvent.CLICK, buttonClickHandler); 
     } 

     //do whatever action you need. For example: 
     gotoAndStop(/*neededFrame*/); 
    } 
} 

// initialize dictionary values, and add listeners 
for each (var button:DisplayObject in buttons) { 
    clicked[button] = false; 
    button.addEventListener(MouseEvent.CLICK, buttonClickHandler); 
} 

還沒有測試過,但它應該工作。無論如何,該計劃應該是有效的。

+0

謝謝非常多,leetwinski。 – 3iskalt

+0

如果這些按鈕是動畫片段(動態),則可以在它們上面設置動態屬性(單擊)並放棄字典。 – BadFeelingAboutThis

+0

問問代碼,你應該明白了!在SO的免費代碼日.... – BotMaster

0
stop(); 

const buttons:Array = [btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_seven]; 

// dictionary to keep clicks state 
const clicked:Dictionary = new Dictionary(); 

function buttonClickHandler(event:Event):void { 
    // record specific button's click to a dictionary 
    clicked[event.currentTarget] = true; 

    // true, if all the dict keys have true values 
    const allButtonsClicked:Boolean = buttons.every(
     function(button:Object, ...rest):Boolean { 
      return clicked[button]; 
     }); 

    if (allButtonsClicked) { 
     // remove all listeners 
     for each (var button:DisplayObject in buttons) { 
      button.removeEventListener(MouseEvent.CLICK, buttonClickHandler); 
     } 

     //do whatever action you need. For example: 
     gotoAndStop(2); 
    } 
} 

// initialize dictionary values, and add listeners 
for (var button:DisplayObject in buttons) { 
    clicked[button] = false; 
    btn_one.addEventListener(MouseEvent.CLICK, buttonClickHandler); 
    btn_two.addEventListener(MouseEvent.CLICK, buttonClickHandler); 
    btn_three.addEventListener(MouseEvent.CLICK, buttonClickHandler); 
    btn_four.addEventListener(MouseEvent.CLICK, buttonClickHandler); 
    btn_five.addEventListener(MouseEvent.CLICK, buttonClickHandler); 
    btn_six.addEventListener(MouseEvent.CLICK, buttonClickHandler); 
    btn_seven.addEventListener(MouseEvent.CLICK, buttonClickHandler); 
} 

我測試了leetwinski的代碼,並添加了我的按鈕的eventlisteners,但它不起作用。我得到錯誤「1067:將類型的類的值隱式強制爲無關類型的flash.display:DisplayObject」我做錯了什麼?

+0

你是如何創建這些按鈕的?看起來這些btn_one,...都是類名。您應該在代碼中使用實例名稱。實例名稱應該爲舞臺上的每個按鈕設置。 – leetwinski

+0

https://helpx.adobe.com/flash/using/symbol-instances.html – leetwinski

+0

對不起。我忘了告訴你,我使用MovieClips而不是Buttons,因爲我可以改變顏色或點擊這些「按鈕」時的形狀 - >在frame1中開始;點擊顯示frame2。我很抱歉。 – 3iskalt