Copy link to clipboard
Copied
I tried some code for my enemyShooter's hitTest and it worked.
if (_root.enemyShooter.body.hitTest(_root.ship.body)) {...}
(enemyShooter is the instance name of movieclip)
However, this doesn't work.
var name = _name
if (_root.name.body.hitTest(_root.ship.body)) {...}
The reason I am doing this is because there are many enemies like enemyShip1, enemyShip2, enemyShip3.
The name if it is set by
"EnemyShip" + _root.getNextHighestDepth()
because I use
_root.attachMovie("EnemyShip", "EnemyShip" + _root.getNextHighestDepth(), _root.getNextHighestDepth());
I cannot find the instance name unless using _name
How can I make it work?
Copy link to clipboard
Copied
in most situations it's best to store those enemy references in an array and loop through the array especially if you're sometimes removing enemies and they no longer need to be checked for a hittest (or anything else).
Copy link to clipboard
Copied
Yes, I already used the Array feature.
My whole code:
class EnemyBasic extends MovieClip
{
var _x, _y, speed, removeMovieClip, hitTest, health, shaking, shakeDuration, _rotation, name, _name;
function EnemyBasic()
{
super();
} // End of the function
function onLoad()
{
_x = 700;
_y = Math.random() * 200 + 50;
speed = Math.random() * 3 + 3;
_root.ship.enemies.push(this);
health = 2;
shaking = false;
shakeDuration = 10;
_rotation = 0;
name = _name;
} // End of the function
function onEnterFrame()
{
_x = _x - speed;
if (_x < -100)
{
this.removeMovieClip();
} // end if
trace(name)
if (_root.ship.enemies.hitTest(_root.ship.body))
{
this.explode();
}
}
} // End of Class
Some of the code are removed to make this simpler.
How do I change the HitTest part? _root.ship.enemies doesn't work
Copy link to clipboard
Copied
why doesn't _root.ship.enemies work?
is it populated when you start your hittest? if yes, how are you trying to use it?
Copy link to clipboard
Copied
What means populated when starting hittest?
If I use if (this.hitTest(_root.ship.body))
then no problem
but I want _root.ship.body to have hit Test with the Enemy movieclip's body.
so I try to use:
if (_root.ship.enemies.body.hitTest(_root.ship.body)) or
if (_root.ship.name.body.hitTest(_root.ship.body)) or
if (_root.ship._name.body.hitTest(_root.ship.body))
I tried all of them and doesn't work.
(I am very new to flash, so I just put anything I can think of into it)
Copy link to clipboard
Copied
use:
for(var i:Number=_root.ship.enemies.length-1;i>=0;i--){
_root.ship.enemies.hitTest(_root.ship.body){
// do whatever
// if _root.ship.enemies is destroyed and no longer needed use the two lines below:
_root.ship.enemies.removeMovieClip();
_root.ship.enemies.splice(i,1);
}
}
Copy link to clipboard
Copied
I copied and paste this code but it doesn't work.
The enemy exploded before it enter the screen(before it collide with ship)
I am using ActionScript 2, also can you explain this piece of code?
Copy link to clipboard
Copied
show the code you used so i can see where you placed this code.
Copy link to clipboard
Copied
whole piece of code:
class EnemyBasic extends MovieClip
{
var _x, _y, speed, removeMovieClip, hitTest, health, shaking, shakeDuration, _rotation, name, _name;
function EnemyBasic()
{
super();
} // End of the function
function onLoad()
{
_x = 700;
_y = Math.random() * 200 + 50;
speed = Math.random() * 3 + 3;
_root.ship.enemies.push(this);
health = 2;
shaking = false;
shakeDuration = 10;
_rotation = 0;
name = _name;
} // End of the function
function onEnterFrame()
{
_x = _x - speed;
if (_x < -100)
{
this.removeMovieClip();
} // end if
for(var i:Number=_root.ship.enemies.length-1;i>=0;i--){
_root.ship.enemies.hitTest(_root.ship.body)
{
this.explode();
if (_root.ship.shield._visible == false)
{
_root.ship.updateHealth(-20);
_root.ship.initShake();
} // end if
var _loc3 = _root.attachMovie("SmallExplosion", "SmallExplosion" + _root.getNextHighestDepth(), _root.getNextHighestDepth());
_loc3._x = _root.ship._x;
_loc3._y = _root.ship._y;
}} // end if
if(shaking == true){ this.shake(); }
} // End of the function
function takeDamage()
{
if (health < 2) { this.explode(); }
else { health -= 1; initShake();}
} // End of the function
function explode()
{
var _loc4 = _root.attachMovie("Explosion", "Explosion" + _root.getNextHighestDepth(), _root.getNextHighestDepth());
_loc4._x = _x;
_loc4._y = _y;
_root.ship.updateScore(100);
_root.ship.kills = _root.ship.kills + 1;
var _loc3 = _root.attachMovie("RewardPoints", "RewardPoints" + _root.getNextHighestDepth(), _root.getNextHighestDepth());
_loc3._x = _x;
_loc3._y = _y;
_loc3.field.text = 100;
_root.soundFX.attachSound("big_explosion.wav");
_root.soundFX.start();
this.removeMovieClip();
} // End of the function
function initShake()
{
shaking = true;
shakeDuration = 10;
_rotation = 5;
var _loc5 = _root.attachMovie("SmallExplosion", "SmallExplosion" + _root.getNextHighestDepth(), _root.getNextHighestDepth());
_loc5._x = _x;
_loc5._y = _y;
} // End of the function
function shake()
{
_rotation = _rotation * -1;
shakeDuration = shakeDuration - 1;
if (shakeDuration == 0)
{
shaking = false;
_rotation = 0;
} // end if
}
} // End of Class
Copy link to clipboard
Copied
that hittest shouldn't be in the enemy class. it should be in some kind of controller class.
or, if you want to have each enemy run its own hittest, don't use an array to store enemy instances and use this.hitTest(_root.ship.body)
Copy link to clipboard
Copied
this.hitTest works for enemy, but I am trying to make it like enemy.body.hitTest or something like that because I want to make a "mask" for the enemy.
how do I do it in controller class?
Copy link to clipboard
Copied
you can do it in a controller class using the array and code i showed previously, but you should probably use a this.body.hitTest() in your enemy class.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now