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;
}
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;
}
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);
}
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
if (enemy1Array
enemy1Array
enemy1Array.splice(j,1);
}
}
}
if(shotA.x>stage.stageWidth) {
shotA.parent.removeChild(shotA);
shotA.splice(i,1);
}
}
}
Copy link to clipboard
Copied
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
if (enemy1Array
enemy1Array
enemy1Array.splice(j,1);
}
}
}
if(shotA.x>stage.stageWidth) {
shotA.parent.removeChild(shotA);
shotA.splice(i,1);
}
}
}