• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Hello! Im trying to make my enemies to have hitpoints.All of my enemies have the same instance name "enemy".So, when i fire and hit an enemy hitpoints from 4 -- to 3,and here is the problem,when i fire a new enemy the hitpoints it continue from 3.

Community Beginner ,
Feb 06, 2016 Feb 06, 2016

Copy link to clipboard

Copied

var shotA:Array=[];

var enemy1Array:Array = new Array();

for (var e1:int = numChildren - 1; e1 >= 0; e1--)

  {

         var child:DisplayObject = getChildAt(e1);

         if (child.name == "enemy")

     {

       enemy1Array.push(child);

         }

        }

  for each(var enemy:MovieClip in enemy1Array)

  {

    var hitpoints:int = 4;

       trace (hitpoints);

  }

stage.addEventListener(Event.ENTER_FRAME,looping);

function looping (event:Event):void

{

    for(var i:int=shotA.length-1;i>=0;i--)

    {

      shotA.x +=10;

   for each(enemy in enemy1Array)

   {

      if (shotA.hitTestObject(enemy))

     {

  hitpoints --;

     trace ("ok");

  trace (hitpoints);

     shotA.y = 1000;

     shotA.visible = false;

  if (hitpoints == 0)

  {

  enemy.y = -1000;

  }

     }

   }

      if(shotA.x>stage.stageWidth)

        {

            shotA.splice(i,1);

        }

    }

}

fire_btn.addEventListener(MouseEvent.CLICK,fire);

function fire(MouseEvent:Event):void

{

  for each(enemy in enemy1Array)

  {

  trace (hitpoints);

  }

  var shot:bullet = new bullet();

  shotA.push(shot);

  this.addChild(shot);

  shot.x = player.x;

  shot.y = player.y;

}

TOPICS
ActionScript

Views

341

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 06, 2016 Feb 06, 2016

Copy link to clipboard

Copied

that's problematic coding.

to start, each object should have a unique instance name.  you could use enemy1, enemy2 etc

var shotA:Array=[];

var enemy1Array:Array = new Array();

// if your enemies are all on stage and not created by code this for-loop is not good but it's ok.  otherwise, it's not and enemies should be added to the array when they're created.

 

for (var e1:int = numChildren - 1; e1 >= 0; e1--)

  {

         var child:DisplayObject = getChildAt(e1);

         if (child.name.indexOf("enemy")>-1)

     {

       enemy1Array.push(MovieClip(child));

MovieClip(child).hitPoints=4;

         }

        }

stage.addEventListener(Event.ENTER_FRAME,looping);

function looping (event:Event):void{

    for(var i:int=shotA.length-1;i>=0;i--)  {

      shotA.x +=10;

   for(var j:int=enemy1Array.length-1;j>=0;j--) {

      if (shotA.hitTestObject(enemy1Array)) {

shotA.splice(i,1);

enemy1Array.hitPoints--;

  if (enemy1Array.hitPoints == 0) {

enemy1Array.splice(j,1);

  }

     }

   }

      if(shotA.x>stage.stageWidth)  {

            shotA.splice(i,1);

        }

}

}

fire_btn.addEventListener(MouseEvent.CLICK,fire);

function fire(MouseEvent:Event):void

{

  var shot:bullet = new bullet();

  shotA.push(shot);

  this.addChild(shot);

  shot.x = player.x;

  shot.y = player.y;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 07, 2016 Feb 07, 2016

Copy link to clipboard

Copied

TypeError: Error #1010: A term is undefined and has no properties.

  at _2016withhelp_fla::MainTimeline/looping()

I try something else to put hitpoints in my enemies.

I put movie clips inside the enemy movieclip .

it works but i thing its better to set hitpoints with your way!

for each(var enemy1:MovieClip in enemy1Array)

  {

  if(enemy1.enemy1a.hitTestPoint(bombA.x,bombA.y))

  {

  bombA.y =-1000;

  enemy1.enemy1a.y = 1000;

  }

  if(enemy1.enemy1b.hitTestPoint(bombA.x,bombA.y))

  {

  bombA.y =-1000;

  enemy1.enemy1b.y = 1000;

  }

  if(enemy1.enemy1c.hitTestPoint(bombA.x,bombA.y))

  {

  bombA.y =-1000;

  enemy1.enemy1c.y = 1000;

  }

  if(enemy1.enemy1d.hitTestPoint(bombA.x,bombA.y))

  {

  bombA.y =-1000;

  enemy1.enemy1d.y = 1000;

  score += 10;

  score_txt.text = String (score);

  }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 07, 2016 Feb 07, 2016

Copy link to clipboard

Copied

tick 'permit debugging' and use the trace function to debug your code.

and use:

stage.addEventListener(Event.ENTER_FRAME,looping);

function looping (event:Event):void{

    for(var i:int=shotA.length-1;i>=0;i--)  {

      shotA.x +=10;

   for(var j:int=enemy1Array.length-1;j>=0;j--) {

      if (shotA.hitTestObject(enemy1Array)) {

shotA.parent.removeChild(shotA);

shotA.splice(i,1);

enemy1Array.hitPoints--;

  if (enemy1Array.hitPoints == 0) {

enemy1Array.parent.removeChild(enemy1Array);

enemy1Array.splice(j,1);

  }

     }

   }

      if(shotA.x>stage.stageWidth)  {

shotA.parent.removeChild(shotA);

            shotA.splice(i,1);

        }

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 08, 2016 Feb 08, 2016

Copy link to clipboard

Copied

LATEST

TypeError: Error #1010: A term is undefined and has no properties.

  at _2016withhelp_fla::MainTimeline/looping()[_2016withhelp_fla.MainTimeline::frame5:80]

I remove the

shotA.parent.removeChild(shotA); shotA.splice(i,1); when the shot hit the enemy and i replace with shotA . y = 1000 and it works.

Hitpoints its ok now.

You are a genius!!

stage.addEventListener(Event.ENTER_FRAME,looping);

function looping (event:Event):void{

    for(var i:int=shotA.length-1;i>=0;i--)  {

      shotA.x +=10;

   for(var j:int=enemy1Array.length-1;j>=0;j--) {

      if (shotA.hitTestObject(enemy1Array)) {

//shotA.parent.removeChild(shotA);

//shotA.splice(i,1);

shotA.y = 1000;

enemy1Array.hitPoints--;

  if (enemy1Array.hitPoints == 0) {

enemy1Array.parent.removeChild(enemy1Array);

enemy1Array.splice(j,1);

  }

     }

   }

      if(shotA.x>stage.stageWidth)  {

shotA.parent.removeChild(shotA);

            shotA.splice(i,1);

        }

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines