Actionscript 3.0 激活对象的事件监听器
在我的 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);
使用以下代码行添加到屏幕上:
this.addChild(envContainer);
存储 SkullDemon 实例的“skullDemonContainer”被放置到另一个名为“envContainer”的容器中,该容器 SkullDemon 实例已创建并加载到屏幕上。当我尝试运行 SkullDemon 类中的任何函数时,就会出现问题。
如前所述,我尝试为“skullD1b”实例调用“SkullDemon.moveSkullDemon()”函数。目前,“moveSkullDemon()”函数只是将 SkullDemon 对象移动到左侧。
我尝试从 SkullDemon 的构造函数调用此函数,但这不起作用。事实上,在创建 Skull Demon 对象时,不会执行 SkullDemon 构造函数中编写的任何代码。我尝试在 SkullDemon 类中创建一个调用“moveSkullDemon()”的 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()”方法,因为它根本不涉及骷髅恶魔实例。 “controlPlayer()”是通过“TME2d_Main”中的以下代码通过事件监听器调用的:
player.addEventListener(Event.ENTER_FRAME, controlPlayer);
4:为调用移动函数的“SkullDemonContainer”创建事件监听器:
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;
}
}
感谢您提供的任何帮助。
In my Flash program, I am trying to run a function within an object. The object uses a class called "SkullDemon.as." The function I am trying to run is called "moveSkullDemon();" this function is coded within the "SkullDemon.as" class.
My main document class is "TME2d_Main.as." Within that file I create an instance of the Skull Demon class like this:
public var skullD1b:SkullDemon;
skullD1b = new SkullDemon();
skullD1b.x = 2990.75;
skullD1b.y = 836.95;
skullDemonContainer.addChild(skullD1b);
The 'skullDemonContainer', which stores the SkullDemon instances, is placed into another container, called 'envContainer,' which is added to the screen using this line of code:
this.addChild(envContainer);
The SkullDemon instance is created and loads onto the screen just fine. The problem occurs when I try to run any function within the SkullDemon class.
As mentioned before, I try to call the 'SkullDemon.moveSkullDemon()' function for the 'skullD1b' instance. For now, the 'moveSkullDemon()' function just moves the SkullDemon object to the left.
I tried calling this function from the SkullDemon's constructor function, but that does not work. In fact, any code written within the SkullDemon's constructor is not executed when a Skull Demon object is created. I have tried to create an EventListener within the SkullDemon class that calls 'moveSkullDemon(),' but that does nothing.
I have tried the following methods to get the 'moveSkullDemon()' function to run:
1: Calling 'moveSkullDemon()' from 'TME2d_Main' like this:
skullD1b.moveSkullDemon();
But this brings up an error (#1006), saying that 'moveSkullDemon' is not a function.
2: Doing the same thing as #1 except that I have no parenthesis within the function call:
skullD1b.moveSkullDemon;
3: Creating an EventListener within 'TME2d_Main' for the 'skullD1b' instance that calls its 'moveSkullDemon' function:
skullD1b.addEventListener(Event.ADDED, skullD1b.moveSkullDemon);
This brings up another error: "Error #2007: Parameter listener must be non-null." It also constantly finds a "null" reference error:
"1009: Cannot access a property or method of a null object reference.
at classes::TME2d_Main/controlPlayer()"
I do not know why this problem refers to the 'controlPlayer()' method, as it does not involve the Skull Demon instance at all. 'controlPlayer()' is called through an EventListener with this code within 'TME2d_Main':
player.addEventListener(Event.ENTER_FRAME, controlPlayer);
4: Creating an EventListener for the 'SkullDemonContainer' that calls the move function:
skullDemonContainer.addEventListener(Event.ADDED, skullD1b.moveSkullDemon);
This brings up the same error as in #3.
I have been having this problem for a few days now and do not know what is causing it. Here is my code within the 'moveSkullDemon()' function:
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;
}
}
Thanks for any help you may offer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确,您正在尝试针对您的显示对象分派事件,因此调用函数?
当您实例化您的头骨恶魔对象时,请为您希望处理的事件类型添加一个事件侦听器。例如,如果您创建了一个“DemonEvent”类:
当您想要调用“moveDemonHandler”函数时,您可以针对该对象分派一个事件:
您可以将代码放置在事件处理程序中,或者从事件处理程序代码中调用您的函数:
例如“DemonEvent”,如果您需要:
更新
根据您的评论,听起来您正在确定放置代码来移动对象的最佳位置。您的头骨恶魔可以自行移动,或者由于您的 TME2d_Main 类添加了头骨恶魔,最初将其定位,并且因为您需要对地板进行撞击测试,所以将该逻辑放入 TME2d_Main 类中可能更有意义。
这个例子没有使用事件;不过,希望能帮助您入门。如果您想要一个使用事件的示例,请告诉我。
这里有两个类作为示例实现:
TME2d_Main.as
SkullDemon.as
If I understand right, you are attempting to dispatch an event against your display object, therefore calling a function?
When you instantiate your skull demon object, add an event listener for the type of event you wish to handle. For example, if you created a "DemonEvent" class:
When you want to call the "moveDemonHandler" function, you dispatch an event against the object:
You can place code within the event handler, or call your function from the event handler code:
Example "DemonEvent" if you need:
UPDATE
Based upon your comment, it sounds like you are determining the best place to put code to move your object. Your skull demon could move itself, or since your TME2d_Main class adds the skull demon, positions it initially, and because you need to hit test against a floor, it might make more sense to put that logic in the TME2d_Main class.
This example doesn't use events; however, hopefully helps you get started. Let me know if you'd rather have an example that uses events instead.
here are two classes as an example implementation:
TME2d_Main.as
SkullDemon.as
在您的 TME2d_Main.as 文件中,您声明您的代码如下所示。
最后 4 行需要放入一个函数中。您不能在函数外部的类中实例化或赋值。
好吧,你可以像这样实例化,但所有其他代码都需要功能化(甚至是一个词)。
你最好的选择就是这样做。
如果这不能解决问题,请发布完整的 TME2d_Main.as 文件。
in your TME2d_Main.as file you state you your code is like so.
The last 4 lines need to be put in a function. You can not instantiate or assign values in a class outside of a function.
Well you can instantiate like so, but all other code needs to be functionalized(is that even a word).
Your best bet is to do this.
If this doesn't fix it then post your full TME2d_Main.as file.