2 Replies Latest reply on Mar 6, 2012 7:24 AM by LukeElChul

    Moving On hit Test??

    LukeElChul Level 1

      I have this script and it all works  but i want the enemy to jump back on hit test of the player.

       

      This is my enemy script:

       

      EnemysInScene = new Array();

      tag=0;

       

       

       

       

      SpawnEnemy = function ()

      {

                //INITIALISE VARS

       

       

                          attachedObj = _root.attachMovie("Enemy", "EnemyStick"+tag, tag, {_x:600, _y:287});

       

       

                          attachedObj.health = 100;

                          attachedObj.speed=5;

                          attachedObj.hit=false;

                          attachedObj.alive = true;

                          attachedObj.gotoFunction = AIZombie;

                                      

                          EnemysInScene.push(attachedObj);

       

       

                          //MonstersInScene

                          tag++;

       

      }

       

       

      function AIZombie()

      {

                if(this.alive)

                {

                          this.gotoAndStop("walk");

                          //YOUR FIRST AI

       

                          if(Player._x<=this._x-40)

                          {

                                    this._xscale = 100;

                                    this._x-=this.speed;

                          }

                          else if(Player._x>=this._x+40)

                          {

                                    this._x+=this.speed;

                                    this._xscale = -100;

                          }

       

       

                          //We have to add a small offset of + and - 20 to stop the zombie

                          //from 'flickering' when he's close.  Remove the numbers and you'll see what I mean

       

       

       

                }

                else

                {

       

                          this.gotoAndStop("dead");

                          //Activates the dying animation, which automatically removes this movie clip/sprite

                          //when it reaches the final frame

                }

       

       

      }

       

       

       

       

       

       

       

       

      function RenderEnemy()

      {

       

                for (counter=0;counter<EnemysInScene.length;counter++)

                {

                                    EnemysInScene[counter].gotoFunction();

       

                                    if(Attack == true)

                                    {

                                              if(_root.Player.hitTest(EnemysInScene[counter]))

                                                        EnemysInScene[counter].health -= 10;

      EnemysInScene = new Array();

      tag=0;

       

       

       

       

      SpawnEnemy = function ()

      {

                //INITIALISE VARS

       

       

                          attachedObj = _root.attachMovie("Enemy", "EnemyStick"+tag, tag, {_x:600, _y:287});

       

       

                          attachedObj.health = 100;

                          attachedObj.speed=5;

                          attachedObj.hit=false;

                          attachedObj.alive = true;

                          attachedObj.gotoFunction = AIZombie;

                                      

                          EnemysInScene.push(attachedObj);

       

       

                          //MonstersInScene

                          tag++;

       

      }

       

       

      function AIZombie()

      {

                if(this.alive)

                {

                          this.gotoAndStop("walk");

                          //YOUR FIRST AI

       

                          if(Player._x<=this._x-40)

                          {

                                    this._xscale = 100;

                                    this._x-=this.speed;

                          }

                          else if(Player._x>=this._x+40)

                          {

                                    this._x+=this.speed;

                                    this._xscale = -100;

                          }

       

       

                          //We have to add a small offset of + and - 20 to stop the zombie

                          //from 'flickering' when he's close.  Remove the numbers and you'll see what I mean

       

       

       

                }

                else

                {

       

                          this.gotoAndStop("dead");

                          //Activates the dying animation, which automatically removes this movie clip/sprite

                          //when it reaches the final frame

                }

       

       

      }

       

       

       

       

       

       

       

       

      function RenderEnemy()

      {

       

                for (counter=0;counter<EnemysInScene.length;counter++)

                {

                                    EnemysInScene[counter].gotoFunction();

       

                                    if(Attack == true)

                                    {

                                              if(_root.Player.hitTest(EnemysInScene[counter]))

                                                        EnemysInScene[counter].health -= 100;

       

      //I put this line in but the enemy just moves back I want it to move back like he has been hit and he jumps back then proceds foward again

      if(Player._x<=this._x-40)

                          {

      EnemysInScene[counter]._x-=2;

                          }

                          else if(Player._x>=this._x+40)

                          {

      EnemysInScene[counter]._x+=10;

       

                          }

       

                                    }

       

                                    if(EnemysInScene[counter]._x==null)

                                    {

                                              EnemysInScene.splice(counter,1);

                                              SpawnEnemy();

                                                            

                                    }

                                    if(EnemysInScene[counter].health <= 0){

                                              EnemysInScene[counter].alive = false;

                          }

                }

      };

       

       

       

       

       

       

      ////////////// MAIN PROGRAM CALLS

       

       

      SpawnEnemy();

      Initialize();

       

       

       

       

       

       

      onEnterFrame = function()

      {

                RenderEnemy();

      }

       

                                    }

       

                                    if(EnemysInScene[counter]._x==null)

                                    {

                                              EnemysInScene.splice(counter,1);

                                              SpawnEnemy();

                                                            

                                    }

                                    if(EnemysInScene[counter].health <= 0){

                                              EnemysInScene[counter].alive = false;

                          }

                }

      };

       

       

       

       

       

       

      ////////////// MAIN PROGRAM CALLS

       

       

      SpawnEnemy();

      Initialize();

       

       

       

       

       

       

      onEnterFrame = function()

      {

                RenderEnemy();

      }

       

       

       

      So Bacicly everything is working but not the way i want it and im not sure how to make it work the way I want it