KeyboardEvent Listener 导致 TypeError?
我收到这个奇怪的错误,不知道为什么,我已将代码减少到最少并找到了问题的根源,但我不明白为什么会出错。
Main.as
package {
import flash.display.MovieClip;
public class main extends MovieClip{
public function main(){
var player1 = new playerTest( 100 , 275 );
addChild( player1 );
}
}
}
Player.as
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
public class playerTest extends MovieClip{
public function playerTest(xCoord:int, yCoord:int){
x = xCoord;
y = yCoord;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownEvent);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);
}
private function keyDownEvent(e:KeyboardEvent):void{
//
}
private function keyUpEvent(e:KeyboardEvent):void{
//
}
}
}
如果我注释掉其中一个 KeyboardEvent 侦听器,那么它可以正常工作,但是只要将它们放在那里就会出错并返回
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at playerTest()
at main()
这只是在我转换代码以通过代码生成我的角色后才开始发生的而不是直接把它扔到舞台上,在它完全正常工作之前是这样的。
I'm getting this weird error, don't know why, I've reduced the code to minimum and found the source of the problem but I don't understand why it errors.
Main.as
package {
import flash.display.MovieClip;
public class main extends MovieClip{
public function main(){
var player1 = new playerTest( 100 , 275 );
addChild( player1 );
}
}
}
Player.as
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
public class playerTest extends MovieClip{
public function playerTest(xCoord:int, yCoord:int){
x = xCoord;
y = yCoord;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownEvent);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);
}
private function keyDownEvent(e:KeyboardEvent):void{
//
}
private function keyUpEvent(e:KeyboardEvent):void{
//
}
}
}
If I comment out either of the KeyboardEvent listeners then it works fine, but just having them there like that makes it error and returns
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at playerTest()
at main()
This only has started happening since I converted my code to make my character be spawned through code instead of just throwing it onto the stage, when it was like that before it worked totally fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,您可以从舞台上已有的任何 DisplayObject 访问舞台。因此,在您的班级中,您可以等到播放器添加到舞台后添加键盘侦听器,如下所示:
Actually, you can access stage from any DisplayObject that is already on the stage. So, in your class, you can wait till the player is added to the stage to add the keyboard listeners, like this:
与 Benny 所说的相反,您绝对可以使用外部类中的舞台,但是该类必须是也在舞台上的 DisplayObject(或其子类)。当player1被实例化然后添加到舞台时,它还没有对舞台的引用。您可以侦听 Event.ADDED_TO_STAGE,然后根据需要将事件连接到舞台。
Contrary to what Benny says, you can absolutely use the stage from an external class, however that class must be a (or a subclass of a) DisplayObject that is also -on- the stage. As player1 is instantiated and then added to the stage, it does not yet have a reference to the stage. You can listen for Event.ADDED_TO_STAGE and then hookup your events to the stage if you'd like.
从外部类引用
stage
并不是最佳实践,如果您没有对任何displayobject
进行子类化,则可能不允许您引用stage< /代码>。所以最好使用displayObjects。
//主要的
It is not a best practice to refer the
stage
from an external class, may be if you are not subclassing anydisplayobject
that will not allow you to referstage
. So it is better to go with displayObjects.//main