【问题标题】:createJS hittest for labelcreateJS hittest 标签
【发布时间】:2014-02-14 17:26:35
【问题描述】:

我正在尝试在 createJS 标签上注册命中测试。

我有两个问题:

1) 如下图所示,只有当我将鼠标悬停在红球上时才会注册命中测试,而不是标签。我认为这是因为球的尺寸要大得多。如何获得标签的命中测试?

2) CreateJS 文档,http://www.createjs.com/tutorials/HitTest/hitTest.html,表明我需要将hitTest 事件放在刻度内。我宁愿不把它放在那里,因为我不希望浏览器一直浪费资源检查 hittest。我可以将hittest 代码放在jQuery doc ready 之类的地方吗?

stage = new createjs.Stage("demoCanvas");
stage.mouseMoveOutside = true;

circle = stage.addChild(new createjs.Shape());
circle.graphics.beginFill("red").drawCircle(50,50,50);
circle.x = 0;
circle.y = 0;

mylabel = new createjs.Text("testing", "14px Arial", "white");
mylabel.x = 300;
mylabel.y = 100;
stage.addChild(circle, mylabel);                


function tick(event) {  
    if (circle.hitTest(stage.mouseX, stage.mouseY)) { 
        log("ball hit"); 
    }
    if (mylabel.hitTest(stage.mouseX, stage.mouseY)) { 
        log("label hit"); 
    }
    stage.update(event);    
}       

【问题讨论】:

    标签: javascript jquery html hittest createjs


    【解决方案1】:
    1. 看起来问题是您在 .hitTest() 中使用全局坐标,而它需要局部坐标。使用 globalToLocal 或 localX/Y(在最新的 EaselJS 版本中)来转换鼠标位置。

    2. 您可以随时运行 hitTest。它可以是鼠标移动、单击、计时器 - 对您的应用程序有意义的任何事情。

    【讨论】:

    • 1) 我想我不完全了解 localToGlobal vs globalToLocal vs localToLocal 2) 为什么我不需要转换红球的坐标?
    • 3) hitTest 是否需要在 tick 事件中才能在 mousehover 上注册?
    猜你喜欢
    • 1970-01-01
    • 2017-04-05
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多