PDA

View Full Version : [AS2] collision detection between multiple instances.


jon_lad
08-29-2011, 10:38 PM
i am trying to make a scrolling shooter and have used the following code to create the enemys
function enemy1Gen():Void{
//atach enemy and behaivior
var enemy1_mc:MovieClip = _root.attachMovie("enemy1", "e1_"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
for(var i:Number = 0; i < _root.getNextHighestDepth(); i++){
_root["e1_"+i].apply(collision());
}
i have used the same method to create the bullets
function fireWeapon():Void
{
//check if weapon is reloaded, if true, fire bullet
if(reloadComplete == true)
{
//attach bullet
var bullet_mc:MovieClip = _root.attachMovie("bullet", "b"+_root.getNextHighestDepth(), _root.getNextHighestDepth());

//position bullet on player
bullet_mc._x = player_mc._x;
bullet_mc._y = player_mc._y - 32;

then the collision function is
function collision():Void{
if (this.hitTest(_root["b"+i])){
score += point1;
this._x = Math.random()*Stage.width;
this._y = -32-Math.random()*300;
trace(score);
}
if (this.hitTest(_root.player_mc)){
health -= colDam;
this._x = Math.random()*Stage.width;
this._y = -32-Math.random()*300;
trace(health);
}
};
i have put this in an on enter frame event handler in the for loop
for(var i:Number = 0 ; i<100; i++){
_root["e1_"+i].collision();
}
the results are that the variable health decreases at random intervals and the frame jumps about randomly collisions have no effect either between the bullets or the player.
i have tried putting the enemy1_mc name into the collision function instead of .this and taken it out the for loop and getting rid of the .apply this just makes the health variable go down on collisions but the enemy1_mc dosent move and collisions between it and bullets still do nothing.
any help would be appreciated.

jon_lad
08-30-2011, 11:22 PM
i changed my code now so i create instances of objects rather than instances of movie clips but now the code wont compile it says theres a syntax error on line 104

heres the code for the constructor
function enemy1():Void{
this.attachMovie("enemy1","e1_"+enemy1.getNextHighestDepth(), enemy1.getNextHighestDepth());
this.onEnterFrame = function(){
if(this._y > Stage.height){
this._x = Math.random()*Stage.width;
this._y = -32 - Math.random()*300;
}else{
this._y += 4;
}
if(this.hitTest(player_mc)){
this._x = Math.random()*Stage.width;
this._y = -32 - Math.random()*300;
health -= 30;
};
if(this.hitTest(player_mc)){
this._x = Math.random()*Stage.width;
this._y = -32 - Math.random()*300;
score += 5;
}
}
};

and this is the code i use to create instances with line 104 marked n1 is declared earlier in the script.
function enemy1Gen():Void{
n1 += 1;
//atach enemy
104. var _root["ienemy1"+n1]:Object = new enemy1();
//position enemy
_root["ienemy1"+n1]._x = Math.random()*Stage.width;
_root["ienemy1"+n1]._y = -32-Math.random()*300;
//start countdown
readyEnemy1();
once again any help would be appreciated

rrh
09-01-2011, 12:14 AM
I think it's borking from
var _root["ienemy1"+n1]:Object
The "var *:Class" pattern is expecting a variable name in there. If you do it without that, just _root["ienemy1"+n1] = new enemy1();it might still work.

Unless there's a problem with the constructor. I don't really use AS2 so maybe it handles constructors a lot differently from AS3 but it seems off.

In as3 you would have to put it in a class named "enemy1" and you wouldn't set it to return Void. Maybe look for a tutorial on classes in as2 if you want a constructor?

Or you could make just a function that returns the new enemy, like:

function newEnemy():MovieClip {
var nenemy = attachMovie(...)
nenemy.onEnterframe = function ...
...
return nenemy;
}

jon_lad
09-02-2011, 04:34 PM
Thanks for the help it was a problem with the var before the object though i may have to change the constructor as well because the movie clip is not attaching but im sure i can work this out thanks again.