我正在尝试在 createJS 标签上注册 HitTest 。
我有两个问题:
1)正如您从下图中看到的, HitTest 仅在我将鼠标悬停在红球上而不是标签上时注册。我认为这是因为球的尺寸要大得多。如何进行标签的 HitTest ?
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);
}
最佳答案
问题似乎是您在 .hitTest() 中使用全局坐标,而它需要本地坐标。使用 globalToLocal 或 localX/Y(在最新的 EaselJS 版本中)来转换鼠标位置。
您可以随时运行 hitTest。它可以是鼠标移动、单击、计时器 - 任何对您的应用程序有意义的方式。
关于javascript - createJS 标签 HitTest ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21785739/