Skip navigation
theBigBoppa
Currently Being Moderated

Game Problem

Jul 26, 2012 10:07 PM

Tags: #problem #game

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

 
Replies
  • Currently Being Moderated
    Jul 27, 2012 4:31 AM   in reply to theBigBoppa

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 27, 2012 6:21 PM   in reply to theBigBoppa

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points