我不明白发生了什么 这是我的Main.as
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Main extends MovieClip {
public var pirkles:Circles = new Circles()
public function Main() {
gotoAndStop(1)
playbtn.addEventListener(MouseEvent.CLICK, playscreen)
}
public function playscreen(event:MouseEvent):void {
gotoAndStop(2)
addChild(pirkles)
}
}
}
这是我的Circles.as
package {
import flash.display.MovieClip
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard
import flash.events.MouseEvent;
public class Circles extends MovieClip{
public function Circles():void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, MOVE)
this.y = 175
this.x = 10
}
public function MOVE(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.RIGHT) {
this.x = this.x+10
}
else if (event.keyCode == Keyboard.LEFT) {
this.x = this.x-10
}
else if (event.keyCode == Keyboard.UP) {
this.y = this.y-10
}
else if (event.keyCode == Keyboard.DOWN) {
this.y = this.y+10
}
}
}
}
现在我收到一条错误消息,指出我的Circles.as第11行和Main.as第8行存在问题。但是,在这些方面,我不明白是什么导致了这个问题。我在第11行添加了一个事件监听器,但是当我把它拿出来时它有效。另外,在第8行,我刚刚定义了一个变量。
答案 0 :(得分:0)
您无法访问类构造函数中的舞台。 这一行
stage.addEventListener(KeyboardEvent.KEY_DOWN, MOVE)
导致错误。
如果您需要访问舞台,请在构造函数中为ADDED_TO_STAGE事件添加一个侦听器,并在回调函数中,您将能够访问该舞台
所以:
public function Circles():void {
this.addEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
this.y = 175
this.x = 10
}
private function onAddedToStage (evt:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, MOVE)
}