Adolf Montaner
Adolf Montaner

Reputation: 1

Converting AS2 to AS3

I am new to Flash AS3 and I have seen this code online but its coded in AS2 can anyone help me.

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 in advance I'll be using this as a reference.

Upvotes: 0

Views: 1313

Answers (1)

Swati Singh
Swati Singh

Reputation: 1863

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

Upvotes: 1

Related Questions