Question

I'm trying to code a sort of strategy game through FlashDevelop and I'm getting problems when trying to use Events (particularly MouseEvents). It's not so much that the events are returning errors, it's just that they don't do anything, not even getting a trace.

I'm trying to make the hexagons HexObject image invisible when clicked on (just a simple test to see if the MouseEvent is actually working).

This is my code:

Main.as

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

    }

}

HexGrid.as

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

    /**
     * ...
     * @author Dean Sinclair
     */
    public class HexGrid extends Sprite {

        public static var HEX_RADIUS:int = 0;
        public static var HEX_DIAMETER:int = 0;
        public static var GRID_WIDTH:int = 0;
        public static var GRID_HEIGHT:int = 0;
        public var TOTAL_X:int = 0;

        public var MIN_X:int = 0;
        public var MAX_X:int = 0;
        public var MIN_Y:int = 0;
        public var MAX_Y:int = 0;

        public var Grid:Array;
        public var yArray:Array;

        public function HexGrid(min_x:int, max_x:int, min_y:int, max_y:int) {
            super();
            MIN_X = min_x;
            MAX_X = max_x;
            MIN_Y = min_y;
            MAX_Y = max_y;
        }

        public function initialiseGrid():void {
            setGridDetails();
            setLineLengths();
            setGridPositions();
        }

        public function setGridDetails():void {
            HEX_RADIUS = 25;
            HEX_DIAMETER = 2 * HEX_RADIUS;
            GRID_WIDTH = (((MAX_X - MIN_X) / HEX_DIAMETER) - 1);
            GRID_HEIGHT = ((((MAX_Y - 100) - MIN_Y) / (HEX_DIAMETER - (HEX_DIAMETER / 3))) - 3);
            TOTAL_X = GRID_WIDTH + Math.floor((GRID_HEIGHT - 1) / 2);
        }

        private function setLineLengths():void {
            yArray = new Array(TOTAL_X);
            for (var a:int = 0; a < TOTAL_X; a++) {
                yArray[a] = new Array(2);
            }
            for (var x:int = 0; x < TOTAL_X; x++) {
                if (x < GRID_WIDTH) {
                    yArray[x][0] = 0;
                }else {
                    yArray[x][0] = (x - GRID_WIDTH + 1) * 2;
                }
                yArray[x][1] = 1 + (2 * x);
                if (yArray[x][1] > GRID_HEIGHT) {
                    yArray[x][1] = GRID_HEIGHT;
                }
                trace("Line", x, "   starts at", yArray[x][0], "   ends at", yArray[x][1]);
            }
        }

        public var nox:int = 5;
        public var noy:int = 3;

        private function setGridPositions():void {
            var hexState:int = 4;

            Grid = new Array(TOTAL_X);
            for (var x:int = 0; x < TOTAL_X; x++) {
                Grid[x] = new Array(yArray[x][1]);
                for (var y:int = yArray[x][0]; y < yArray[x][1]; y++) {
                    if(nox!=4 || noy!=6){
                        Grid[x][y] = new HexObject(HEX_DIAMETER + (HEX_DIAMETER * x) - (HEX_RADIUS * y), HEX_DIAMETER + (HEX_DIAMETER * y) - ((HEX_DIAMETER / 3) * y), HEX_RADIUS, 2);
                    }
                } 
            }
        }
    }
}

HexObject.as

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

I've tried countless methods to get the events working, but none have had any success. Any sort of solution to this would be a lifesaver as I've been toiling over this for hours, thanks!

Was it helpful?

Solution

My problem was fixed by VBCPP, I was using the class Bitmap which cannot dispatch MouseEvents. The solution was to take image from HexObject and put it inside an container of type Sprite, with the logical one being the object it was in. I just had to add the following code inside HexObject.as:

this.addChild(image);

and then just refer to the Object in future as opposed to image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top