4 Replies Latest reply on Jul 27, 2012 9:08 PM by theBigBoppa

# Game Problem

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

• ###### 1. Re: Game Problem

Yes, it is pretty hard to read, especially without knowing what different elements of the code represent.  If you can highlight the code that is relevant and explain which parts of it are used for determining whether something should move and which parts are supposed to result in movement vs stopping it could help.

• ###### 2. Re: Game Problem

public function update(alienEn1:Object):void

{

trace(main.player.xPos,main.player.yPos,alienEn1. origX,alienEn1.origY,alienEn1.stopNShoot);

//THE FOLLOWING CALLS THE FUNCTION BELOW THIS CURRENT UPDATE FUNCTION.....

onPlatform(alienEn1);

//THIS CONDITIONAL CHECKS IF THE PLAYER IS IN RANGE OF THE ENEMY, IF THE PLAYER IS IN RANGE THE aliienEn1.stopNShoot IS SET TO TRUE....IF NOT IN RANGE, ITS SET TO FALSE...

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;

}

// THE FOLLOWING CHECKS IF alienEn1.stopNShoot IS FALSE, IF IT IS THEN IT SETS THE BOOLEAN PROPERTIES AS TO WHETHER THE ALIEN SHOULD MARCH LEFT OR RIGHT....

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;

}

//THE FOLLOWING CHECKS IF THE alienEn1.stopNShoot BOOLEAN IS TRUE, IF SO THEN IT MAKES THE ALIEN STOP AND FACE THE PLAYER

}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;

//THE FOLLOWING CHECKS IF THE BOOLEAN alienEn1.moveLeft IS TRUE, IF SO IT MAKES THE ANIMATION RUN AND MOVES THE ENEMY TO THE LEFT

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);

//THE FOLLOWING DOES THE SAME THING AS PREVIOUS BUT MOVES ENEMY TO THE LEFT

} 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;

//THE FOLLOWING JUST CHECKS TO SEE IF ENEMY HAS HIT THE PLATFORM

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;

//SETS THE X AND Y PROPERTIES OF THE ALIEN IMAGE TO THE CALCULATED X AND Y POSITIONS CALCULATED ABOVE...

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;

}

THANK YOU

• ###### 3. Re: Game Problem

This will be a great opportunity for you to learn how to use the trace function to see what the status of different things and see where they disagree with what you believe they should be.

If the problem lies in the object not moving when it should be, then trace the values that would make that true and see if they agree.  If they agree, then check the details of the values that dictate motion changes and see if they are changing.

If traces are too difficult to follow, use textfields that update with the values you are interested in tracking.

• ###### 4. Re: Game Problem

ok thanks