将AS2转换为AS3

时间:2012-01-24 17:25:22

标签: actionscript-3 actionscript-2

我是Flash AS3的新手,我在网上看过这个代码,但是在AS2编码可以帮助我。

onClipEvent (enterFrame)
{
   distance = _root.enemy._x - _root.player._x;
   if (distance < 100 && distance > -100 && _root.enemyTimer == 0)
   {
      a = int(Math.random() * 100);
      if (a >= 0 && a < 60)
      {
          _root.enemy.gotoAndStop('attack1');
          _root.healthbar.gotoAndStop(_root.healthbar._currentframe -= 5);
      }
      else if (a >= 60 && a <= 100)
      {
          _root.enemy.gotoAndStop('attack2');
          _root.healthbar.gotoAndStop(_root.healthbar._currentframe -= 8);
      }
   }
   if (distance < 160 && distance > 100)
   {
          this._xscale = _root.eScale;
          _x -= 2;
   }
   if (distance > -160 && distance < -100)
   {
         this._xscale = -_root.eScale;
         _x += 2;
   }
}
提前tnx我将把它作为参考。

1 个答案:

答案 0 :(得分:1)

this.addEventListener(Event.ENTER_FRAME, functionName);

function functionName(e:Event):void
{
    var distance:Number = this.enemy.x - this.player.x;
    if (distance < 100 && distance > -100 && this.enemyTimer == 0)
    {
        var a:int = int(Math.random() * 100);
        if (a >= 0 && a < 60)
        {
            this.enemy.gotoAndStop('attack1');
            this.healthbar.gotoAndStop(this.healthbar.currentFrame -= 5);
        }
        else if (a >= 60 && a <= 100)
        {
             this.enemy.gotoAndStop('attack2');
             this.healthbar.gotoAndStop(this.healthbar.currentFrame -= 8);
        }
    }
    if (distance < 160 && distance > 100)
    {
        this.scaleX = this.eScale;
        this.x -= 2;
    }
    if (distance > -160 && distance < -100)
    {
        this.scaleX = -this.eScale;
        this.x += 2;
    }
}