AS2 CODE:

var dir:Number = 1

this.onEnterFrame = function () {
    this.gotoAndStop(this._currentframe + this.dir)
    if (this._currentframe == this._totalframes){
         this.dir = -1
     } else if (this._currentframe == 1){
         this.dir = 1
     }
}

AS3 CODE:

var dir:int = 1

addEventListener(Event.ENTER_FRAME, animate)

function animate (evt:Event) {
    gotoAndStop(currentFrame + dir)
    if (currentFrame == totalFrames){
         dir = -1
    } else if (currentFrame == 1){
          dir = 1
    }
}