Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 Actionscript 3.0-鼠标事件不工作_Actionscript 3_Mouseevent_Stage - Fatal编程技术网

Actionscript 3 Actionscript 3.0-鼠标事件不工作

Actionscript 3 Actionscript 3.0-鼠标事件不工作,actionscript-3,mouseevent,stage,Actionscript 3,Mouseevent,Stage,我试图通过FlashDevelop编写一种策略游戏,但在尝试使用事件(特别是MouseeEvents)时遇到了问题。事件并不是返回错误,只是它们什么都不做,甚至没有得到跟踪 我试图使六边形HexObject图像在单击时不可见(只是一个简单的测试,看看MouseEvent是否实际工作) 这是我的代码: 梅因 package { import flash.display.Shape; import flash.display.Sprite; import flash.even

我试图通过FlashDevelop编写一种策略游戏,但在尝试使用事件(特别是MouseeEvents)时遇到了问题。事件并不是返回错误,只是它们什么都不做,甚至没有得到跟踪

我试图使六边形
HexObject
图像在单击时不可见(只是一个简单的测试,看看MouseEvent是否实际工作)

这是我的代码:

梅因

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

    /**
     * ...
     * @author Dean Sinclair
     */

    public class Main extends Sprite {

        public var gameInitialised:Boolean = false;
        public var MIN_X:int = 0;
        public var MAX_X:int = stage.stageWidth;
        public var MIN_Y:int = 0;
        public var MAX_Y:int = stage.stageHeight - 100;

        public var GameGrid:HexGrid = new HexGrid(MIN_X, MAX_X, MIN_Y, MAX_Y);
        public var blackBG:Shape = new Shape();

        public function Main():void {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            addEventListener(Event.ENTER_FRAME, update);
            // entry point
        }

        private function update(event:Event):void {
            if (gameInitialised == false) {
                GameGrid.initialiseGrid();
                initialiseBackground();
                gameInitialised = true;
            }

            updateGraphics();
        }

        public function drawGrid():void {
            for (var x:int = 0; x < GameGrid.TOTAL_X; x++) {
                for (var y:int = GameGrid.yArray[x][0]; y < GameGrid.yArray[x][1]; y++) {
                    if (x != GameGrid.nox || y != GameGrid.noy) {
                        GameGrid.Grid[x][y].update();
                        this.stage.addChild(GameGrid.Grid[x][y].image);
                    }
                }
            }
        }

        public function updateGraphics():void {
            this.stage.addChild(blackBG);
            drawGrid();
        }

        public function initialiseBackground():void {
            blackBG.graphics.beginFill(0x000000, 1);
            blackBG.graphics.lineStyle(10, 0xffffff, 1);
            blackBG.graphics.drawRect(0, 0, stage.stageWidth-1, stage.stageHeight-1);
            blackBG.graphics.endFill();
        }

    }

}

我尝试了无数的方法来让这些活动发挥作用,但都没有成功。任何解决这个问题的方法都是救命稻草,因为我已经为此辛劳了几个小时,谢谢

我的问题已由
VBCPP
解决,我使用的是类
位图
,它无法分派MouseEvents。解决方案是从
HexObject
中获取
图像
,然后将其放入类型为
Sprite
的容器中,其中的逻辑对象就是它所在的对象。我只需在HexObject中添加以下代码。如:

this.addChild(image);

然后只需参考未来的对象,而不是
image

在函数中有很多初始化代码,每个帧都会被调用,我会从提取代码开始,我已经重新设计了代码,这样初始化的代码不会在每一帧都被调用,而且更整洁,但仍然没有显示跟踪。就好像这些对象甚至没有被添加到舞台上,尽管它们显示正确。我建议编辑您的帖子以显示更新的代码。但不管怎么说,你的问题是图像是一个赢了的
位图
;t发送mouseevents,将其放入容器
Sprite
立即更新帖子。谢谢,这对我的问题很有帮助。我该如何将其放入精灵容器中呢?在HexObject中创建imageContainer精灵,然后创建imageContainer.addChild(图像)。现在,不要在其他类中引用obj.image,而是将其更改为imageContainer。虽然我会重构您的代码,只需将图像添加为HexObject(它是一个Sprite)的子对象,然后从其他类中删除.image的所有用法&只需处理该对象即可
package {
    import flash.display.Bitmap;
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    /**
     * ...
     * @author Dean Sinclair
     */
    public class HexObject extends Sprite {
        [Embed(source = "../images/hexagons/hex_darkRed.png")]
        private var DarkRedHex:Class;
        [Embed(source = "../images/hexagons/hex_lightBlue.png")]
        private var LightBlueHex:Class;
        [Embed(source = "../images/hexagons/hex_midGreen.png")]
        private var MidGreenHex:Class;

        public var image:Bitmap = new LightBlueHex();
        protected var state:int = 0;
        private var radius:int = 0;

        public function HexObject(xPos:int, yPos:int, hexRadius:int, hexState:int) {
            super();
            x = xPos;
            y = yPos;
            state = hexState;
            radius = hexRadius;
            checkState();
            initialiseGraphics();
        }

        private function checkState():void {
            switch(state) {
                case 1:     // plains
                    image = new MidGreenHex();
                    break;
                case 2:     // hills
                    break;
                case 3:     // rock
                    image = new DarkRedHex();
                    break;
                case 4:     // water
                    image = new LightBlueHex();
                    break;
                default:
                    break;
            }
        }

        private function initialiseGraphics():void {
            image.visible = true;
            image.width = radius * 2;
            image.height = radius * 2;
            image.x = x - radius;
            image.y = y - radius;
        }

        private function onMouseClick(e:MouseEvent):void {
            image.visible = false;
            trace("image.visible =", image.visible);
        }

        public function update():void {
            image.addEventListener(MouseEvent.CLICK, onMouseClick);
        }
    }
}
this.addChild(image);