Actionscript 3 从AS2到AS3的复杂三角转换

Actionscript 3 从AS2到AS3的复杂三角转换,actionscript-3,rotation,line,actionscript-2,trigonometry,Actionscript 3,Rotation,Line,Actionscript 2,Trigonometry,我想做一个游戏,跟在后面 问题在于我使用的是ActionScript 3.0,而本教程是使用ActionScript 2.0编写的 关于敌人的视线,我已将此代码转换为: onClipEvent (enterFrame) { dist_x = _root.hero._x-_x; dist_y = _root.hero._y-_y; dist = Math.sqrt(dist_x*dist_x+dist_y*dist_y); angle = Math.atan(dist_y/dist_x)/(Mat

我想做一个游戏,跟在后面

问题在于我使用的是ActionScript 3.0,而本教程是使用ActionScript 2.0编写的

关于敌人的视线,我已将此代码转换为:

onClipEvent (enterFrame) {
dist_x = _root.hero._x-_x;
dist_y = _root.hero._y-_y;
dist = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
angle = Math.atan(dist_y/dist_x)/(Math.PI/180);
if (dist_x<0) {
    angle += 180;
}
if (dist_x>=0 && dist_y<0) {
    angle += 360;
}
wall_collision = 0;
for (x=1; x<=dist; x++) {
    point_x = _x+x*Math.cos(angle*Math.PI/180);
    point_y = _y+x*Math.sin(angle*Math.PI/180);
    if (_root.wall.hitTest(point_x, point_y, true)) {
        wall_collision = 100;
        break;
    }
}
_root.line._x = _x;
_root.line._y = _y;
_root.line._rotation = angle;
_root.line._alpha = 100-wall_collision;
}
onClipEvent(enterFrame){
dist_x=\u root.hero.\ux-\ux;
dist_y=\u root.hero.\u y-\u y;
dist=Math.sqrt(dist_x*dist_x+dist_y*dist_y);
角度=Math.atan(dist_y/dist_x)/(Math.PI/180);

如果(dist_x=0&&dist_y你能试试下面的代码吗。 迭代器席应该在距离范围内取值,而不是一个轴DX.<
        // calculate rotation based on target
        _dx = this.x - _root.hero.x;
        _dy = this.y - _root.hero.y;

        // the iteration is by distance in original article mentioned so
        // keep dist
        //=================================
        _dist = Math.sqrt(_dx*_dx+_dy*_dy);

        // which way to rotate
        _rotateTo = getDegrees(getRadians(_dx, _dy));   

        // keep rotation positive, between 0 and 360 degrees
        if (_rotateTo > barrel.rotation + 90) _rotateTo -= 360;
        if (_rotateTo < barrel.rotation - 90) _rotateTo += 360;

        // ease rotation
        _trueRotation = (_rotateTo - barrel.rotation) / _rotateSpeedMax;

        // update rotation
        barrel.rotation += _trueRotation;   

        wall_collision = 0;

        // xi iterations are to a distance
        //==                  =======
OuterLoop:  for (var xi=1; xi<=_dist; xi++)
        {
            var point_x:Number = this.x + xi*Math.cos(_rotateTo);
            var point_y:Number = this.y + xi*Math.sin(_rotateTo);

            if(_root.wall.hitTestPoint(point_x, point_y, true))
            {
                trace("HIT");
                wall_collision = 100;
                break OuterLoop;
            }
        }

        _root.sight.x = this.x;
        _root.sight.y = this.y;
        _root.sight.rotation += _trueRotation;

        // EDITED AFTER OTHERS SOLVED
        // was
        //_root.sight.alpha = 100 - wall_collision;
        // should be:

        // Alpha changed from [0, 100] scale to [0, 1] scale.
        _root.sight.alpha = (100 - wall_collision) * 0.01;
        // END OF SOLUTION
//根据目标计算旋转
_dx=this.x-_root.hero.x;
_dy=this.y-_root.hero.y;
//在前面提到的原始文章中,迭代是按距离进行的
//保持距离
//=================================
_dist=Math.sqrt(_-dx*_-dx+_-dy*_-dy);
//旋转的方向是什么
_rotateTo=getDegrees(getRadians(_dx,_dy));
//保持正旋转,在0到360度之间
如果(_rotateTo>barrel.rotation+90)_rotateTo-=360;
如果(_-rotateTo尝试以下方法:

// calculate rotation based on target
_dx = _root.hero.x-this.x;
_dy = _root.hero.y-this.y;

// The full distance is missing from your AS3 code
_dist = Math.sqrt(_dx*_dx+_dy*_dy);

// Return the old good approach for finding angle
angle = Math.atan(_dy/_dx)/(Math.PI/180);
if (_dx<0) {
    _angle += 180;
}
if (_dx>=0 && _dy<0) {
    _angle += 360;
}

wall_collision = 0;

OuterLoop: for (var xi=1; xi<=_dist; xi++)
{
    var point_x:Number = this.x + xi*Math.cos(_angle*Math.PI/180);
    var point_y:Number = this.y + xi*Math.sin(_angle*Math.PI/180);

    if(_root.wall.hitTestPoint(point_x, point_y, true))
    {
        trace("HIT");
        wall_collision = 100;
        break OuterLoop;
    }
}

_root.sight.x = this.x;
_root.sight.y = this.y;
_root.sight.rotation = _angle;

// Alpha changed from [0, 100] scale to [0, 1] scale.
_root.sight.alpha = (100 - wall_collision) * 0.01;
//根据目标计算旋转
_dx=_root.hero.x-this.x;
_dy=_root.hero.y-this.y;
//AS3代码中缺少完整距离
_dist=Math.sqrt(_-dx*_-dx+_-dy*_-dy);
//返回旧的查找角度的好方法
角度=Math.atan(_-dy/_-dx)/(Math.PI/180);

如果(_dx=0&&&&_-dy,即使英雄在墙后,视线仍然可见,它也会闪烁。即使英雄在墙后,视线仍然可见,它也会闪烁。(与尝试叛逃者解决方案时相同)亲爱的@user3123633,我已经用一种旧方法的确切端口更新了代码。我相信这可能会对您有所帮助!请告诉我您是否更关心如何使您的方法起作用,或者此方法是否仍然存在一些问题!
// calculate rotation based on target
_dx = _root.hero.x-this.x;
_dy = _root.hero.y-this.y;

// The full distance is missing from your AS3 code
_dist = Math.sqrt(_dx*_dx+_dy*_dy);

// Return the old good approach for finding angle
angle = Math.atan(_dy/_dx)/(Math.PI/180);
if (_dx<0) {
    _angle += 180;
}
if (_dx>=0 && _dy<0) {
    _angle += 360;
}

wall_collision = 0;

OuterLoop: for (var xi=1; xi<=_dist; xi++)
{
    var point_x:Number = this.x + xi*Math.cos(_angle*Math.PI/180);
    var point_y:Number = this.y + xi*Math.sin(_angle*Math.PI/180);

    if(_root.wall.hitTestPoint(point_x, point_y, true))
    {
        trace("HIT");
        wall_collision = 100;
        break OuterLoop;
    }
}

_root.sight.x = this.x;
_root.sight.y = this.y;
_root.sight.rotation = _angle;

// Alpha changed from [0, 100] scale to [0, 1] scale.
_root.sight.alpha = (100 - wall_collision) * 0.01;