我目前遇到问题时引用了MovieClip
子项,我将其添加到Document
类的Stage
中。基本上当孩子被添加到Document
类的Stage
时,我想Stage
上的某个MovieClip
已经在Stage
上引用它。参考影片剪辑作为子项添加到舞台后
此外,如果可能的话,我不希望MovieClip
引用孩子被添加到Stage
有与Document
类连接它的参数,因为我计划稍后在另一个MovieClip
嵌套这MovieClip
在将来。
下面是MovieClip
类,这是参照孩子一旦它被添加到Stage
代码:
package com.gameEngine.assetHolders
{
import com.gameEngine.documentClass.*;
import com.gameEngine.assetHolders.*;
import com.gameEngine.assetHolders.Levels.*;
import flash.display.*;
import flash.events.*;
public class FallingPlatform extends MovieClip
{
public var _document:Document;
// Trying to reference "_player"
public var _player:Player;
public var fallState:Boolean;
public var platformYSpeed:Number = 0;
public var platformGravityPower:Number = 0.75;
public function FallingPlatform()
{
this.addEventListener(Event.ADDED_TO_STAGE, initFallingPlatform);
// constructor code
}
public function initFallingPlatform(event:Event)
{
this.addEventListener(Event.ENTER_FRAME, dynamicFall);
this.addEventListener(Event.ENTER_FRAME, hitTest);
}
public function dynamicFall(event:Event)
{
if (this.fallState)
{
this.platformYSpeed += this.platformGravityPower;
y += this.platformYSpeed;
}
}
// Trying to reference "_player"
public function hitTest(event:Event)
{
if (this.hitTestPoint(_player.x, _player.y + 1, true))
{
this.fallState = true;
}
}
}
}
好吧,我用hitTest函数中的“_document._player”替换了“_player”,但现在我得到错误#1009:无法访问空对象引用的属性或方法。任何想法如何解决这个错误? – Blaze356
是否_documment属性发起somwhere?也许你应该创建一个静态变量来保存文档的实例 –
该代码仍然与上次相同,只不过我在hitTest函数中将“_player”替换为“_document._player”。 “_document”属性是在FallingPlatform类(我发布的类)中初始化的,但是我是否正确地做了它? – Blaze356