ActionScript 3.0中激活事件监听的对象 [英] Actionscript 3.0 Activating EventListener for an Object

查看:115
本文介绍了ActionScript 3.0中激活事件监听的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Flash程序,我想运行一个对象中的功能。该对象使用一类称为SkullDemon.as。我想运行的功能被称为moveSkullDemon();该功能中的SkullDemon.as类codeD。

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.

我的主文档类是TME2d_Main.as。在该文件中创建这样的骷髅妖类的一个实例:

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

的'skullDemonContainer',其存储SkullDemon实例中,放入另一个容器中,被称为envContainer',这是使用这条线的code加入到屏幕:

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

该SkullDemon实例被创建并加载到屏幕上就好了。当我尝试运行SkullDemon类中的任何功能时,会出现问题。

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.

正如前面提到的,我尝试调用'SkullDemon.moveSkullDemon()函数的'skullD1b实例。就目前而言,moveSkullDemon()函数只是移动SkullDemon对象的左侧。

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.

我打过电话从SkullDemon的构造函数这个功能,但不起作用。事实上,不执行任何code写的SkullDemon的构造函数中创建了一个骷髅妖对象时。我试图创建调用SkullDemon类中的EventListenermoveSkullDemon(),但什么都不做。

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.

我曾尝试下面的方法来获取moveSkullDemon()函数来运行:

I have tried the following methods to get the 'moveSkullDemon()' function to run:

1:调用moveSkullDemon()'从'TME2d_Main是这样的:

1: Calling 'moveSkullDemon()' from 'TME2d_Main' like this:

skullD1b.moveSkullDemon();

但是,这带来了一个错误(#1006),他说,moveSkullDemon不是一个函数。

But this brings up an error (#1006), saying that 'moveSkullDemon' is not a function.

2:在做同样的事情,#1,只是我有函数调用中没有括号:

2: Doing the same thing as #1 except that I have no parenthesis within the function call:

skullD1b.moveSkullDemon;

3:创建为skullD1b实例调用其moveSkullDemon功能内TME2d_Main'的事件监听器:

3: Creating an EventListener within 'TME2d_Main' for the 'skullD1b' instance that calls its 'moveSkullDemon' function:

skullD1b.addEventListener(Event.ADDED, skullD1b.moveSkullDemon);

这带来了另一个错误:错误#2007:参数监听器必须为非空它也不断地发现一个空引用错误:

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()"

我不知道为什么这个问题指的是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:创建事件监听器的'SkullDemonContainer调用移动功能:

4: Creating an EventListener for the 'SkullDemonContainer' that calls the move function:

skullDemonContainer.addEventListener(Event.ADDED, skullD1b.moveSkullDemon);

这带来了同样的错误在#3。

This brings up the same error as in #3.

我一直有这个问题了几天,现在不知道是什么原因造成的。这里是我中的moveSkullDemon()'code函数:

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.

推荐答案

如果我理解正确的,你正试图调度事件对你的显示对象,因此调用一个函数?

If I understand right, you are attempting to dispatch an event against your display object, therefore calling a function?

当你实例化你的头骨妖对象,事件要处理类型添加事件侦听器。例如,如果您创建了一个DemonEvent类:

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

当你要拨打的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));

您可以将code中的事件处理程序中,或从事件处理code调用你的函数:

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

为例DemonEvent如果你需要:

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);
        }

    }
}

更新

基于您的评论,这听起来像你确定把code键移动你的对象最好的地方。你的头骨恶魔就能到本身,还是因为你的TME2d_Main类添加头骨妖,起初定位它,因为你需要打的测试对地板,它可能会更有意义把这种逻辑在TME2d_Main类。

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

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

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
        //      {
        //      }

    }
}

这篇关于ActionScript 3.0中激活事件监听的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆