所以我的文档类中有这个非常基本的代码:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.*;
import flash.events.MouseEvent;
import flash.display.Stage;
import flash.display.MovieClip;
public class Main extends Sprite
{
//Properties
public var circle:Circle;
public var vx:Number;
public var vy:Number;
addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
addEventListener(Event.ENTER_FRAME, onEnter);
public function addedToStageHandler(event:Event):void
{
}
public function Main()
{
super();
init();
}
public function init():void
{
vx = 0;
vy = 0;
circle = new Circle(35, 0x0066FF);
stage.addChild(circle);
circle.x = 50;
circle.y = 50;
}
public function onKeyboardDown(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.LEFT:
vx = -5;
break;
case Keyboard.RIGHT:
vx = 5;
break;
case Keyboard.UP:
vy = -5;
break;
case Keyboard.DOWN:
vy = 5;
break;
}
}
public function onKeyboardUp(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.LEFT:
vx = 0;
break;
case Keyboard.RIGHT:
vx = 0;
break;
case Keyboard.UP:
vy = 0;
break;
case Keyboard.DOWN:
vy = 0;
break;
}
}
public function onEnter(event:Event):void
{
circle.x += vx;
circle.y += vy;
}
}
}
问题是我不断收到对初学者没有任何意义的错误:
“调用可能未定义的方法 addEventListener。” x 3“访问未定义的属性 onEnter。” “访问未定义的属性 onKeyboardUp。” “访问未定义的属性 onKeyboardDown。”
我真的不明白这个问题。AS3 怎么不能识别 addEventListener?同样,我确实拥有它,所以我的事件侦听器被添加到舞台“stage.addEventListener”中,它也无法识别舞台。有人可以在这个问题上将我推向正确的方向吗?谢谢!