2011-02-06 31 views
0

我重溫一些舊的代碼過濾XML,但是這可以很容易地應用到的方法(我使用它的方式,它本質上是)的參數。這是一個問題,我覺得我遇到很多問題,不知道解決這個問題的好方法。AS3:測試可選參數和組合

所以問題是,我有3個參數。他們都是可選的。我想看看他們的價值觀基礎上哪些是存在(根據可能性排序)哪些禮物和測試:

var shiftDown : Boolean = false; 
var controlDown : Boolean = false; 

if ("@shift" in x) 
{ 
    shiftDown = Global.stringToBoolean([email protected]()); 
} 
if ("@control" in x) 
{ 
    controlDown = Global.stringToBoolean([email protected]()); 
} 


if ("@code" in x && "@shift" in x && "@control" in x) 
{ 
    if (KeyManager.keyIsDown(KeyManager[ [email protected]().toUpperCase() ]) && (KeyManager.shiftKey == shiftDown) && (KeyManager.controlKey == controlDown)) 
    { 
     ... 
    } 
} 
else if ("@code" in x && "@shift" in x) 
{ 
    if (KeyManager.keyIsDown(KeyManager[ [email protected]().toUpperCase() ]) && (KeyManager.shiftKey == shiftDown)) 
    { 
     ... 
    }  
} 
else if ("@code" in x && "@control" in x) 
{ 
    if (KeyManager.keyIsDown(KeyManager[ [email protected]().toUpperCase() ]) && (KeyManager.controlKey == controlDown)) 
    { 
     ... 
    }  
}  
else if ("@code" in x) 
{ 
    if (KeyManager.keyIsDown(KeyManager[ [email protected]().toUpperCase() ])) 
    { 
     ... 
    } 
} 
else if ("@shift" in x) 
{ 
    if (KeyManager.shiftKey == shiftDown) 
    { 
     ... 
    } 
} 
else if ("@control" in x) 
{ 
    if (KeyManager.controlKey == controlDown) 
    { 
     ... 
    } 
} 

else if ("@control" in x) && ("@shift" in x)) 
{ 
    if ((KeyManager.shiftKey == shiftDown) && (KeyManager.controlKey == controlDown)) 
    { 
     ... 
    } 
} 

我覺得必須有到有這麼多的重複寫一個較短的方式目前的形式。有人可以建議一個更清潔和更有效的方式來寫這個嗎?

感謝您的想法。

編輯:if語句順序是錯誤的。改變了。

這可以推廣。爲了清晰起見,我只包括我的代碼。如果一般問題仍然不清楚,我得到的印象是:

什麼是最純淨/最有效的方式來測試排他性的可選參數的所有組合?

+0

能降檔,controlDown variabled即使沒有降檔,controlDown在XML是真的嗎? – Patrick 2011-02-06 01:42:34

+0

你確定你的IF順序的,因爲如果@code設置你將永遠不會達到所有其他的測試......如果他們在XML不是 – Patrick 2011-02-06 02:19:13

回答

0

我要在這裏做一些假設。從你的例子來看,我認爲你想基於按鍵執行任務。 XML似乎包含某種偏好,無論您是要啓用還是禁用某些密鑰。您有兩個確定的鍵,轉換和控制,以及一個基於鍵碼的通配符鍵。如果這些假設是正確的,您應該能夠通過將偏好測試和實際的關鍵測試結合在一條線上來縮短事情。

var shiftIsDown:Boolean = [email protected]() ? KeyManager.keyIsDown(KeyManager [ [email protected]().toUpperCase() ]) : false; 
var controlIsDown:Boolean = [email protected]() ? KeyManager.keyIsDown(KeyManager [ [email protected]().toUpperCase() ]) : false; 
var customIsDown:Boolean = [email protected]() ? KeyManager.keyIsDown(KeyManager [ [email protected]().toUpperCase() ]) : false; 

我認爲KeyManager行有點奇怪。我不知道Flex或常規AS3中的KeyManager,這個自定義代碼也是如此?如果是這樣,你可以通過像一個customKeyIsDown()方法把大寫鍵對碼在那裏,而不是做一切在這裏。 Shift和Control無論如何都是固定的,所以不需要反向匹配XML的值,對吧?

var shiftIsDown:Boolean = [email protected]() ? KeyManager.keyIsDown(KeyManager.SHIFT) : false; 
var controlIsDown:Boolean = [email protected]() ? KeyManager.keyIsDown(KeyManager.CONTROL) : false; 
var customIsDown:Boolean = [email protected]() ? KeyManager.customKeyIsDown([email protected]) : false; 

我認爲這已經更清晰了一些,但是我再也不知道KeyManager做了什麼。在此之後,你仍然有三個變量,它們都是可選的。如果他們都需要排他性,那麼就會有8種可能的結果。

if (shiftIsDown && controlIsDown && customIsDown) { 
    // 1 
} else if (shiftIsDown && controlIsDown) { 
    // 2  
} else if (shiftIsDown && customIsDown) { 
    // 3  
} else if (shiftIsDown) { 
    // 4  
} else if (controlIsDown && customIsDown) { 
    // 5  
} else if (controlIsDown) { 
    // 6  
} else if (customIsDown) { 
    // 7 
} else { 
    // 8 
} 

如果雖然你要基於鍵做的是不是排他性的,你可以回到剛剛執行的三大關鍵任務基礎

if (shiftIsDown) { 
    // 1 
} 
if (controlIsDown) { 
    // 2 
} 
if (customIsDown) { 
    // 3 
} 

這是否幫助? 乾杯, EP。