【发布时间】:2011-10-24 21:29:27
【问题描述】:
在我的 Flash 程序中,我试图在一个对象中运行一个函数。该对象使用一个名为“SkullDemon.as”的类。我试图运行的函数称为“moveSkullDemon();”此函数在“SkullDemon.as”类中编码。
我的主要文档类是“TME2d_Main.as”。在该文件中,我创建了一个 Skull Demon 类的实例,如下所示:
public var skullD1b:SkullDemon;
skullD1b = new SkullDemon();
skullD1b.x = 2990.75;
skullD1b.y = 836.95;
skullDemonContainer.addChild(skullD1b);
存储 SkullDemon 实例的“skullDemonContainer”被放置到另一个名为“envContainer”的容器中,该容器使用以下代码行添加到屏幕:
this.addChild(envContainer);
SkullDemon 实例被创建并加载到屏幕上就好了。当我尝试运行 SkullDemon 类中的任何函数时,就会出现问题。
如前所述,我尝试为“skullD1b”实例调用“SkullDemon.moveSkullDemon()”函数。目前,“moveSkullDemon()”函数只是将 SkullDemon 对象移动到左侧。
我尝试从 SkullDemon 的构造函数中调用此函数,但这不起作用。事实上,在 SkullDemon 的构造函数中编写的任何代码都不会在创建 Skull Demon 对象时执行。我试图在调用“moveSkullDemon()”的 SkullDemon 类中创建一个 EventListener,但它什么也没做。
我尝试了以下方法来运行“moveSkullDemon()”函数:
1:像这样从“TME2d_Main”调用“moveSkullDemon()”:
skullD1b.moveSkullDemon();
但这带来了一个错误 (#1006),说 'moveSkullDemon' 不是一个函数。
2:除了函数调用中没有括号之外,与 #1 做同样的事情:
skullD1b.moveSkullDemon;
3:在 'TME2d_Main' 中为调用其 'moveSkullDemon' 函数的 'skullD1b' 实例创建一个 EventListener:
skullD1b.addEventListener(Event.ADDED, skullD1b.moveSkullDemon);
这会引发另一个错误:“错误 #2007:参数侦听器必须为非空。”它还不断发现“空”引用错误:
"1009: Cannot access a property or method of a null object reference.
at classes::TME2d_Main/controlPlayer()"
我不知道为什么这个问题是指 'controlPlayer()' 方法,因为它根本不涉及 Skull Demon 实例。 'controlPlayer()' 通过 EventListener 调用,在 'TME2d_Main' 中使用此代码:
player.addEventListener(Event.ENTER_FRAME, controlPlayer);
4:为调用 move 函数的“SkullDemonContainer”创建一个 EventListener:
skullDemonContainer.addEventListener(Event.ADDED, skullD1b.moveSkullDemon);
这带来了与 #3 中相同的错误。
我这几天一直有这个问题,不知道是什么原因造成的。这是我在“moveSkullDemon()”函数中的代码:
public function moveSkullDemon() {
trace("skull demon moving!");
this.x -= 5;
// If the Player has not hit the 'floor,' increase his falling
//speed
if (! floor.hitTestPoint(this.x, this.y, true)) {
this.y += this.sdGravity;
// The Skull Demon is not on the ground when he's not touching it
sdOnGround = false;
}
// Increase the 'sdYVel' variable so that the Skull Demon will fall
// progressively faster down the screen. This code technically
// runs "all the time" but in reality it only affects the Skull Demon
// when he's off the ground.
sdYVel += sdGravity;
// Increase the Skull Demon's 'y' coordinate by the 'sdYVel' value
if (! sdOnGround) {
this.y += sdYVel;
}
}
感谢您提供的任何帮助。
【问题讨论】:
-
我应该提到,在我为我的项目使用包式系统之前,从“SkullDemon.as”的构造函数中调用“moveSkullDemon()”是可行的。某些情况迫使我使用项目系统。
标签: flash actionscript-3 class object event-listener