在ActionScript中将hitTestObject与'if'语句一起使用

时间:2012-12-17 01:28:22

标签: actionscript if-statement drag-and-drop hittest statements

我正在尝试使用hitTestObject和enter_frame来创建一个“测试”页面,用户将“答案框”拖到另一个框中,如果答案是正确的,则显示“正确,如果不是”错误的单词,但是我有四个问题(q1)和4个答案(a1),在输入第一个答案后,它没有做任何其他事情,除非我将正方形移出正确的答案。

如何让它工作并忽略以前的回复?我也在使用拖放命令。

-

finalTest.addEventListener(Event.ENTER_FRAME, detectCollision);

function detectCollision (evt: Event) : void {
    if(a1.hitTestObject(q1)) {
        finalTest.checkText.text = "Correct answer";
    }
    else if(a1.hitTestObject(q2)) {
        finalTest.checkText.text = "wrong";
    }
    else if(a1.hitTestObject(q3)) {
        finalTest.checkText.text = "wrong";
    }
    else if(a1.hitTestObject(q4)) {
        finalTest.checkText.text = "wrong";
    }

    else if(a2.hitTestObject(q2)) {
        finalTest.checkText.text = "Correct answer";
    }
    else if(a2.hitTestObject(q1)) {
        finalTest.checkText.text = "wrong";
    }
    else if(a2.hitTestObject(q3)) {
        finalTest.checkText.text = "wrong";
    }
    else if(a2.hitTestObject(q4)) {
        finalTest.checkText.text = "wrong";
    }

    else if(a3.hitTestObject(q3)) {
        finalTest.checkText.text = "Correct answer";
    }
    else if(a3.hitTestObject(q1)) {
        finalTest.checkText.text = "wrong";
    }
    else if(a3.hitTestObject(q2)) {
        finalTest.checkText.text = "wrong";
    }
    else if(a3.hitTestObject(q4)) {
        finalTest.checkText.text = "wrong";
    }

    else if(a4.hitTestObject(q4)) {
        finalTest.checkText.text = "Correct answer";
    }
    else if(a4.hitTestObject(q1)) {
        finalTest.checkText.text = "wrong";
    }
    else if(a4.hitTestObject(q2)) {
        finalTest.checkText.text = "wrong";
    }
    else if(a4.hitTestObject(q3)) {
        finalTest.checkText.text = "wrong";
    }
    else {
        finalTest.checkText.text = " ";
    }
}

1 个答案:

答案 0 :(得分:0)

也许这会有所帮助:

var questionArray:Array = new Array(q1,q2,q3,q4);
var answerArray:Array = new Array(a1,a2,a3,a4);

var checkId:int = 0;

for(var i:int = 0; i<answerArray.length; i++) {
     answerArray[i].ids = i;
     answerArray[i].addEventListener(MouseEvent.MOUSE_DOWN, onDragHandler);
     answerArray[i].addEventListener(MouseEvent.MOUSE_UP, onReleaseHandler);
}

function onDragHandler(e:MouseEvent) {
     e.currentTarget.startDrag();
     checkId = e.currentTarget.ids;
}

function onReleaseHandler(e:MouseEvent) {
    e.currentTarget.stopDrag();
    if(e.currentTarget.hitTestObject(questionArray[checkId])) {
        finalTest.checkText.text = "Correct Answer";
    } else {
        finalTest.checkText.text = "Wrong";
    }
}

我提出问题并回答数组。我没有使用ENTER_FRAME ..