我正在创建一个简单的临时游戏(6张牌)。它甚至不必包含困难的逻辑。你最终总是赢;)
我写的代码效果很好,但我一直认为它可能会更好。我不太熟悉编码,但我正在逐步学习。
您是否有想法以更清洁,更复杂的方式写下来?
E.g。我试图分离在每个函数中返回的IF语句,所以我只需要将其写下来一次,但我没有成功。
提前感谢您的光临。
//set card variables to 0 and when they're scratched, they're changed to 1
var checkCard1:int = 0, checkCard2:int = 0, checkCard3:int = 0, checkCard4:int = 0, checkCard5:int = 0, checkCard6:int = 0;
leaf1.addEventListener(MouseEvent.ROLL_OVER, cardChecked1);
function cardChecked1 (event:MouseEvent):void {
checkCard1 = 1;
if (checkCard1 && checkCard2 && checkCard3 && checkCard4 && checkCard5 && checkCard6 == 1) {
var myDelay:Timer = new Timer(2000);
myDelay.addEventListener(TimerEvent.TIMER, showMessage);
myDelay.start();
function showMessage(event:TimerEvent):void{
myDelay.stop();
gotoAndStop("nowinner");
}
}
}
leaf2.addEventListener(MouseEvent.ROLL_OVER, cardChecked2);
function cardChecked2 (event:MouseEvent):void {
checkCard2 = 1;
if (checkCard1 && checkCard2 && checkCard3 && checkCard4 && checkCard5 && checkCard6 == 1) {
var myDelay:Timer = new Timer(2000);
myDelay.addEventListener(TimerEvent.TIMER, showMessage);
myDelay.start();
function showMessage(event:TimerEvent):void{
myDelay.stop();
gotoAndStop("nowinner");
}
}
}
leaf3.addEventListener(MouseEvent.ROLL_OVER, cardChecked3);
function cardChecked3 (event:MouseEvent):void {
checkCard3 = 1;
if (checkCard1 && checkCard2 && checkCard3 && checkCard4 && checkCard5 && checkCard6 == 1) {
var myDelay:Timer = new Timer(2000);
myDelay.addEventListener(TimerEvent.TIMER, showMessage);
myDelay.start();
function showMessage(event:TimerEvent):void{
myDelay.stop();
gotoAndStop("nowinner");
}
}
}
leaf4.addEventListener(MouseEvent.ROLL_OVER, cardChecked4);
function cardChecked4 (event:MouseEvent):void {
checkCard4 = 1;
if (checkCard1 && checkCard2 && checkCard3 && checkCard4 && checkCard5 && checkCard6 == 1) {
var myDelay:Timer = new Timer(2000);
myDelay.addEventListener(TimerEvent.TIMER, showMessage);
myDelay.start();
function showMessage(event:TimerEvent):void{
myDelay.stop();
gotoAndStop("nowinner");
}
}
}
leaf5.addEventListener(MouseEvent.ROLL_OVER, cardChecked5);
function cardChecked5 (event:MouseEvent):void {
checkCard5 = 1;
if (checkCard1 && checkCard2 && checkCard3 && checkCard4 && checkCard5 && checkCard6 == 1) {
var myDelay:Timer = new Timer(2000);
myDelay.addEventListener(TimerEvent.TIMER, showMessage);
myDelay.start();
function showMessage(event:TimerEvent):void{
myDelay.stop();
gotoAndStop("nowinner");
}
}
}
leaf6.addEventListener(MouseEvent.ROLL_OVER, cardChecked6);
function cardChecked6 (event:MouseEvent):void {
checkCard6 = 1;
if (checkCard1 && checkCard2 && checkCard3 && checkCard4 && checkCard5 && checkCard6 == 1) {
var myDelay:Timer = new Timer(2000);
myDelay.addEventListener(TimerEvent.TIMER, showMessage);
myDelay.start();
function showMessage(event:TimerEvent):void{
myDelay.stop();
gotoAndStop("nowinner");
}
}
}
答案 0 :(得分:2)
通过将还原剂代码移动到函数中来应用DRY(不要重复自己)原则。我还将检查变量更改为布尔值而不是整数。如果您只将它们用于已选中/未选中的标记,则只需要一个布尔值。
var checkCard1:Boolean = false, checkCard2:Boolean = false, checkCard3:Boolean = false, checkCard4:Boolean = false, checkCard5:Boolean = false, checkCard6:Boolean = false;
function checkAllCards()
{
if (checkCard1 && checkCard2 && checkCard3 && checkCard4 && checkCard5 && checkCard6)
{
var myDelay:Timer = new Timer(2000);
myDelay.addEventListener(TimerEvent.TIMER, showMessage);
myDelay.start();
function showMessage(event:TimerEvent):void{
myDelay.stop();
gotoAndStop("nowinner");
}
}
}
function cardChecked(event:MouseEvent):void
{
if (event.currentTarget === leaf1)
{ checkCard1 = true; }
else if (event.currentTarget === leaf2)
{ checkCard2 = true; }
else if (event.currentTarget === leaf3)
{ checkCard3 = true; }
else if (event.currentTarget === leaf4)
{ checkCard4 = true; }
else if (event.currentTarget === leaf5)
{ checkCard5 = true; }
else if (event.currentTarget === leaf6)
{ checkCard6 = true; }
checkAllCards();
}
leaf1.addEventListener(MouseEvent.ROLL_OVER, cardChecked);
leaf2.addEventListener(MouseEvent.ROLL_OVER, cardChecked);
leaf3.addEventListener(MouseEvent.ROLL_OVER, cardChecked);
leaf4.addEventListener(MouseEvent.ROLL_OVER, cardChecked);
leaf5.addEventListener(MouseEvent.ROLL_OVER, cardChecked);
leaf6.addEventListener(MouseEvent.ROLL_OVER, cardChecked);
我没有为此编写测试版应用程序,所以如果您遇到问题,请告诉我。