敌人的旋转非常奇怪

发布于 2024-12-13 04:52:43 字数 1361 浏览 0 评论 0原文

我有这个问题:有一个敌人轮流到我的玩家。当与我的玩家一起“绕行”敌人时,我可以看到敌人正在向我的玩家旋转。

然后敌人突然转了个360度,再次面向我的玩家。我不知道为什么它会进行这种奇怪的 360 度转弯,但每次我绕敌人几秒钟时都会发生这种情况。我不知道问题可能出在哪里。

tempEnemy.dX = tempEnemy.x - player.x;
tempEnemy.dY = tempEnemy.y - player.y;
                        
tempEnemy.rotateTo = toDegrees(getRadians(tempEnemy.dX, tempEnemy.dY));
                        
                        
                        
if(tempEnemy.frame < 0) tempEnemy.frame += 360;
if(tempEnemy.frame > 359) tempEnemy.frame -= 360;
                        
                        
tempEnemy.trueRotation = int((tempEnemy.rotateTo - tempEnemy.frame) / tempEnemy.rotateSpeed);
                        
tempEnemy.vX += (player.x - tempEnemy.x) / tempEnemy._speed;
tempEnemy.vY += (player.y - tempEnemy.y) / tempEnemy._speed;
         
tempEnemy.vX *= tempEnemy.decay;
tempEnemy.vY *= tempEnemy.decay;

更新:

private function toDegrees(radians:Number):Number
    {
        var degrees:Number = Math.floor(radians * 180 / Math.PI);
        //trace (degrees);
        return degrees;
    }

private function getRadians(deltaX:Number, deltaY:Number):Number
    {
        var radian:Number = Math.atan2(deltaY, deltaX);
        
        if (deltaY < 0)
        {
            radian += (2 * Math.PI);
        }
        return(radian);
    }

I have this problem: there is this enemy which rotates to my player. While "orbiting" the enemy with my player I can see that the enemy is rotating towards my player.

And then the enemy suddenly turns around 360 degrees and facing to my player again. I don't know why it does this strange 360 degree turn but it happens every time when I orbit the enemy for a few seconds. I don't know where the problem might be.

tempEnemy.dX = tempEnemy.x - player.x;
tempEnemy.dY = tempEnemy.y - player.y;
                        
tempEnemy.rotateTo = toDegrees(getRadians(tempEnemy.dX, tempEnemy.dY));
                        
                        
                        
if(tempEnemy.frame < 0) tempEnemy.frame += 360;
if(tempEnemy.frame > 359) tempEnemy.frame -= 360;
                        
                        
tempEnemy.trueRotation = int((tempEnemy.rotateTo - tempEnemy.frame) / tempEnemy.rotateSpeed);
                        
tempEnemy.vX += (player.x - tempEnemy.x) / tempEnemy._speed;
tempEnemy.vY += (player.y - tempEnemy.y) / tempEnemy._speed;
         
tempEnemy.vX *= tempEnemy.decay;
tempEnemy.vY *= tempEnemy.decay;

Update:

private function toDegrees(radians:Number):Number
    {
        var degrees:Number = Math.floor(radians * 180 / Math.PI);
        //trace (degrees);
        return degrees;
    }

private function getRadians(deltaX:Number, deltaY:Number):Number
    {
        var radian:Number = Math.atan2(deltaY, deltaX);
        
        if (deltaY < 0)
        {
            radian += (2 * Math.PI);
        }
        return(radian);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想挽留 2024-12-20 04:52:43

如果没有看到 getRadians 函数,也许您正在通过 360° 计算反射角?

也许是这样的:

var degrees:Number = Math.atan2(tempEnemy.y - player.y, tempEnemy.x - player.x) * 180 / Math.PI;
var delta:Number = degrees - tempEnemy.rotation;

while (delta <= -180)
    delta += 360;
while (delta > 180)
    delta -= 360;

用敌人显示对象设计的静止角度补偿

Without seeing your getRadians function, perhaps you're passing 360° calculating a reflex angle?

Maybe something like:

var degrees:Number = Math.atan2(tempEnemy.y - player.y, tempEnemy.x - player.x) * 180 / Math.PI;
var delta:Number = degrees - tempEnemy.rotation;

while (delta <= -180)
    delta += 360;
while (delta > 180)
    delta -= 360;

Compensate degrees with the resting angle of your enemy display object design.

岁月如刀 2024-12-20 04:52:43

听起来您已经使用了时间线补间。使用补间代码,或者获取我在此处使用的补间 http://flexdiary.blogspot.com/2010/04/sample-code-for-oop-timeline-insideria.html。我不记得我到底做了什么来使时间线补间完整循环,而不是在重新启动之前扭转回来,但您应该能够导出并重用补间。

如果您想使用补间代码,可以使用 Flash 内置的补间类或使用 Tweensy 等库。

It sounds like you've used a timeline tween. Either use a code tween, or grab the tween I used here http://flexdiary.blogspot.com/2010/04/sample-code-for-oop-timeline-insideria.html. I don't remember what exactly I did to make the timeline tween go full circle, rather than twisting back before restarting, but you should be able to export and reuse the tween.

If you want to use a code tween, you can either use Flash's built in tween classes or use a library like Tweensy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文