仅在给定的周长内移动物体

时间:2017-12-26 11:31:47

标签: actionscript-3 flash

我正在为孩子们创造一个游戏(不完全是)。这是为了说明我们可以制作具有固定长度的螺纹,针板和三个销钉的无限三角形。如下所示:enter image description here

学生可以移动任何针脚。这样做会创建新的三角形,但三角形的周长应该是给定的长度。我使用了以下代码:

var xMax:Number = drawBoard.x + drawBoard.width;
var xMin:Number = drawBoard.x;
var yMax:Number = drawBoard.y + drawBoard.height;
var yMin:Number = drawBoard.y;

var isDragging1:Boolean = false;
var isDragging2:Boolean = false;
var isDragging3:Boolean = false;

drawBoard.x = xMin;
drawBoard.y = yMin;
var lines:Shape = new Shape();
addChild(lines);

stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseUpdate);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
pin1.addEventListener(MouseEvent.MOUSE_DOWN, startDragging1);
pin2.addEventListener(MouseEvent.MOUSE_DOWN, startDragging2);
pin3.addEventListener(MouseEvent.MOUSE_DOWN, startDragging3);
function mouseUpdate(evt:MouseEvent):void
{
    if (isDragging1)
    {
        pin1.x = validX(evt.stageX);
        pin1.y = validY(evt.stageY);
        lines.graphics.clear();
        drawLines();
    }
    if (isDragging2)
    {
        pin2.x = validX(evt.stageX);
        pin2.y = validY(evt.stageY);
        lines.graphics.clear();
        drawLines();
    }
    if (isDragging3)
    {
        pin3.x = validX(evt.stageX);
        pin3.y = validY(evt.stageY);
        lines.graphics.clear();
        drawLines();
    }
    evt.updateAfterEvent();
}
function validX(thisX:Number):Number
{
    if (thisX < xMin)
    {
        return (xMin);
    }
    if (thisX > (xMax))
    {
        return (xMax);
    }
    return (thisX);
}
function validY(thisY:Number):Number
{
    if (thisY < yMin)
    {
         return (yMin);
    }
    if (thisY > (yMax))
    {
        return (yMax);
    }
    return thisY;
}
function drawLines():void
{
    lines.graphics.lineStyle(1, 0xFF0000);
    lines.graphics.moveTo(pin1.x, pin1.y);
    lines.graphics.lineTo(pin2.x, pin2.y);
    lines.graphics.lineTo(pin3.x, pin3.y);
    lines.graphics.lineTo(pin1.x, pin1.y);
    lines.filters = [new DropShadowFilter()];
}
function startDragging1(evt:MouseEvent):void
{
    isDragging1 = true;
}
function startDragging2(evt:MouseEvent):void
{
    isDragging2 = true;
}

function startDragging3(evt:MouseEvent):void
{
    isDragging3 = true;
}
function stopDragging(evt:MouseEvent):void
{
    isDragging1 = false;
    isDragging2 = false;
    isDragging3 = false;
}
function DistanceTwoPoints(x1:Number, x2:Number,  y1:Number, y2:Number):Number
{
    var dx:Number = x1 - x2;
    var dy:Number = y1 - y2;
    return Math.sqrt(dx * dx + dy * dy);
}

它完成了一半。我可以移动任何针脚,它会创建三角形。但每次外围都不同。我试图通过计算三边之间的距离来限制它。问题是,我可以将针移动到任何地方,这使得难以获得精确的周长。结果,难以平稳地移动销。所以,我的问题是,是否可以将引脚仅移动到有效位置而不是沿着鼠标移动?任何帮助将不胜感激。

0 个答案:

没有答案