简体   繁体   English

globalToLocal表现不符合我的预期吗? (非常确定我没有得到)

[英]globalToLocal not behaving as I expected? (pretty sure I'm just not getting it)

I've asked a few questions on this already, for other issues on it, but I've almost finished it! 我已经问了几个问题,还有其他问题,但是我已经快完成了!

I'm working on a Drag and Drop interaction in Animate CC, and I've almost got it working. 我正在Animate CC中进行拖放交互,并且几乎可以正常工作。 It recognizes on drag and on drop, but the locations on the target areas seems off. 它可以识别拖放,但是目标区域上的位置似乎不正确。 Demo 演示版

My guess is that its something to do with my not doing globalToLocal() correctly, but I'm not sure. 我的猜测是与我没有正确执行globalToLocal() ,但是我不确定。 So far modifying values in my w1 , w2 , h1 , h2 haven't changed a thing. 到目前为止,修改我的w1w2h1h2并没有改变任何事情。

here's the intersect testing function, it's a pretty popular one based here 这里的相交测试功能,它是一个基于一个非常流行的一种这里

function intersect(obj1, obj2){
  var objBounds1 = obj1.nominalBounds.clone(); //used nominalBounds for shape in animateCC
  var objBounds2 = obj2.nominalBounds.clone();

  var pt = obj1.globalToLocal(objBounds2.x, objBounds2.y); //is this what's wrong?

  var h1 = -(objBounds1.height / 2 + objBounds2.height);
  var h2 = objBounds2.height / 2;
  var w1 = -(objBounds1.width / 2 + objBounds2.width);
  var w2 = objBounds2.width / 2;

  if(pt.x > w2 || pt.x < w1){
    spliceThis(hitTestArray, obj2);
    return false;
  } 
  if(pt.y > h2 || pt.y < h1){
    spliceThis(hitTestArray, obj2);
    return false;
  } 
    if(hitTestArray.indexOf(obj2) < 0){ //Obj2 not found
    hitTestArray.push(obj2);
  }
  return true;
}

obj1 is the drag object, obj2 is the drop target. obj1是拖动对象, obj2是放置目标。 I know easelJS doesn't have width and height, so maybe that's part of it? 我知道easylJS没有宽度和高度,所以也许这是其中一部分? but I'm not sure how I'd refactor it to account for that. 但我不确定如何将其重构以解决这一问题。 Also, its somewhat working, so I'm not sure why that'd be the case if the height and width are just null? 而且,它有些起作用,所以我不确定为什么高度和宽度都为null时会是这种情况?

Am I just completely not understanding this, or am I close? 我只是完全不了解这一点,还是我接近?

Technically I found what's causing the issue, but I don't know why. 从技术上讲,我找到了造成此问题的原因,但我不知道为什么。

在此处输入图片说明

What's going on is the x and y for the bounds on both obj1 and obj2 are being set to negative values on the stage. 这是在舞台上将obj1obj2的边界的x和y设置为负值。 I don't know why yet, but hey, at least I know what's wrong now... maybe someone else could elaborate? 我不知道为什么,但是,嘿,至少我知道现在出了什么问题...也许其他人可以详细说明? I'm still new to JavaScript and EaselJS. 我还是JavaScript和EaselJS的新手。

UPDATE: 更新:

fixed the line var pt = obj1.globalToLocal(obj2.x, obj2.y); 修复了行var pt = obj1.globalToLocal(obj2.x, obj2.y); because it was using the negative bounds, but obj2 was fine on the x and y. 因为它使用负数边界,但是obj2在x和y上很好。 then changed the registry point to the top-left corner because it was good on that part, but still went way out into the blank area. 然后将注册表点更改为左上角,因为在该部分效果不错,但仍然超出了空白区域。

It's working as intended now, though I still don't know why it was returning negative values. 它现在按预期工作,尽管我仍然不知道为什么它返回负值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM