ActionScript - 鼠标坐标不准确?

发布于 2024-11-27 02:17:40 字数 1106 浏览 1 评论 0原文

跟踪显示对象的 mouseX / mouseYlocalX / localY 坐标时,为什么 x 从 1 开始,而 y 从 1 开始代码>从0开始?

例如,我在舞台上绘制了一个简单的 400 x 400 像素精灵,并使用 MouseEvent.MOUSE_MOVE 事件侦听器调用处理函数来跟踪鼠标的本地坐标。

第一个左上角像素返回 {x:1, y:0},最后一个右下角像素返回 {x:400, y:399}xy 不应该以相同的值开始和结束吗?我不确定对于第一个鼠标坐标(0 或 1)哪个更有意义,但它们不同肯定没有意义?

[SWF(width = "1000", height = "600", backgroundColor = "0xCCCCCC")]

import flash.display.Sprite;
import flash.events.MouseEvent;

var darkBlueRect:Sprite = createSprite();
darkBlueRect.x = 23;
darkBlueRect.y = 42;
addChild(darkBlueRect);

darkBlueRect.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler);

function mouseMoveEventHandler(evt:MouseEvent):void
{
    trace(darkBlueRect.mouseX, evt.localX, darkBlueRect.mouseY, evt.localY);
}

function createSprite():Sprite
{
    var result:Sprite = new Sprite();
    result.graphics.beginFill(0x0000FF, 0.5);
    result.graphics.drawRect(0, 0, 400, 400);
    result.graphics.endFill();

    return result;
}

when tracing the mouseX / mouseY or localX / localY coordinates of a display object, why does x start at 1 while y starts at 0?

for example, i've drawn a simple 400 x 400 pixel sprite onto the stage with a MouseEvent.MOUSE_MOVE event listener that calls a handler function to trace the local coordinates of the mouse.

the first, top-left pixel returns {x:1, y:0} and the last, bottom-right pixel returns {x:400, y:399}. shouldn't both the x and y start and end with the same value? i'm not sure which makes more sense for a the first mouse coordinate (either 0 or 1) but it certainly doesn't make sense that they are different?

[SWF(width = "1000", height = "600", backgroundColor = "0xCCCCCC")]

import flash.display.Sprite;
import flash.events.MouseEvent;

var darkBlueRect:Sprite = createSprite();
darkBlueRect.x = 23;
darkBlueRect.y = 42;
addChild(darkBlueRect);

darkBlueRect.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveEventHandler);

function mouseMoveEventHandler(evt:MouseEvent):void
{
    trace(darkBlueRect.mouseX, evt.localX, darkBlueRect.mouseY, evt.localY);
}

function createSprite():Sprite
{
    var result:Sprite = new Sprite();
    result.graphics.beginFill(0x0000FF, 0.5);
    result.graphics.drawRect(0, 0, 400, 400);
    result.graphics.endFill();

    return result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2024-12-04 02:17:40

报告错误,您发现了一个错误:

http://bugs.adobe.com/flashplayer/

我想也许这是为了给精灵上的一条线腾出空间,但所有这些测试都证明情况并非如此。在我看来,这是一个错误。将其归档,我会附议,或者如果您不介意,请告诉我,我将归档它,因为它应该得到修复。

更新

我刚刚尝试了另一个测试,将 stageScaleMode 设置为允许缩放,然后将对象放大 2-3 倍,并尝试在 MOUSE_DOWN 上获取 0/0 读数。只是显然无法完成。我最终确实在 X 上得到了 0 读数,但随后 Y 就出来了。我认为这个问题可能归结为以下事实:flash 将 X/Y 指针位置返回为数字而不是整数,并且您可以在指针位置上获得小数值,正如您放大时会注意到的那样。也许是只是有错误的代码,或者归结为基于小数点计算方式的浮点精度问题,或者如果您没有放大并且对象没有缩放,则闪存会将该小数值舍入为 int,这也可以解释奇怪的行为。不知道只是猜测,我想我会添加这个额外测试的结果。

Report a bug, you've found one:

http://bugs.adobe.com/flashplayer/

I thought perhaps it was to make room for a line applied to the sprite among other things but all those tests proved not to be the case. This is, in my opinion, a bug. File it and I'll second it, or if you can't be bothered let me know I'll file it cause it should be fixed.

Update

I've just tried another test where I set the stageScaleMode to allow for scaling, then zoomed in 2-3 times to the object and tried to get a 0/0 reading on MOUSE_DOWN. Just can't be done apparently. I did get a 0 reading on the X finally but then the Y is out. I think this issue may come down to the fact that flash returns X/Y pointer position as a Number and not an int, and you can get decimal values on the pointer position as you'll notice if you're zoomed in. Perhaps it's just buggy code or it comes down to floating point precision issues based on how the decimal points are calculated, or if you're not zoomed in and the object isn't scaled, flash rounds that decimal value off an int and this may also explain the weird behavior. Dunno just guessing, thought I'd add the results of this extra test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文