Hello Everyone,I am trying to implement an AI for my simple platform game .Right now he can stand if i my mc is not in his distance and if he is in distance he will move to my player,and if he is in range he will attack my mc.My problem is when the AI is not on the reach or distance of my player i want the AI to move left or right back and forth.Example if the ai already move right like 400 steps he will turn around and go to left like a loop or cycle.Then while the ai is walking and my mc is in range it likes before he will chase my mc.How can i do it?This is my code right now:
Code:
//enemy
var distance:int;
var moveEnemy:Boolean=true;
var rNumber:int;
var attackEnemy:Boolean=false;
var walkEnemy:Boolean=false;
//Timer
var clock:Timer=new Timer(1000,0);
clock.addEventListener(TimerEvent.TIMER,tick);
distance=enemy.x- player.x;
	
	clock.start();
	//AI TRY
	trace(distance);
	
	if(enemy.currentLabel=="stand"){
		if(walkEnemy){
			enemy.gotoAndPlay("enRun");
			
		}
	}else if(enemy.currentLabel=="enRun"&&!walkEnemy){
				
				enemy.gotoAndPlay("stand");
			}
	if(moveEnemy){
		if(distance < 400 && distance >40){
			enemy.x -= 4;
			enemy.scaleX = 1;
			walkEnemy=true;
		}
	else{
		walkEnemy=false;
	}
	if(distance < -40 && distance > -400){
		enemy.x += 4;
		walkEnemy=true;
		enemy.scaleX  =-1;
	
		}
	}
	
	if(!attackEnemy){
		if(distance<100 && distance >-100 )
		{
			walkEnemy=false;
			rNumber=(Math.random()*100)
			if(rNumber >=0 && rNumber <60){
			enemy.gotoAndPlay("attack1");
			attackEnemy=true;
		
		} else if (rNumber>=60 && rNumber<=100){
			attackEnemy=true;
			}
		}
	}
Thats's how my ai is working right now.Then what i thought is something like this.
Code:
       if(!inDistance){//I will try to make something like this Boolean
            enemy.x -= 4;
	    enemy.scaleX = 1;
            //then maybe create a timer that will make flip after time
but i dont know how to somehow do it or is there another or much easier way to make an ai go back and forth like after moving in this value then it will flip.BTW I am using flash cs6 as3.Thank you