[积累]2种跟踪算法(内附演示)

[ 2004-12-04 02:43:59 | 发布: N神 ]
字体大小: | |
让enemy跟踪player,speed为enemy速度。


//-----------------------第一种---------------------------------------
//利用坐标

function genzong(enemy, player, speed) {
  dx = player._x-enemy._x;
  dy = player._y-enemy._y;
  if (Math.abs(dx)>=speed) {
    if (dx>=0) {
      enemy._x += speed;
    }
    if (dx<0) {
      enemy._x -= speed;
    }
  }
  if (Math.abs(dy)>=speed) {
    if (dy>=0) {
      enemy._y += speed;
    }
    if (dy<=0) {
      enemy._y -= speed;
    }
  }
}
_root.onEnterFrame = function() {
  genzong(mc2, mc1, 5);
};
mc1.onMouseMove = function() {
  this._x = _root._xmouse;
  this._y = _root._ymouse;
  updateAfterEvent();
};


//----------------------------------------------------第2种--------------------------
//利用三角函数

function genzong2(enemy, player, speed) {
  dx = player._x-enemy._x;
  dy = player._y-enemy._y;
  distance = Math.sqrt((dx*dx)+(dy*dy));
  if (speed<=distance) {
    xspeed = speed*(dx/distance);
    yspeed = speed*(dy/distance);
    enemy._x += xspeed;
    enemy._y += yspeed;
  }
}
_root.onEnterFrame = function() {
  genzong2(mc2, mc1, 5);
};
mc1.onMouseMove = function() {
  this._x = _root._xmouse;
  this._y = _root._ymouse;
  updateAfterEvent();
};
[最后编辑于 N神, at 2005-06-07 01:33:36]



此文还没有评论.

发表评论
表情
[arrow] [biggrin] [confused] [cool]
[cry] [eek] [evil] [exclaim]
[frown] [idea] [lol] [mad]
[mrgreen] [neutral] [question] [razz]
[redface] [rolleyes] [sad] [smile]
[surprised] [twisted] [wink] [sweat]
打开 UBB 编码
自动识别链接
显示表情
隐藏的评论
用户名:   密码:   注册?
验证码 * 请输入验证码