[Solved] Sprite does not receive mouse events

This is a highly unusual issue that seems simple but I cannot for the life of me figure out why the sprite I have in my code will not receive mouse events. I have done traces, checked other resources, and from what I can tell I am doing absolutely nothing wrong other than the fact that Flash is a terrible platform. I am tempted to create a StackTrace variable and write the output via trace just so I know what the hell is going on behind-the-scenes but I think that’s just an overreaction for what is otherwise a simple workaround.

Here is my code:

class Main extends Sprite 
{
    
    private var editor:CodeEditor;
    private var _codeButton:BitmapData;
    private var _codeButtonDown:BitmapData;
    private var _btnCode:Bitmap;
    private var _btnCodeSprite:Sprite;
    private var _editorShown:Bool;
    
    public function new() 
    {
        super();
        
        editor = new CodeEditor();
        editor.ClassPaths = "Array;Date;DateTools;EReg;IntIterator;Lambda;List;Math;Reflect;Std;String;StringBuf;StringTools;Sys;" +
                                "Type;Xml";
        editor.EnumPaths = "ValueType";
        editor.x = stage.stageWidth - editor.width;
        editor.y = 30;
        editor.visible = false;
        
        _codeButton = Assets.getBitmapData("img/CodeButton.png");
        _codeButtonDown = Assets.getBitmapData("img/CodeButtonDown.png");
        
        _btnCode = new Bitmap(_codeButton);
        _btnCode.x = stage.stageWidth - 50;
        _btnCode.y = 0;
        
        _btnCodeSprite = new Sprite();
        _btnCodeSprite.x = stage.stageWidth - 50;
        _btnCodeSprite.y = 0;
        _btnCodeSprite.addChild(_btnCode);
        
        addEventListener(Event.ADDED_TO_STAGE, init);
    }
    
    private function init(e:Event):Void
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        
        _btnCodeSprite.addEventListener(MouseEvent.CLICK, _btnCode_OnClick);
        
        addChild(_btnCode);
        addChild(editor);
    }
    
    private function _btnCode_OnClick(e:MouseEvent):Void
    {
        _editorShown = !_editorShown;

        _btnCode.bitmapData = _editorShown ? _codeButtonDown : _codeButton;
        editor.visible = _editorShown;
    }
}

Can someone tell me what I am doing wrong here?

Hmmmm, I think you just rushed things through and forgot to check some things. :stuck_out_tongue_winking_eye:

You were adding the bitmap to stage, not the button. :sweat_smile:

Change this part:

        _btnCode.x = stage.stageWidth - 50;
        _btnCode.y = 0;

To this:

        _btnCode.x = 0;
        _btnCode.y = 0;

And also change this part:

        addChild(_btnCode);
        addChild(editor);

To this:

        addChild(_btnCodeSprite );
        addChild(editor);

I think this will work. I checked here and it worked normally. :smile:

Oh wow, I completely missed that. Thanks… I need to read my code more often lol