使用自定义事件的AS3引用错误

时间:2011-12-25 08:03:56

标签: flash actionscript-3

* 编辑 - 当我尝试更改动画片段的图形属性时,我发现给我错误的是什么。 *

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

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

这是我的代码。 下面的代码创建一个新的Grid。然后添加自定义事件。悬停事件工作正常,没有错误。 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,它添加了两个mouseEvent,然后触发自定义事件。

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

    }
}

1 个答案:

答案 0 :(得分: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);
        }

    }
}