Flash 我想我的形状当击中舞台内部,旋转和倒数移动在真正的角度永远

Flash 我想我的形状当击中舞台内部,旋转和倒数移动在真正的角度永远,flash,actionscript-3,math,Flash,Actionscript 3,Math,!![我的地图][1] 我想在我的项目中模拟台球。 但是我的代码工作不好。 当以(例如)34度开始时,当用墙撞击时(本例中的阶段),返回真实度。 在flash中,as3 public function loop(e:Event) : void { if(luanch) { y += Math.sin(degreesToRadians(rotation)) * speed;

!![我的地图][1]

我想在我的项目中模拟台球。 但是我的代码工作不好。 当以(例如)34度开始时,当用墙撞击时(本例中的阶段),返回真实度。 在flash中,as3

public function loop(e:Event) : void
        {
            if(luanch)
            {



                y += Math.sin(degreesToRadians(rotation)) * speed;
                x += Math.cos(degreesToRadians(rotation)) * speed;






            if (x > stage.stageWidth){

                rotation -= 90;
                x = stage.stageWidth;


                trace("X :" , x , rotation);


                }
            else if (x < 0)
            {
                rotation += 90;
                x=3;

                //rotation += 90;

                trace("X :" , x , rotation);



            }

            if (y > stage.stageHeight)
            {
                y = stage.stageHeight;
                rotation -= 90;

                trace("Y : " , y , rotation);


            }

             else if (y <0)
            {
                rotation += 90;

                //rotation += 90;
                trace("Y :" , y , rotation);


            }
        }






        }

        public function degreesToRadians(degrees:Number) : Number
        {
            return degrees * Math.PI / 180;
        }

    }


}
公共函数循环(e:事件):无效
{
如果(卢安奇)
{
y+=数学sin(度弧度(旋转))*速度;
x+=数学cos(度弧度(旋转))*速度;
如果(x>阶段宽度){
旋转-=90;
x=阶段宽度;
轨迹(“X:”,X,旋转);
}
else如果(x<0)
{
旋转+=90;
x=3;
//旋转+=90;
轨迹(“X:”,X,旋转);
}
如果(y>阶段高度)
{
y=舞台高度;
旋转-=90;
轨迹(“Y:,Y,旋转);
}

否则,如果(y你的物理是错误的:当一个球从墙上反弹时,它的速度(通常)不会改变90度,它会反射。此外,你用
x
来表示x位置和x速度,但它们是不同的


你的问题是关于角度的,但在你确定位置/速度之前,你无法确定角度。

我在这个答案中的代码将向你展示移动物体的好方法。(与你现在使用的系统相同) 您可以很容易地使其适应您的系统。

现在要从墙上反射,只需反转x或y速度,这取决于它撞击的墙壁

vx *= -1; // if you hit the left or right side of the stage.
vy *= -1; // if you hit the top or bottom of the stage.
注意:像这样将物体从墙上弹起是有问题的。每隔一段时间,当你弹起物体时,物体就会振荡并被卡住,从墙上弹起

解决此问题的最简单方法(使用您使用的代码)是在检查是否与舞台边界发生碰撞时,确保考虑对象的大小

像这样的

if(x - width < 0)
{
     x = width;
     // we would now want to change the velocity.
     vx *= -1;
}

if( x + width > stage.stageWidth)
{
     x = stage.stageWidth - width;
     // we would now want to change the velocity.
     vx *= -1;
}
if(x-宽度<0)
{
x=宽度;
//我们现在想要改变速度。
vx*=-1;
}
如果(x+宽度>舞台宽度)
{
x=舞台高度。舞台宽度-宽度;
//我们现在想要改变速度。
vx*=-1;
}
y轴也是一样的。 希望这有帮助

编辑:顺便说一句,如果你想打台球,你真的应该使用向量。当你对快速移动的物体进行碰撞检测时,向量会帮你省去一些麻烦