I’m trying to convert this script to as3. Can you help me convert this line below so I fits into the script.
Teh one line im hung up on (so far)
//var tempEnemy = map_mc.attachMovie("enemy", "enemy"+map_mc.getNextHighestDepth(), map_mc.getNextHighestDepth());
var tempEnemy = addChild(enemy);
Full script
/**
* Random Movement & Random Direction Change
*
* Version: 1.0
* Author: Philip Radvan
* URL: http://www.freeactionscript.com
*/
//Settings
var enemyArray:Array=[];
var enemySpeed:Number=2;
var enemyTimer:Number=25;
//we will be creating new enemies inside map_mc
//so we set the walk boundry by using map_mc's dimentions
var topBorder:Number=0;
var leftBorder:Number=0;
var botBorder:Number=map_mc.height;
var rightBorder:Number=map_mc.width;
/**
* Create Enemy
*/
function createEnemy(_enemySpeed) {
//attach a new enemy movieclip from the library
//var tempEnemy = map_mc.attachMovie("enemy", "enemy"+map_mc.getNextHighestDepth(), map_mc.getNextHighestDepth());
var tempEnemy = addChild(enemy);
//set new enemy position to the mouse position
tempEnemy.X=map_mc.width/2;
tempEnemy.Y=map_mc.height/2;
//set this enemy's speed
tempEnemy.mySpeed=enemySpeed;
//sets random enemy behavior
setBehavior(tempEnemy);
//give each enemy its own onEnterFrame to make it move
addEventListener(Event.ENTER_FRAME,myFunction);
function myFunction(event:Event) {
//check if enemy is within the preset boundry on the X axis, if not, invert xSpeed to go in opposite direction
if (tempEnemy.X<=leftBorder) {
tempEnemy.xSpeed=Math.abs(tempEnemy.xSpeed);
} else if (tempEnemy.X >= rightBorder) {
tempEnemy.xSpeed=- tempEnemy.xSpeed;
}
//update enemy X position by adding the calculated distance
tempEnemy._x+=tempEnemy.xSpeed;
//check if enemy is within the preset boundry on the Y axis, if not, invert ySpeed to go in opposite direction
if (tempEnemy.Y<=topBorder) {
tempEnemy.ySpeed=Math.abs(tempEnemy.ySpeed);
} else if (tempEnemy.Y >= botBorder) {
tempEnemy.ySpeed=- tempEnemy.ySpeed;
}
//update enemy Y position by adding the calculated distance
tempEnemy.Y+=tempEnemy.ySpeed;
//update enemy timer
tempEnemy.myTimer--;
}
}
//sets random enemy behavior
function setBehavior(enemy:MovieClip) {
//get random action for the X axis
var randomAction=Math.round(Math.random() * (2 - 0)) + 0;
switch (randomAction) {
case 0 :
// move right (xSpeed is positive)
enemy.xSpeed=Math.round(Math.random() * (enemy.mySpeed - 1)) + 1;
//enemy.xSpeed=Math.random(enemy.mySpeed)*enemy.mySpeed;
break;
case 1 :
// move left (xSpeed is negative)
enemy.xSpeed= -Math.round(Math.random() * (enemy.mySpeed - 1)) + 1;
//enemy.xSpeed = -(Math.random(enemy.mySpeed))*enemy.mySpeed;
break;
default :
//
break;
}
//get random action for the Y axis
var randomAction2=Math.round(Math.random() * (2 - 0)) + 0;
switch (randomAction2) {
case 0 :
// move down (ySpeed is positive)
enemy.ySpeed==Math.round(Math.random() * (enemy.mySpeed - 1)) + 1;
break;
case 1 :
// move up (ySpeed is negative)
enemy.ySpeed = -Math.round(Math.random() * (enemy.mySpeed - 1)) + 1;
break;
default :
//
break;
}
}
create_btn.addEventListener(MouseEvent.MOUSE_UP, create);
function create(event:MouseEvent):void {
createEnemy(enemySpeed);
}