0

我正在开发一个基本的 as3 弹弓游戏,它使用 startDrag() 和 stopDrag() 让用户拉一个物体并开火。当对象没有拉伸“弹性”时,MOUSE_UP 函数按应有的方式工作,但是当它低于设定点并且正在拉伸字符串时,不会调用 MOUSE_UP 函数。

  • 变量

    var gravity = 0.1;
    var angle1:Number = 0;
    var angle2:Number = 0;
    var radius:Number = 1;
    var elasticCoefficient:Number = 0.002;
    var released:Boolean = true;
    var forced:Boolean = false;
    var acc:Object = {x:0 , y:0}; 
    var vel:Object = {x:0 , y:0};
    var elastic:MovieClip = new MovieClip();
    
  • ENTER_FRAME 代码是

    function doConstantly(e:Event):void
    {
        acc.x = 0;
        acc.y = gravity;
    
        if(released == true) 
        {
            vel.x += acc.x; 
            vel.y += acc.y; 
            ball.x += vel.x;
            ball.y += vel.y
        }
    
    
        if(ball.y > stage.stageHeight + 500 || ball.y < -50)
        {
            resetLevel();
        }
    
    
        elastic.graphics.clear();
        elastic.graphics.lineStyle(2, 0xFFF2BD);
    
    
        if(ball.y > point1.y && ball.x < point2.x)
        {
                forced = true;
    
            var x1:Number = ball.x - point1.x;
            var y1:Number = ball.y - point1.y;
            var x2:Number = point2.x - ball.x;
            var y2:Number = point2.y - ball.y;
            var distance1:Number = Math.sqrt(x1 * x1 + y1 * y1);
            var distance2:Number = Math.sqrt(x2 * x2 + y2 * y2);
            angle1 = Math.atan2(y1,x1);
            angle2 = Math.atan2(y2,x2);
            var xOffset:Number = Math.cos(angle1 + Math.PI / 2) * radius;
            var yOffset:Number = Math.sin(angle1 + Math.PI / 2) * radius;
            var xOffset2:Number = Math.cos(angle2 + Math.PI / 2) * radius;
            var yOffset2:Number = Math.sin(angle2 + Math.PI / 2) * radius;
            angle1 +=  Math.sin(radius / distance1);
            angle2 +=  Math.sin(radius / distance2) * -1;
            elastic.graphics.moveTo(point1.x, point1.y);
            elastic.graphics.lineTo(ball.x+xOffset, ball.y+yOffset);
            elastic.graphics.moveTo(point2.x, point2.y);
            elastic.graphics.lineTo(ball.x+xOffset2, ball.y+yOffset2);
        }
        else
        {
            forced = false;
    
            if(forced == true){trace("forced is true")}
            if(forced == false){trace("forced is false")}
            elastic.graphics.moveTo(point1.x, point1.y);
            elastic.graphics.lineTo(point2.x, point2.y);
        }
    
        if (released == true && forced == true)
        {
            acc.x +=  distance1 * Math.sin(angle2) * elasticCoefficient;
            acc.y +=   -  distance1 * Math.cos(angle1) * elasticCoefficient;
            acc.x +=  distance2 * Math.sin(angle1) * elasticCoefficient;
            acc.y +=   -  distance2 * Math.cos(angle2) * elasticCoefficient;
    
            vel.x +=  acc.x;
            vel.y +=  acc.y;
        }
    }
    
  • 和鼠标事件

    function ballMouseDown(event:MouseEvent)
    {
    //call function to reset level
    resetLevel();
    //follow mouse
    ball.x = mouseX;
    ball.y = mouseY;
    ball.startDrag();
    //set released to false so that gravity wont affect the ball when clicked
    released = false;
    }
    function ballMouseUp(event:MouseEvent)
    {
    trace("mouse up function called")
    released = true; //gravity will affect the ball when released
    ball.stopDrag();
    }
    

谢谢。

4

1 回答 1

3

尝试将MOUSE_UP处理程序添加到stage而不是 - 目前,您需要在鼠标悬停时释放鼠标,但ball情况可能并非如此。

更新您的MOUSE_DOWN处理程序以将侦听器附加到舞台:

function ballMouseDown(e:MouseEvent):void
{
    // ...your current code.

    stage.addEventListener(MouseEvent.MOUSE_UP, ballMouseUp);
}

并在触发处理程序时删除侦听器:

function ballMouseUp(e:MouseEvent):void
{
    // ...your current code.

    stage.removeEventListener(MouseEvent.MOUSE_UP, ballMouseUp);
}
于 2013-08-13T07:33:37.920 回答