如何调试 ActionScript 中的 If 和 Else 语句?

How to debug If and Else statements in ActionScript?

我的游戏是一款拖放游戏,您必须在其中装扮角色。我需要帮助的是,每次我点击完成时,它都会直接进入第 12 帧(重试屏幕)。

我不确定如何才能做到当所有部件都在正确的位置并且我单击完成时,它会将用户带到第 11 帧(恭喜屏幕)并且当部件不在时正确的位置,它将把用户带到第 12 帧(重试屏幕)。

//Menu Button
menubtn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_11);

function fl_ClickToGoToAndStopAtFrame_11(event:MouseEvent):void
{
    gotoAndStop(5);
}

//Not sure what this means
stage.addEventListener(Event.ENTER_FRAME,EntFrame);
    function EntFrame(e:Event) :void
{


  //there are a total of 9 pieces (hair,shirt,shorts, leftglove,rightglove,leftsock,rightsock,rightboot,leftboot)
//from hair to left boot the code is just copied and pasted.)

    //Hair
hairmc.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_13);

function fl_ClickToDrag_13(event:MouseEvent):void
{
    hairmc.startDrag();
}

stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_13);

function fl_ReleaseToDrop_13(event:MouseEvent):void
{
    hairmc.stopDrag();
}
if (targethairmc.hitTestObject(hairmc.hair_mc))

{
    hairmc.x = 368.05;
    hairmc.y = 71.70;

}


//Left Boot
leftbootmc.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_23);

function fl_ClickToDrag_23(event:MouseEvent):void
{
    leftbootmc.startDrag();
}

stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_23);

function fl_ReleaseToDrop_23(event:MouseEvent):void
{
    leftbootmc.stopDrag();
}
if (targetleftbootmc.hitTestObject(leftbootmc.leftboot_mc))

{
    leftbootmc.x = 310.30;
    leftbootmc.y = 461.55;
}


finishbtn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);

function fl_MouseClickHandler_2(event:MouseEvent):void
{
    if (shirtmc.x == 365.95 && shirtmc.y == 208.60 && leftglovemc.x == 239.45 && leftglovemc.y == 160.15 && leftsockmc.x == 333.05 && leftsockmc.y == 411.25 && leftbootmc.x == 310.30 && leftbootmc.y == 461.55 && hairmc.x == 368.05 && hairmc.y == 71.70 && pantsmc.x == 367.05 && pantsmc.y == 311.25 && rightbootmc.x == 426.80 && rightbootmc.y == 461.55 && rightsockmc.x == 405.95 && rightsockmc.y == 411.25 && rightglovemc.x == 496.95 && rightglovemc.y == 157.65){
        removeEventListener(Event.ENTER_FRAME, EntFrame);
        gotoAndStop(11);
    }else {
        gotoAndStop (12);
    }

}

每当我点击完成按钮时,棋子的位置和位置不正确,或者菜单按钮(这会让我回到可以从 3 个游戏中 select 的屏幕,它会带来在输出部分添加一条消息:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Ronaldo_fla::MainTimeline/EntFrame()

按钮仍然有效,它们会转到它们预期的帧,但消息不断重复,导致游戏滞后和变慢。

这是显示弹出消息的图片。 http://i.imgur.com/Iz3sf9m.png

这行好像是错误的:

if (shirtmc.x == 365.95 && shirtmc.y == 208.60 && leftglovemc.x == 239.45 && leftglovemc.y == 160.15 && leftsockmc.x == 333.05 && leftsockmc.y == 411.25 && leftbootmc.x == 310.30 && leftbootmc.y == 461.55 && hairmc.x == 368.05 && hairmc.y == 71.70 && pantsmc.x == 367.05 && pantsmc.y == 311.25 && rightbootmc.x == 426.80 && rightbootmc.y == 461.55 && rightsockmc.x == 405.95 && rightsockmc.y == 411.25 && rightglovemc.x == 496.95 && rightglovemc.y == 157.65){

首先,这不是很可读,并且这可能向您隐藏了您本可以发现的问题。我假设 ActionScript 会让您换行 - 您会先在您的代码中尝试这个修正吗?

if (
    shirtmc.x == 365.95 && shirtmc.y == 208.60 &&
    leftglovemc.x == 239.45 && leftglovemc.y == 160.15 &&
    leftsockmc.x == 333.05 && leftsockmc.y == 411.25 &&
    leftbootmc.x == 310.30 && leftbootmc.y == 461.55 &&
    hairmc.x == 368.05 && hairmc.y == 71.70 &&
    pantsmc.x == 367.05 && pantsmc.y == 311.25 &&
    rightbootmc.x == 426.80 && rightbootmc.y == 461.55 &&
    rightsockmc.x == 405.95 && rightsockmc.y == 411.25 &&
    rightglovemc.x == 496.95 && rightglovemc.y == 157.65
){

好的:这 18 个条件中的一个或多个在您不期望的地方失败了。人们会假设 ActionScript 为您提供了一些关于如何调试的选择:

  • 在此if行设置断点,并在触发断点时仔细检查每个变量的值;
  • 将变量输出到文件或控制台;
  • 注释掉条款,直到找到有问题的条款(见下文)。

我怀疑使用此技术您会确定问题所在。


"Commenting out" 是一个方便的调试技巧,可以在您准备好 运行 之前从计算机中隐藏代码位。暂时把代码改成这样:

if (
    true
    //shirtmc.x == 365.95 && shirtmc.y == 208.60 &&
    //leftglovemc.x == 239.45 && leftglovemc.y == 160.15 &&
    //leftsockmc.x == 333.05 && leftsockmc.y == 411.25 &&
    //leftbootmc.x == 310.30 && leftbootmc.y == 461.55 &&
    //hairmc.x == 368.05 && hairmc.y == 71.70 &&
    //pantsmc.x == 367.05 && pantsmc.y == 311.25 &&
    //rightbootmc.x == 426.80 && rightbootmc.y == 461.55 &&
    //rightsockmc.x == 405.95 && rightsockmc.y == 411.25 &&
    //rightglovemc.x == 496.95 && rightglovemc.y == 157.65
){

您的第一个任务是确保子句然后 运行s,因为 true 将始终导致它成为 运行。接下来,试试这个:

if (
    shirtmc.x == 365.95 && shirtmc.y == 208.60 //&&
    //leftglovemc.x == 239.45 && leftglovemc.y == 160.15 &&
    //leftsockmc.x == 333.05 && leftsockmc.y == 411.25 &&
    //leftbootmc.x == 310.30 && leftbootmc.y == 461.55 &&
    //hairmc.x == 368.05 && hairmc.y == 71.70 &&
    //pantsmc.x == 367.05 && pantsmc.y == 311.25 &&
    //rightbootmc.x == 426.80 && rightbootmc.y == 461.55 &&
    //rightsockmc.x == 405.95 && rightsockmc.y == 411.25 &&
    //rightglovemc.x == 496.95 && rightglovemc.y == 157.65
){

请注意,我已经注释掉了第一行的 &&,因此该语句仍然有意义。好的,那就试试吧。如果可行,则执行下一行,如果失败(例如出现错误或 else 部分触发)找出原因。继续取消注释行,直到发现问题。