ActionScript报1084:expecting rightparen before colon错误排查
错误原因定位
1. 直接触发语法报错的原因
你两处调用moveCharacter的写法都存在语法错误:
- 调用函数时不需要给参数加类型标注,
:KeyboardEvent是函数定义阶段写在参数列表里的,调用时写这个会触发语法解析错误,就是你看到的expecting rightparen before colon报错 - 函数调用语句结束后不应该跟大括号,你写的
{ timeStep = 0; }属于多余的非法语法块,ActionScript不支持这种调用函数后加块的写法。
2. 隐藏的逻辑/类型错误
就算你改了上面的语法问题,代码也无法正常运行:timing函数接收的参数是TimerEvent类型,完全不符合moveCharacter要求的KeyboardEvent参数类型,TimerEvent实例里没有keyCode属性,运行时会抛出属性不存在的错误。
修正方案
你需要先把按键状态单独存储,不要依赖KeyboardEvent来触发定时移动:
// 先定义变量存储当前按下的方向,0=无方向,1=右,-1=左 var currentDirection:int = 0; // 键盘按下时记录方向 function onKeyDown(evt:KeyboardEvent):void { if (evt.keyCode == Keyboard.RIGHT) { currentDirection = 1; } else if (evt.keyCode == Keyboard.LEFT) { currentDirection = -1; } } // 键盘抬起时清空方向 function onKeyUp(evt:KeyboardEvent):void { if (evt.keyCode == Keyboard.RIGHT || evt.keyCode == Keyboard.LEFT) { currentDirection = 0; eggHead.gotoAndStop(1); // 回到静止帧 } } // 移动逻辑改成接收方向参数,不需要依赖事件 function moveCharacter(direction:int):void { eggHead.scaleX = direction; // 直接用方向值处理翻转 eggHead.x += 10 * direction; // 帧动画逻辑不变 if (eggHead.currentFrame == 9) { eggHead.gotoAndStop(2); } else { eggHead.gotoAndStop(eggHead.currentFrame+1); } } function timing(evt:TimerEvent):void { timeStep += 1; var shouldMove:Boolean = false; if (run) { shouldMove = true; } else if (timeStep == 2) { shouldMove = true; } if (shouldMove && currentDirection != 0) { moveCharacter(currentDirection); timeStep = 0; } }
内容的提问来源于stack exchange,提问作者user10116394
相关产品推荐
相关产品推荐

