Actionscript 3 错误#1009仅在第2帧上
它点亮了部分的第二行,并表示无法访问空对象,但只能访问第二帧 如果在主文档上,我将其设置为转到第1帧,它将正常工作。子文档添加完美字符 地块类Actionscript 3 错误#1009仅在第2帧上,actionscript-3,flash,frame,Actionscript 3,Flash,Frame,它点亮了部分的第二行,并表示无法访问空对象,但只能访问第二帧 如果在主文档上,我将其设置为转到第1帧,它将正常工作。子文档添加完美字符 地块类 package { import flash.display.MovieClip; import flash.events.Event; public class Ground_1 extends MovieClip { public function Ground_1() {
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Ground_1 extends MovieClip
{
public function Ground_1()
{
//constructor code
trace(root);
this.addEventListener(Event.ENTER_FRAME, loop);
}
function loop(e:Event)
{
//loop
if(!root)
{
return;
}
for(var i:int=0;i<10;i++)
{
//solid ground
trace("Char :", Main(root).char);
if(this.hitTestPoint(MovieClip(root).char.x,MovieClip(root).char.y,true))
{
MovieClip(root).char.y --;
MovieClip(root).char.Yvelocity = 0;
MovieClip(root).char.canJump = true;
}
//Cieling
if(this.hitTestPoint(MovieClip(root).char.x,MovieClip(root).char.y-25,true))
{
MovieClip(root).upPressed = false;
MovieClip(root).char.Yvelocity = 0;
MovieClip(root).char.y ++;
}
//Walls
if(this.hitTestPoint(MovieClip(root).char.x+MovieClip(root).char.width/2,MovieClip(root).char.y-21,true))
{
if(MovieClip(root).rightPressed)
{
MovieClip(root).rightPressed = false;
MovieClip(root).char.x -= MovieClip(root).char.speed;
}
}
else if(this.hitTestPoint(MovieClip(root).char.x-MovieClip(root).char.width/2,MovieClip(root).char.y-21,true))
{
if(MovieClip(root).leftPressed)
{
MovieClip(root).leftPressed = false;
MovieClip(root).char.x += MovieClip(root).char.speed;
}
}
}
}
}
}
您的问题是
root
在Ground\u 1
实例中第一次运行loop
函数时为空
显示对象的根
,父
和阶段
变量仅在所述显示对象添加到显示列表时填充。在root&stage的情况下,还必须添加父/母(以及任何祖父母),以便最顶层的父/祖父母就是stage本身
在document类中,不能保证在其构造函数(函数Main
)中它已经在显示列表中。有时(通常)stage在文档类构造函数中有一个值,但并不总是这样
下面是如何在代码中执行的示例:
事件来等待安全。已将\u添加到\u STAGE
事件
public function Main(){
//if stage doesn't exist yet, wait for it, then call stageReady
if(!stage){
this.addEventListener(Event.ADDED_TO_STAGE, stageReady);
}else{
stageReady();
}
}
private function stageReady(e:Event = null):void {
this.gotoAndStop(1);
//Character
char = new player();
char.x = starter.x;
char.y = starter.y;
addChild(char);
//starter
starter.visible = false;
//Event Listeners
this.addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, downHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, upHandler);
}
同样,在你的Ground_1
课程中,你也需要这样做:
public function Ground_1()
{
//constructor code
trace(root); //likely null
this.addEventListener(Event.ADDED_TO_STAGE, stageReady, false, 0, true);
this.addEventListener(Event.REMOVED_FROM_STAGE, stageUnloaded, false, 0, true); //this is optional
this.addEventListener(Event.ENTER_FRAME, loop);
}
private function stageReady(e:Event):void {
//only listen for frame events once this object has been added to the stage
this.addEventListener(Event.ENTER_FRAME, loop);
}
private function stageUnloaded(e:Event):void {
//stop listening for frame events if this object gets removed from the stage
this.removeEventListener(Event.ENTER_FRAME, loop);
}
执行此代码时,最有可能的
char
在根时间线上不存在。虽然没有人能给你一个好的答案,但如果不了解更多关于代码和上下文的信息,char会添加到主文档中,addchild不会添加到特定的框架中,我在框架上工作one@BadFeelingAboutThis只有当我删除地上的blocks.如果我制作了一个框架的精确副本(使用F6),它可以工作。ech ground block是从库中添加的类的实例,而不是Child。我的敌人类也发生了同样的事情,它说空对象是Char。但是Char没有添加到特定的框架中,我刚刚在主文档类上做了[addChild(Char)],请帮助!!!!
public function Ground_1()
{
//constructor code
trace(root); //likely null
this.addEventListener(Event.ADDED_TO_STAGE, stageReady, false, 0, true);
this.addEventListener(Event.REMOVED_FROM_STAGE, stageUnloaded, false, 0, true); //this is optional
this.addEventListener(Event.ENTER_FRAME, loop);
}
private function stageReady(e:Event):void {
//only listen for frame events once this object has been added to the stage
this.addEventListener(Event.ENTER_FRAME, loop);
}
private function stageUnloaded(e:Event):void {
//stop listening for frame events if this object gets removed from the stage
this.removeEventListener(Event.ENTER_FRAME, loop);
}