package src.enemies { //reflects the folder stucture this file is in //import the libaries needed for this class import flash.display.*; //create the class public class badMan extends MovieClip { /* Properties of the badMan */ public var velocity:Object; public var state:String; public var onGround:Boolean; //constructor public function badMan():void { //setup anything badMan needs to start working //velocity velocity = new Object(); velocity.x = 0; velocity.y = 0; //the 'state' badMan is in currently. state = "wait"; //same as for player onGround = false; } //runs all the code to update this enemy public function update():void { //get root var root = this.root; //calculate player distance //a better way would be to use Pythagorean theorem, but this will do var pDistance = this.x - root.player.x; //a big "if/else if" statement //do different things based on different states switch( state ) { case "wait": //'look' for the player if( pDistance < 70 ) { //if the player is close, chase him this.state = "follow"; } //break out of the switch break; case "follow": //move torwards the player if( pDistance > 0 ) { //if the distance is positive, move to the left velocity.x = -3; } else { //move to the right velocity.x = 3; } //check to see if in attack range if( Math.abs( pDistance ) < 10 ) { this.state = "attack"; } //break out of the switch break; case "attack": //attack the player //play an attack animation this.gotoAndPlay( 5 ); //then begin the process again this.state = "wait"; //break out of the switch break; default: //do nothing if the state is something we aren't expecting break; } //update velocity velocity.y += root.gravity; velocity.x *= root.friction; //test to see if the player is on the ground if( root.gameStage.hitTestPoint(this.x, this.y, true) && velocity.y > 0) { //if it has hit the ground, halt gravity velocity.y = 0; //set on ground to true onGround = true; } else { //hes not on the ground, let the game know this. onGround = false; } //update position this.x += velocity.x; this.y += velocity.y; } } }