【问题标题】:Converting complicated trigonometry from AS2 to AS3将复杂的三角函数从 AS2 转换为 AS3
【发布时间】:2014-03-11 22:04:46
【问题描述】:

我正在尝试制作游戏,关注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;

但它不起作用。

旋转可以正常工作,但整个“如果玩家在墙后,则 alpha = 0”不起作用。

请帮我解决问题。

【问题讨论】:

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


    【解决方案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.

    【讨论】:

    • 即使英雄在墙后,视线仍然可见,它有点闪烁。 (与尝试脱北者的解决方案时相同)
    • 亲爱的@user3123633,我已经用旧方法的确切端口更新了代码。我相信它可以帮助你!如果您更关心使您的方法奏效,或者这个方法仍然存在一些问题,请告诉我!
    【解决方案2】:

    根据AS3 reference,alpha 是从 0 到 1,而不是 0 到 100。这表明 `_root.sight.alpha = (100 - wall_collision)/100.0´ 可能会起作用。

    【讨论】:

      【解决方案3】:

      你能试试下面的代码吗?我没有使用 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。

      【讨论】:

      • 即使英雄在墙后仍然可见,它有点闪烁。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 2017-07-28
      相关资源
      最近更新 更多