如何在actionscript 3中不改变hitbox的情况下调整对象的大小



当我在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!");
}

如何让点击框保持不变时,调整一个对象的大小?

这似乎是一个更好的和更干净的方法,你正在做的(假设你移动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;
}

改变按钮大小会影响其内部的mouseX/mouseY值。所以不要依赖于它,只使用基于按钮位置和大小的检查。

if(mouseX >= clickMe.x && mouseX <= clickMe.x+clickMe.width && mouseY >= clickMe.y && mouseY <= clickMe.y+clickMe.height)

相关内容

  • 没有找到相关文章

最新更新