Copy link to clipboard
Copied
I wanted to built an enemy that is able to move on stage randomly in xposition and perhaps able to detect my character within the detectable zone and chase after me....
but currently I just wanted to make the enemy move randomly in x-position for starter....
It is my first time making and enemy with AI, but I'm just wasn't sure where to begin, I have used a fairy easy script to animated the movieclip in the beginning but got stucks whenever it reaches 960 or below 0 of the stage, help anyone can help me out on this...
thanks in advance.
//////////////////////////////////////////////////////////
addEventListener(Event.ENTER_FRAME,initGames);
function initGames(e:Event):void{
moveEnemy();
}
var moveRight:Number = 10;
var moveLeft:Number = -10;
var randomNumber:Number = Math.random(); //use math.random to decide which direction the enemy will move in the beginning.
function moveEnemy():void{
if(randomNumber>0.5){
enemy.x += moveRight;
}
if(randomNumber<0.5){
enemy.x += moveLeft;
}
if(enemy.x>=960){
//move in opposite direction
}
if(enemy.x<=0){
//move in opposite direction
}
}
...// max increment, the increments will be in range <0, enemyMaxSpeedIncrement> in each direction
var enemyMaxSpeedIncrement:int = 1;
// actual speed of enemy
var enemySpeed:Number = 0;
addEventListener(Event.ENTER_FRAME,initGames);
function initGames(e:Event):void{
moveEnemy();
}
function moveEnemy():void{
// Math.random() returns values from interval <0, 1>
// multiply by 2 modifies the interval to <0, 2>
// if you subtract the interval from number 1, you get interval <-1, 1>
// and
Copy link to clipboard
Copied
You want the enemy to change direction only when he reaches the edge of the screen? Because you set the randomNumber only once. If you want the enemy to change his direction randomly you should always get new value of Math.random(). Of course, then the enemy would move jittery and it's not good either. To smoothen the movement you could use speed parameter, which would be increased / decreased according to the Math.random() value. When he reaches edge of the screen, just multiply his speed by (-1) and he bounces right off the edge.
Copy link to clipboard
Copied
For starter yes, I want the enemy to be able to bounce off and move in opposite direction whenever it hits the end/beginning of the stage, then if possible I also want the enemy to be able to change its direction regardless on what position the enemy is in(e.g. 100, 600, 342... )
Copy link to clipboard
Copied
I believe this should do the trick. You can tweak enemyMaxSpeedIncrement value so it looks good.
var enemyMaxSpeedIncrement:int = 1;
var enemySpeed:Number = 0;
addEventListener(Event.ENTER_FRAME,initGames);
function initGames(e:Event):void{
moveEnemy();
}
function moveEnemy():void{
enemySpeed += enemyMaxSpeedIncrement * (1 - 2 * Math.random());
if(enemy.x > 960 || enemy.x < 0) {
enemySpeed *= -1;
}
enemy.x += enemySpeed;
}
Copy link to clipboard
Copied
thanks for the solution,
but can you explain how each line works? ![]()
Why do you use 1-2 for a random number generated?
I have also notice that the enemy move a lot faster whenever it reaches the wall, how can I slow that down?
Copy link to clipboard
Copied
// max increment, the increments will be in range <0, enemyMaxSpeedIncrement> in each direction
var enemyMaxSpeedIncrement:int = 1;
// actual speed of enemy
var enemySpeed:Number = 0;
addEventListener(Event.ENTER_FRAME,initGames);
function initGames(e:Event):void{
moveEnemy();
}
function moveEnemy():void{
// Math.random() returns values from interval <0, 1>
// multiply by 2 modifies the interval to <0, 2>
// if you subtract the interval from number 1, you get interval <-1, 1>
// and if you multiply this interval by enemyMaxSpeedIncrement, you get final interval <-enemyMaxSpeedIncrement, enemyMaxSpeedIncrement>
enemySpeed += enemyMaxSpeedIncrement * (1 - 2 * Math.random());
// enemy is at the edge of the screen
if(enemy.x > 960 || enemy.x < 0) {
// same speed, opposite direction
enemySpeed *= -1;
}
// transform speed to enemy's position
enemy.x += enemySpeed;
}
Copy link to clipboard
Copied
thanks, I got it now![]()
Copy link to clipboard
Copied
I upgraded the code a bit, take a look here: http://dev.flashlabs.eu/examples/random-movement/
You can download the source there.
Copy link to clipboard
Copied
wow thanks, didnt expect you actually spend all the trouble wrote all the code for me . Really appreciate that![]()
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more