【问题标题】:Eventlistener not listening when removing cursor from stage从舞台上移除光标时,事件监听器不监听
【发布时间】:2023-03-03 05:53:22
【问题描述】:

当我将舞台光标设置为无时。我无法以某种方式访问​​我的形状的(Startscreen.startButton)事件监听器。当我不隐藏我的光标时,我可以点击它。

我做错了吗?

function initStartscreen() {
    startscreenLayer = new createjs.Container();

    createMenuCursor();

    stage.addEventListener("stagemousemove", mouseMove);
    stage.enableMouseOver(2);
    stage.cursor = 'none';

    Startscreen = new Startscreen();

    startscreenLayer.addChild(Startscreen.background);

    startscreenLayer.addChild(Startscreen.startButton);
    startscreenLayer.addChild(Startscreen.startText);

        startscreenLayer.addChild(Startscreen.highscoreButton);
        startscreenLayer.addChild(Startscreen.highscoreText);

        stage.addChildAt(startscreenLayer);

    stage.update();

    Startscreen.startButton.addEventListener('click', function() {
        alert(1);
    });
}

function Startscreen() {
    this.background = new createjs.Shape();
    this.background.graphics
        .beginFill('pink')
        .drawRect(0, 0, stage.canvas.width, stage.canvas.height);

    this.startButton = new createjs.Shape();
    this.startButton.graphics
        .beginFill('#000')
        .drawRect(0, 0, 250, 50)
    this.startButton.x = (stage.canvas.width/2)-125;
    this.startButton.y = 150;
    this.startButton.name = "startButton";

    this.startText = new createjs.Text('Start', '30px Calibri', '#FFF');
    this.startText.x = this.startButton.x + 95;
    this.startText.y = this.startButton.y + 7;
}

即时更新: 即使我把所有东西都去掉,只在我的 onload 中留下initStartscreen(),它也不起作用。

这一定是一些分层问题或其他什么,因为我在我的高分层中创建了一个后退按钮,当我通过从主菜单导航到那里使用它时它可以工作。但是在我玩完游戏并转到同一页面/功能后,后退按钮不起作用。

有没有办法总是让我添加到顶部的图层?

【问题讨论】:

    标签: javascript html5-canvas easeljs createjs


    【解决方案1】:

    我必须承认我有点困惑,我可能需要你的澄清!

    我是否理解您隐藏了光标,然后期望用户找到一个带有“不可见”光标的开始按钮? ——你对你的用户很刻薄吗? :p

    如果您觉得stage.cursor='none' 行为不端,您可以在最新版本的 IE、FF 和 Chrome 中使用 CSS 将光标设置为“无”。在“不可见”状态下,鼠标事件仍然被触发。下面的代码和一个小提琴:http://jsfiddle.net/m1erickson/qBw9P/

    $("#theStageCanvas").click(function(){ $("#theStageCanvas").css("cursor","none"); });
    

    或者您是否有多个阶段,并且您想让一个底部阶段响应点击事件,即使它上面有一个顶部阶段?

    你可以像这样关闭顶层的事件监听:

    topStage.onClick=null;
    topStage.onMouseDown=null;
    topSstage.onMouseUp=null;
    topStage.onMouseMove=null;
    

    【讨论】:

    • 哈哈,显然我在 mouseX 和 mouseY 之后有一个位图 ;-) 点击的东西在画布上。所以恐怕 jQuery 并不是一个合适的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 2014-02-21
    • 2021-08-23
    • 2021-03-24
    相关资源
    最近更新 更多