Actionscript 3.0 激活对象的事件监听器

发布于 2024-12-11 20:02:32 字数 2904 浏览 0 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

中性美 2024-12-18 20:02:32

如果我理解正确,您正在尝试针对您的显示对象分派事件,因此调用函数?

当您实例化您的头骨恶魔对象时,请为您希望处理的事件类型添加一个事件侦听器。例如,如果您创建了一个“DemonEvent”类:

// instantiate and add event listener:
skullD1b = new SkullDemon();
skullD1b.addEventListener(DemonEvent.MOVE, moveDemonHandler);

当您想要调用“moveDemonHandler”函数时,您可以针对该对象分派一个事件:

// do call this event listener, dispatch an event
// against the object
skullD1b.dispatchEvent(new DemonEvent(DemonEvent.MOVE));

您可以将代码放置在事件处理程序中,或者从事件处理程序代码中调用您的函数:

    protected function moveDemonHandler(event:DemonEvent):void
    {
        moveSkullDemon();
    }

    protected function moveSkullDemon():void
    {
        // ... your code here.
    }

例如“DemonEvent”,如果您需要:

package
{
    import flash.events.Event;

    public class DemonEvent extends Event
    {

        public static const MOVE:String = "MOVE";

        public function DemonEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            super(type, bubbles, cancelable);
        }

    }
}

更新

根据您的评论,听起来您正在确定放置代码来移动对象的最佳位置。您的头骨恶魔可以自行移动,或者由于您的 TME2d_Main 类添加了头骨恶魔,最初将其定位,并且因为您需要对地板进行撞击测试,所以将该逻辑放入 TME2d_Main 类中可能更有意义。

这个例子没有使用事件;不过,希望能帮助您入门。如果您想要一个使用事件的示例,请告诉我。

这里有两个类作为示例实现:

TME2d_Main.as

package
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class TME2d_Main extends Sprite
    {
        /** skull demon instance */
        public var skullD1b:SkullDemon;

        /** floor */
        public var floor:Sprite;

        /** constructor */
        public function TME2d_Main()
        {
            super();

            // best if implemented on addedToStage
            skullD1b = new SkullDemon();
            skullD1b.x = stage.stageWidth * 0.5;
            skullD1b.y = 1;
            addChild(skullD1b);

            // listen for enter frame
            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }

        /** every frame, move skull demon */
        protected function enterFrameHandler(event:Event):void
        {
            // call local function of this class
            moveSkullDemon();
        }

        protected function moveSkullDemon():void
        {
            // since this TME2d_Main class defined and positioned
            // skullD1b, it probably makes sense for this class
            // to continue positioning it, especially if you need to
            // hit test again a floor, which is probably also defined
            // in this class?
            if (skullD1b.hitTestObject(floor))
            {
                // skull is on floor 
                skullD1b.sdOnGround = true;

                // end animation
                removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
            }
            else
            {
                // otherwise, skull is in air and falling
                skullD1b.y *= skullD1b.sdYVel;
            }
        }

        // if you wanted to have skull demon
        // move itself, you would call:
        //
        //     skullD1b.moveSkullDemon()
        //
        // ...but make sure that function exists
        // in SkullDemon class.

    }
}

SkullDemon.as

package
{
    import flash.display.Sprite;

    public class SkullDemon extends Sprite
    {

        /** acceleration factor */
        public var sdYVel:Number = 1.05;

        /** whether skull demon is on ground */
        public var sdOnGround:Boolean = false;

        /** constructor */
        public function SkullDemon()
        {
            super();
        }

        // if you want SkullDemon to move itself,
        // make sure you define a moveSkullDemon function:
        //
        //      public function moveSkullDemon():void
        //      {
        //      }

    }
}

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:

// instantiate and add event listener:
skullD1b = new SkullDemon();
skullD1b.addEventListener(DemonEvent.MOVE, moveDemonHandler);

When you want to call the "moveDemonHandler" function, you dispatch an event against the object:

// do call this event listener, dispatch an event
// against the object
skullD1b.dispatchEvent(new DemonEvent(DemonEvent.MOVE));

You can place code within the event handler, or call your function from the event handler code:

    protected function moveDemonHandler(event:DemonEvent):void
    {
        moveSkullDemon();
    }

    protected function moveSkullDemon():void
    {
        // ... your code here.
    }

Example "DemonEvent" if you need:

package
{
    import flash.events.Event;

    public class DemonEvent extends Event
    {

        public static const MOVE:String = "MOVE";

        public function DemonEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            super(type, bubbles, cancelable);
        }

    }
}

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

package
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class TME2d_Main extends Sprite
    {
        /** skull demon instance */
        public var skullD1b:SkullDemon;

        /** floor */
        public var floor:Sprite;

        /** constructor */
        public function TME2d_Main()
        {
            super();

            // best if implemented on addedToStage
            skullD1b = new SkullDemon();
            skullD1b.x = stage.stageWidth * 0.5;
            skullD1b.y = 1;
            addChild(skullD1b);

            // listen for enter frame
            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }

        /** every frame, move skull demon */
        protected function enterFrameHandler(event:Event):void
        {
            // call local function of this class
            moveSkullDemon();
        }

        protected function moveSkullDemon():void
        {
            // since this TME2d_Main class defined and positioned
            // skullD1b, it probably makes sense for this class
            // to continue positioning it, especially if you need to
            // hit test again a floor, which is probably also defined
            // in this class?
            if (skullD1b.hitTestObject(floor))
            {
                // skull is on floor 
                skullD1b.sdOnGround = true;

                // end animation
                removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
            }
            else
            {
                // otherwise, skull is in air and falling
                skullD1b.y *= skullD1b.sdYVel;
            }
        }

        // if you wanted to have skull demon
        // move itself, you would call:
        //
        //     skullD1b.moveSkullDemon()
        //
        // ...but make sure that function exists
        // in SkullDemon class.

    }
}

SkullDemon.as

package
{
    import flash.display.Sprite;

    public class SkullDemon extends Sprite
    {

        /** acceleration factor */
        public var sdYVel:Number = 1.05;

        /** whether skull demon is on ground */
        public var sdOnGround:Boolean = false;

        /** constructor */
        public function SkullDemon()
        {
            super();
        }

        // if you want SkullDemon to move itself,
        // make sure you define a moveSkullDemon function:
        //
        //      public function moveSkullDemon():void
        //      {
        //      }

    }
}
伴随着你 2024-12-18 20:02:32

在您的 TME2d_Main.as 文件中,您声明您的代码如下所示。

    public var skullD1b:SkullDemon;
    skullD1b = new SkullDemon();
    skullD1b.x = 2990.75;
    skullD1b.y = 836.95;
    skullDemonContainer.addChild(skullD1b);

最后 4 行需要放入一个函数中。您不能在函数外部的类中实例化或赋值。
好吧,你可以像这样实例化,但所有其他代码都需要功能化(甚至是一个词)。

public var skullD1b:SkullDemon = new SkullDemon();

// TME2d_Main is the constructor and should be called automatically
public function TME2d_Main( ):void{
    skullD1b.x = 2990.75;
    skullD1b.y = 836.95;
    skullDemonContainer.addChild(skullD1b);
}

你最好的选择就是这样做。

public var skullD1b:SkullDemon;

// TME2d_Main is the constructor and should be called automatically
public function TME2d_Main( ):void{
    skullD1b = new SkullDemon();
    skullD1b.x = 2990.75;
    skullD1b.y = 836.95;
    skullDemonContainer.addChild(skullD1b);
}

如果这不能解决问题,请发布完整的 TME2d_Main.as 文件。

in your TME2d_Main.as file you state you your code is like so.

    public var skullD1b:SkullDemon;
    skullD1b = new SkullDemon();
    skullD1b.x = 2990.75;
    skullD1b.y = 836.95;
    skullDemonContainer.addChild(skullD1b);

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).

public var skullD1b:SkullDemon = new SkullDemon();

// TME2d_Main is the constructor and should be called automatically
public function TME2d_Main( ):void{
    skullD1b.x = 2990.75;
    skullD1b.y = 836.95;
    skullDemonContainer.addChild(skullD1b);
}

Your best bet is to do this.

public var skullD1b:SkullDemon;

// TME2d_Main is the constructor and should be called automatically
public function TME2d_Main( ):void{
    skullD1b = new SkullDemon();
    skullD1b.x = 2990.75;
    skullD1b.y = 836.95;
    skullDemonContainer.addChild(skullD1b);
}

If this doesn't fix it then post your full TME2d_Main.as file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文