【问题标题】:AS3 reference error using custom event使用自定义事件的 AS3 引用错误
【发布时间】:2011-12-25 17:49:57
【问题描述】:

*编辑 - 我发现错误是当我尝试更改影片剪辑的图形属性时。*

我收到此错误。 “ReferenceError:错误 #1069:在 flash.events.MouseEvent 上找不到属性项,并且没有默认值。在 PlayScreen/onClick() 中”。 我做了一些研究,大多数网站都说这是由于拼写错误。但据我所知,它看起来不错?

据我所知,我有两个事件的设置完全相同。一个是 onclick 触发的,另一个是 onhover 触发的。悬停事件工作正常,但是当我使用 onclick 事件时,我收到上面的错误。但是,即使我收到错误,该功能仍然可以正常工作。

这是我的代码。 下面的代码创建了一个新的网格。然后添加自定义事件。悬停事件工作正常,没有错误。 onclick 事件工作正常,但我得到了错误。

package  {
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class PlayScreen extends MovieClip {
    public var grid:Grid;

    public function PlayScreen() {
        grid = new Grid();
        grid.addEventListener( GridEvent.HOVER, onHover);
        grid.addEventListener( GridEvent.TILECLICK, onClick);
        grid.x = 0;
        grid.y = 0;
        addChild( grid );

    }

    public function onHover(event:*){
        event.item.graphics.beginFill(0x66ff66);
        event.item.graphics.lineStyle(2, 0x22ff22);
        event.item.graphics.drawRect(-30*0.5, -30*0.5, 30, 30);
        event.item.graphics.endFill();

    }

    public function onClick(event:*){
        event.item.graphics.beginFill(0x000000);
        event.item.graphics.lineStyle(2, 0x22ff22);
        event.item.graphics.drawRect(-30*0.5, -30*0.5, 30, 30);
        event.item.graphics.endFill();
    }

下面是我的网格类。它包含一个movieclip,其中添加了两个mouseEvents,然后触发自定义事件。

package  {
import flash.display.MovieClip;
import flash.events.MouseEvent;

public class Grid extends MovieClip {

    public function Grid() {
        var test = new MovieClip();
        test.x = 0;
        test.y = 0;
        test.graphics.beginFill(0x66ff66); 
        test.graphics.drawRect(-tileWidth*0.5, -tileHeight*0.5, tileWidth, tileHeight);
        test.graphics.endFill();
        test.addEventListener(MouseEvent.MOUSE_OVER, overTile);
        test.addEventListener(MouseEvent.MOUSE_DOWN, clickTile);
        addChild(test);
    }

    function overTile (event:*) {
        dispatchEvent( new GridEvent( GridEvent.HOVER, event.target) );
    }

    function clickTile(event:*) {
        dispatchEvent( new GridEvent( GridEvent.TILECLICK, event.target) );
    }

}

以下是我的自定义事件

   package  
{
    import flash.events.Event;
    public class GridEvent extends Event
    {
        public static const HOVER :String = "hover";
        public static const TILECLICK :String = "click";
        public var item;

        public function GridEvent (type:String, item)
        {
                this.item = item;
                super(type);
        }

    }
}

【问题讨论】:

  • 那是你的完整源代码吗?当您甚至没有为事件提供类型时,Flash 没有理由给出有关 MouseEvent 的错误。肯定有其他问题。
  • 这不是所有的代码,因为完整的代码相当广泛。但是我遇到了同样的问题。我发现这是我尝试更改图形属性的时候。如果我不这样做,我不会收到错误消息。所以我不明白为什么它给我一个 onclick 功能而不是悬停功能的错误?也谢谢你的回复:)

标签: flash actionscript-3


【解决方案1】:

尝试给eventitem 一个类型。 使用Sprite 而不是MovieClip

还有一件事我没有实现 - 如果您为您的图块创建了一个自定义 Tile 类,那么您可以将 GridEvent 中的项目类型设置为 Tile 而不是 Sprite/MovieClip。

这应该可以工作....

package  
{
    import flash.events.MouseEvent;
    import flash.display.MovieClip;
    public class PlayScreen extends MovieClip 
    {
        public var grid:Grid;

        public function PlayScreen() 
        {
            grid = new Grid();
            grid.addEventListener( GridEvent.HOVER, onHover);
            grid.addEventListener( GridEvent.TILECLICK, onClick);
            grid.x = 0;
            grid.y = 0;
            addChild( grid );

        }

        private function onHover(event:GridEvent):void
        {
            if (event.item != null)
            {
                var g:Graphics = event.item.graphics;
                g.beginFill(0x66ff66);
                g.lineStyle(2, 0x22ff22);
                g.drawRect(-30*0.5, -30*0.5, 30, 30);
                g.endFill();
            }
        }

        private function onClick(event:GridEvent):void
        {
            if (event.item != null)
            {
                var g:Graphics = event.item.graphics;
                g.beginFill(0x000000);
                g.lineStyle(2, 0x22ff22);
                g.drawRect(-30*0.5, -30*0.5, 30, 30);
                g.endFill();
            }
        }
    }
}

package  
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class Grid extends Sprite 
    {
        public function Grid() 
        {
            var test = new Sprite();
            test.x = 0;
            test.y = 0;
            test.graphics.beginFill(0x66ff66); 
            test.graphics.drawRect(-tileWidth*0.5, -tileHeight*0.5, tileWidth, tileHeight);
            test.graphics.endFill();
            test.addEventListener(MouseEvent.MOUSE_OVER, overTile);
            test.addEventListener(MouseEvent.MOUSE_DOWN, clickTile);
            addChild(test);

            test.mouseChildren = false;
        }

        private function overTile (event:MouseEvent):void 
        {
            if (event.cancelable)
            {
                // stop MouseEvent from bubbling
                event.stopImmediatePropagation();
            }
            dispatchEvent( new GridEvent( GridEvent.HOVER, event.target) );
        }

        private function clickTile(event:MouseEvent):void 
        {
            if (event.cancelable)
            {
                // stop MouseEvent from bubbling
                event.stopImmediatePropagation();
            }
            dispatchEvent( new GridEvent( GridEvent.TILECLICK, event.target) );
        }
    }
}

package  
{
    import flash.events.Event;
    public class GridEvent extends Event
    {
        public static const HOVER :String = "hover";
        public static const TILECLICK :String = "click";
        public var item:Sprite;

        public function GridEvent (type:String, item:Sprite)
        {
                this.item = item;
                super(type);
        }

    }
}

【讨论】:

  • 谢谢你解决了我的问题!你的例子很棒,我将创建一个 tile 类:)。
猜你喜欢
  • 2021-08-17
  • 1970-01-01
  • 2015-01-10
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多