flash cc createjs hittest works without hit

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 01:38:29

问题


the rect should be alpha=0.1 once the circle touches the rect . but if statement not working . it becomes 0.1 opacity without hitting

/* js

var circle = new lib.mycircle();
stage.addChild(circle);

var rect = new lib.myrect();
stage.addChild(rect);
rect.x=200;
rect.y=300;

circle.addEventListener('mousedown', downF);
function downF(e) {
stage.addEventListener('stagemousemove', moveF);
stage.addEventListener('stagemouseup', upF);
};
function upF(e) {

stage.removeAllEventListeners();
}
function moveF(e) {
circle.x = stage.mouseX;
circle.y = stage.mouseY;
}

if(circle.hitTest(rect))
{
  rect.alpha = 0.1;

}
stage.update();
*/

回答1:


The way you have used hitTest is incorrect. The hitTest method does not check object to object. It takes an x and y coordinate, and determines if that point in its own coordinate system has a filled pixel.

I modified your example to make it more correct, though it doesn't actually do what you are expecting:

circle.addEventListener('pressmove', moveF);
function moveF(e) {
    circle.x = stage.mouseX;
    circle.y = stage.mouseY;
    if (rect.hitTest(circle.x, circle.y)) {
        rect.alpha = 0.1;
    } else {
        rect.alpha = 1;
    }
    stage.update();
}

Key points:

  • Reintroduced the pressmove. It works fine.
  • Moved the circle update above the hitTest check. Otherwise you are checking where it was last time
  • Moved the stage update to last. It should be the last thing you update. Note however that you can remove it completely, because you have a Ticker listener on the stage in your HTML file, which constantly updates the stage.
  • Added the else statement to turn the alpha back to 1 if the hitTest fails.

Then, the most important point is that I changed the hitTest to be on the rectangle instead. This essentially says: "Is there a filled pixel at the supplied x and y position inside the rectangle?" Since the rectangle bounds are -49.4, -37.9, 99, 76, this will be true when the circle's coordinates are within those ranges - which is just when it is at the top left of the canvas. If you replace your code with mine, you can see this behaviour.

So, to get it working more like you want, you can do a few things.

  1. Transform your coordinates. Use localToGlobal, or just cheat and use localToLocal. This takes [0,0] in the circle, and converts that coordinate to the rectangle's coordinate space.

Example:

var p = rect.localToLocal(0, 0, circle);
if (rect.hitTest(p.x, p.y)) {
    rect.alpha = 0.1;
} else {
    rect.alpha = 1;
}
  1. Don't use hitTest. Use getObjectsUnderPoint, pass the circle's x/y coordinate, and check if the rectangle is in the returned list.

Hope that helps. As I mentioned in a comment above, you can not do full shape collision, just point collision (a single point on an object).



来源:https://stackoverflow.com/questions/36257762/flash-cc-createjs-hittest-works-without-hit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!