[SOLVED] Mouse Click Image Event Problem

I have solved this issue and I have pasted my code below. I’ve also left my original question to help people find this issue. I’m sure there are many optimizations that it could undergo (which I welcome). I felt like there wasn’t much working code for me to view for this functionality so I figured leaving my fix would help someone else. The problem I was facing was that Bitmaps could not handle Mouse Events. Though my code would still compile, no event would ever fire correctly.

I’m trying to make a minesweeper like game for my first project in OpenFL. I’ve gotten an 8 by 8 grid of images to display and I’ve gotten the cursor to change over each image like the image is clickable. I expect “Pressed” to print in the terminal every time I press any image; however, this does not happen. Nothing is printed. The picture is a 90x90 grey cube. Below is my code:


import openfl.events.MouseEvent;
import openfl.Assets;
import openfl.display.Bitmap;
import openfl.display.Sprite;
import openfl.Vector;

class Main extends Sprite {
  public function new () {

    super ();
    this.buttonMode = true;
    this.mouseChildren = false;

    var length = 8;
    var tiledata = (Assets.getBitmapData("assets/tile.jpg"));
    var tile = new haxe.ds.Vector(length);

    function onClick(e:Dynamic){

    for(i in 0...tile.length) {
      tile[i] = new haxe.ds.Vector(length);
      for(o in 0...tile[i].length) {
        tile[i][o] = new Bitmap(tiledata);
        tile[i][o].y = i * 100;
        tile[i][o].x = o * 100;
        addEventListener(MouseEvent.CLICK, onClick);

just met the same issue… my way of solving it was… put Bitmap inside a wrapper MovieClip and add eventlistener to it

1 Like