Hi everyone
I had created a AS 2.0 in which a movie_clip(fish) run away when a another movie_clip(Grey button) is brought near to it. the AS is working fine.but i also want to add a random movement to the movie_clip(fish). Can any one help me out as it is very urgent
the code is as follows
var enemiesArray:Array = new Array();
var radians:Number = 180/Math.PI;
createEnemies(1, "typeA", "e1");
createEnemies(1, "typeB", "e2");
createEnemies(20, "typeC", "e3");
function createEnemies(enemyAmount:Number, enemyBehavior:String, enemyLibraryClip:String):Void
{
for(var i = 0; i < enemyAmount; i++)
{
var tempEnemy:MovieClip = _root.attachMovie(enemyLibraryClip, "enemy"+_root.getNextHighestDepth(),_root.getNextHighestDepth())
tempEnemy._x = random(Stage.width);
tempEnemy._y = random(Stage.height);
tempEnemy._rotation = random(180);
if(enemyBehavior == "typeA")
{
tempEnemy.speed = 1
tempEnemy.turnRate = .05
tempEnemy.agroRange = 200;
tempEnemy.mode = "follow"
}
else if(enemyBehavior == "typeB")
{
tempEnemy.speed = 4
tempEnemy.turnRate = .5
tempEnemy.agroRange = 200;
tempEnemy.mode = "follow"
}
else if(enemyBehavior == "typeC")
{
tempEnemy.speed = 10
tempEnemy.turnRate = 5
tempEnemy.agroRange = 200;
tempEnemy.mode = "run"
}
tempEnemy.distanceX = 0;
tempEnemy.distanceY = 0;
tempEnemy.distanceTotal = 0;
tempEnemy.moveDistanceX = 0;
tempEnemy.moveDistanceY = 0;
tempEnemy.moveX = 0;
tempEnemy.moveY = 0;
tempEnemy.totalmove = 0;
enemiesArray.push(tempEnemy)
}
}
function updateEnemies():Void {
for(var i = 0; i < enemiesArray.length; i++)
{
var tempEnemy:MovieClip = enemiesArray[i];
updatePosition(tempEnemy, player_mc);
}
}
function updatePosition(follower:MovieClip, target:MovieClip) {
follower.distanceX = target._x-follower._x;
follower.distanceY = target._y-follower._y;
follower.distanceTotal = Math.sqrt(follower.distanceX * follower.distanceX + follower.distanceY * follower.distanceY);
if(follower.distanceTotal <= follower.agroRange){
follower.moveDistanceX = follower.turnRate * follower.distanceX / follower.distanceTotal;
follower.moveDistanceY = follower.turnRate * follower.distanceY / follower.distanceTotal;
follower.moveX += follower.moveDistanceX;
follower.moveY += follower.moveDistanceY;
follower.totalmove = Math.sqrt(follower.moveX * follower.moveX + follower.moveY * follower.moveY);
follower.moveX = follower.speed * follower.moveX / follower.totalmove;
follower.moveY = follower.speed * follower.moveY / follower.totalmove;
if(follower.mode == "follow")
{
follower._x += follower.moveX;
follower._y += follower.moveY;
follower._rotation = Math.atan2(follower.moveY, follower.moveX) * radians;
}
else if(follower.mode == "run")
{
if((follower._x >= 50)&&(follower._x <= 1900)&&(follower._y >= 70)&&(follower._y <= 1000))
{
follower._x -= follower.moveX;
follower._y -= follower.moveY;
follower._rotation = (Math.atan2(follower.moveY, follower.moveX) * radians)+180;
}
else
{
follower._rotation = (Math.atan2(follower.moveY, follower.moveX) * radians)+180;
follower._x += follower.moveX;
follower._y += follower.moveY;
}
}
}
}
_root.onEnterFrame = function(){
updateEnemies();
}
player_mc.onPress = function(){
startDrag(this);
}
player_mc.onRelease = function(){
stopDrag();
}