Hello,
I'm trying to make an enemy in a game stop from regularly walking back and forth, when the player object gets in a certain range. And when the player object gets back out of range, I want the enemy to continue walking back and forth, but it's not happnin'.
The enemy object stops when the player gets in range but when the player goes back out of range then he does not continue back to walking back and forth. I can't figure out what the problem is, I traced the boolean property to see if it's is false when the player gets back out of range and it confirms "false", meaning he is out of range but the enemy does not go back to walking back and forth. Here is the code.........
public function update(alienEn1:Object):void
{
trace(main.player.xPos,main.player.yPos,alienEn1. origX,alienEn1.origY,alienEn1.stopNShoot);
onPlatform(alienEn1);
if (((main.player.yPos-58) >= (alienEn1.origY -30)) && ((main.player.yPos-58) <= (alienEn1.origY +30))
&&
(main.player.xPos >= (alienEn1.origX - 120)) && (main.player.xPos <= (alienEn1.origX + 120))) {
alienEn1.stopNShoot = true;
}else{
alienEn1.stopNShoot = false;
}
if (alienEn1.stopNShoot == false){
if(alienEn1._xPos <= (alienEn1.origX - 100))
{
alienEn1.moveLeft = false;
alienEn1.moveRight = true;
}
if(alienEn1._xPos >= (alienEn1.origX + 100))
{
alienEn1.moveLeft = true;
alienEn1.moveRight = false;
}
}else if (alienEn1.stopNShoot == true){
if(main.player.xPos < alienEn1._xPos ){
alienEn1.moveLeft = false;
alienEn1.moveRight = false;
alienEn1.imageStandRt.visible = false;
alienEn1.imageWalkRt.visible = false;
alienEn1.imageStandLft.visible = true;
alienEn1.imageWalkLft.visible = false;
alienEn1.imageWalkLft.animationLoop = true;
alienEn1.imageWalkLft.updateCurrentTile ();
alienEn1.imageWalkLft.renderCurrentTile (true);
}
if(main.player.xPos > alienEn1._xPos ){
alienEn1.moveLeft = false;
alienEn1.moveRight = false;
alienEn1.imageStandRt.visible = true;
alienEn1.imageWalkRt.visible = false;
alienEn1.imageStandLft.visible = false;
alienEn1.imageWalkLft.visible = false;
alienEn1.imageWalkLft.animationLoop = true;
alienEn1.imageWalkLft.updateCurrentTile ();
alienEn1.imageWalkLft.renderCurrentTile (true);
}
}
}
public function onPlatform(alienEn1:Object):void
{
verticalChange = alienEn1.dy + main.gravity;
if (verticalChange > 25.0){ verticalChange = 25.0;}
alienEn1.dy += main.gravity;
if (alienEn1.moveLeft) {
// walk left
horizontalChange = -alienEn1.walkSpeed;
alienEn1.imageStandRt.visible = false;
alienEn1.imageWalkRt.visible = false;
alienEn1.imageStandLft.visible = false;
alienEn1.imageWalkLft.visible = true;
alienEn1.imageWalkLft.animationLoop = true;
alienEn1.imageWalkLft.updateCurrentTile();
alienEn1.imageWalkLft.renderCurrentTile(true);
} else if (alienEn1.moveRight) {
// walk right
horizontalChange = alienEn1.walkSpeed;
alienEn1.imageStandRt.visible = false;
alienEn1.imageWalkRt.visible = true;
alienEn1.imageWalkRt.animationLoop = true;
alienEn1.imageWalkRt.updateCurrentTile();
alienEn1.imageWalkRt.renderCurrentTile(true);
alienEn1.imageStandLft.visible = false;
alienEn1.imageWalkLft.visible = false;
}else{
horizontalChange = 0;
}
var newY:Number = alienEn1._yPos + verticalChange;
var newX:Number = alienEn1._xPos + horizontalChange;
// loop through all fixed objects to see if character has landed
for(var q:int=0;q< main.fixedObjects.length;q++) {
if ((alienEn1._xPos > main.fixedObjects[q].leftside-60) && (alienEn1._xPos < main.fixedObjects[q].rightside-40)) {
if ((alienEn1._yPos <= main.fixedObjects[q].topside-40) && (newY > main.fixedObjects[q].topside-40)) {
newY = main.fixedObjects[q].topside-40;
break;
}
}
}
alienEn1._xPos = newX;
alienEn1._yPos = newY;
alienEn1.imageStandRt.x = alienEn1._xPos;
alienEn1.imageStandRt.y = alienEn1._yPos;
alienEn1.imageWalkRt.x = alienEn1._xPos;
alienEn1.imageWalkRt.y = alienEn1._yPos;
alienEn1.imageStandLft.x = alienEn1._xPos;
alienEn1.imageStandLft.y = alienEn1._yPos;
alienEn1.imageWalkLft.x = alienEn1._xPos;
alienEn1.imageWalkLft.y = alienEn1._yPos;
}
I hope this isn't too hard to read, I think the problem would be in the update(); function but I can't figure out the problem. If anyone can see what the problem is please let me know.
Thanks a lot



