所以我在文档类中有这个非常基本的代码:
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。” “在KeyboardUp上访问未定义的属性。” “访问未定义的属性onKeyboardDown。”
我真的不明白这个问题。 AS3如何识别addEventListener?同样,我确实拥有它,所以我的事件监听器被添加到舞台“stage.addEventListener”,它也没有识别舞台。有人可以用这个问题推动我朝着正确的方向前进吗?谢谢!
答案 0 :(得分:1)
这是逻辑,因为你必须将eventListeners放在'init'方法或Class构造函数中。
public function init():void
{
addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
addEventListener(Event.ENTER_FRAME, onEnter);
vx = 0;
vy = 0;
circle = new Circle(35, 0x0066FF);
stage.addChild(circle);
circle.x = 50;
circle.y = 50;
}
如果不是,则将侦听器放在类范围之外,因此无法识别。
祝你好运!答案 1 :(得分:0)
总而言之,你的代码几乎就是你需要更好地了解显示列表的工作原理。
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;
// we can not do function calls like this in the class declaration area
// so we move these listeners to a function
// addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
// addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
// addEventListener(Event.ENTER_FRAME, onEnter);
public function Main()
{
super();
this.init();
}
public function init():void
{
// the "this" keyword means we are scoping it to this class instance
this.addEventListener( EVENT.ADDEDTOSTAGE, addedToStageHandler)
// using "this" is good practice and will help make your code more readable
this.vx = 0;
this.vy = 0;
this.circle = new Circle(35, 0x0066FF);
stage.addChild(circle);
this.circle.x = 50;
this.circle.y = 50;
}
public function addedToStageHandler(event:Event):void
{
// doing addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
// will set the scope for this listener to this class
// you want to target the stage. And since we are waiting for ADDEDTOSTAGE
// to trigger we know we are on the stage.
// the only time we can access stage is if we are on the display list.
// clean up the listener since we do not need it anymore
this.removeEventListener( EVENT.ADDEDTOSTAGE, addedToStageHandler)
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}
public function onKeyboardDown(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.LEFT:
this.vx = -5;
break;
case Keyboard.RIGHT:
this.vx = 5;
break;
case Keyboard.UP:
this.vy = -5;
break;
case Keyboard.DOWN:
this.vy = 5;
break;
}
}
public function onKeyboardUp(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case Keyboard.LEFT:
this.vx = 0;
break;
case Keyboard.RIGHT:
this.vx = 0;
break;
case Keyboard.UP:
this.vy = 0;
break;
case Keyboard.DOWN:
this.vy = 0;
break;
}
}
public function onEnter(event:Event):void
{
this.circle.x += this.vx;
this.circle.y += this.vy;
}
}
}