1

当我在 actionscript 3 中调整按钮的大小时,在我的赋值示例中,hitbox 变为文本而不是正方形:

http://www.datafilehost.com/download-5ff20e2c.html

视频解释问题:http ://sdrv.ms/YcnjYV

这是代码:

import flash.events.MouseEvent;

trace("Stage(X,Y):" + stage.stageWidth + "X" + stage.stageHeight);

stage.addEventListener(MouseEvent.MOUSE_MOVE, mousePosition);
clickMe.addEventListener(MouseEvent.CLICK, handleClicks); 
function mousePosition(event:MouseEvent) {

    if(clickMe.mouseX >= 0 && clickMe.mouseX <= clickMe.width && clickMe.mouseY >= 0 && clickMe.mouseY <= clickMe.height)
    {
        do
        {
            var newX = Math.floor(Math.random()*stage.stageWidth);
            var newY = Math.floor(Math.random()*stage.stageHeight)

        }while(newX >= stage.stageWidth - clickMe.width || newY >= stage.stageHeight - clickMe.height)

        clickMe.x = newX;
        clickMe.y = newY;

        if(clickMe.width > 50)
        {
            clickMe.width=clickMe.width - 5;
            clickMe.height = clickMe.width - 5;
        }
    }
}

function handleClicks(event:MouseEvent)
{
    trace("Button Clicked!");
}

调整对象大小时,如何使 hitbox 保持不变?

4

2 回答 2

1

Changing of button size affects on mouseX/mouseY value inside it. So dont rely on it and just use checking based on button position and size.

if(mouseX >= clickMe.x && mouseX <= clickMe.x+clickMe.width && mouseY >= clickMe.y && mouseY <= clickMe.y+clickMe.height)
于 2013-01-18T13:13:34.987 回答
1

这似乎是您正在做的更好和更清洁的方法(假设您将 clickMe 剪辑移动到随机位置并将其缩小 5px)

import flash.events.MouseEvent;

clickMe.addEventListener(MouseEvent.ROLL_OVER, moveSquare);

function moveSquare(e:MouseEvent):void
{
    var newX       = Math.floor(Math.random()*stage.stageWidth);
    var newY       = Math.floor(Math.random()*stage.stageHeight);
    clickMe.x      = newX;
    clickMe.y      = newY;
    clickMe.width  = clickMe.width - 5;
    clickMe.height = clickMe.height - 5;
}
于 2013-01-18T01:21:55.090 回答