将复杂的三角学从AS2转换为AS3

时间:2014-03-11 22:04:46

标签: actionscript-3 rotation line actionscript-2 trigonometry

我试图按照this tutorial制作游戏。

问题来自于我使用的是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;
}

进入那个:

// calculate rotation based on target
_dx = this.x - _root.hero.x;
_dy = this.y - _root.hero.y;
// 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;

OuterLoop: for (var xi=1; xi<=_dx; 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;
_root.sight.alpha = 100 - wall_collision;

但它不起作用。

旋转确实可以正常工作,但如果玩家在墙后面,整个&#34; alpha = 0&#34;不起作用。

请帮我解决问题。

3 个答案:

答案 0 :(得分:1)

尝试以下方法:

// 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;

Information on alpha in ActionScript 3.0

答案 1 :(得分:0)

您可以尝试以下代码吗?我没有使用flash的prev exp,但似乎你错过了一些东西。 迭代器xi应该取距离范围内的值,而不仅仅是一个轴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。

答案 2 :(得分:0)

根据AS3 reference,alpha为0到1,而不是0到100.这表明 `_root.sight.alpha =(100 - wall_collision)/100.0' 可能有用。