I’m creating a simple scratch game (6 cards). It doesn’t even have to contain difficult logic. You eventually win all the time 😉
The code i wrote works pretty well, but i keep thinking it could be better. I’m not too familiar with coding, but i’m learning step by step.
Do you have ideas to write this down in a cleaner and more sophisticated way?
E.g. I tried to separate the IF statement that returns in every function, so i would only have to write it down once, but i didn’t succeed.
Thanks in advance for looking.
//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");
}
}
}
Apply DRY (Don’t Repeat Yourself) principles by moving redudant code into functions. I also changed your check variables to be Booleans instead of ints. If you are only using them for a checked/unchecked flag, then a Boolean is all you need.
I didn’t write a tester app for this, so let me know if you run into issues.