|
-
Enemies in array, AS3
Hey guys
I'm making a Space Shooter Game for school and I just can't find how to put my enemies in an array.
I found this code in an other thread
Code:
public function checkVirusCollisions() {
// viruses
var i:int = ("virus"+1);
for(var j:int = 0; j < arrEnemies.length; j++){
if (arrEnemies[j].hitTestObject(arrBullets[i])) {
enemieDie(i);
bulletDie(j);
}
So what I need to make is 2 arrays, one arrEnemies where I put my emenies in and the other one arrBullets where the bullets go.
Then these 2 arrays do a collision check
can someone help me? :s thanks allot in advance
-
I thought I'd better post some code 
so this is where I create my Enemy
Code:
private var arrEnemies:Array = new Array ;
var newEnemy:Enemy = new Enemy ;
newEnemy.scaleX = 0.2;//trace(ship.scaledX);
newEnemy.scaleY = 0.2;//trace(ship.scaledY);
newEnemy.x = 850;
//random y-coordinaten van de spawn
newEnemy.y = int(Math.random() * stage.stageHeight);
//then add the enemy to stage and to array
addChild(newEnemy);
arrEnemies.push("newEnemy" + (arrEnemies.length +1));
//random enemyTime for random spawn
enemyTime = int(Math.random() * 21) - 10;
and this is where I do a check collision with between my ship and and the arrEnemies
ship is put on stage with: addChild(ship);
Code:
function checkCollision(event:Event)
{
for (var j:int = 0; j < arrEnemies.length; j++)
{
if (arrEnemies[j].hitTestObject(ship))
{
//testing if I get a collision detection
addChild(ship);
}
}
so this doesn't work anyone a tip?
-
You are not putting your new Enemy in arrEnemies. You are putting a String in. Don't do that.
Change this line:
Code:
arrEnemies.push("newEnemy" + (arrEnemies.length +1));
to this:
Code:
arrEnemies.push(newEnemy);
And you'll actually have Enemies in your arrEnemies array.
Some other stuff that doesn't make sense:
Code:
var i:int = ("virus"+1);
This just doesn't work. You are creating a String ("virus1") but trying to assign that to an int.
You also seem to be using hitTestObject on an objet which is not on the display, since you add it later. That won't work.
When you are testing your bullets vs your enemies, you need to test each bullet against each enemy. The simple way to do this is with a nested loop.
Code:
public function checkVirusCollisions() :void{
for (var i:int = 0; i < arrBullets.length; i++){
for(var j:int = 0; j < arrEnemies.length; j++){
if (arrEnemies[j].hitTestObject(arrBullets[i])) {
//you had i and j backwards here.
enemieDie(j);
bulletDie(i);
}
}
}
}
In terms of good coding practice, you should also always provide a return type for your functions, and explicitly use the function form of the constructor, even when it takes no arguments.
Code:
var newEnemy:Enemy = new Enemy();
Note the parentheses. For no-argument constructors, this is purely stylistic, but it keeps your syntax uniform with other "new" calls which do take arguments.
-
wow thanks I made some really stupid stuff in my code 
but how do I delete those specific enemies and bullets that collided with each other? :s
thanks allot already for the time you put into writing that huge comment
-
I thought enemyDie and bulletDie were already written and working.
At a high-level, you'll need to splice those instances out of their respective arrays and perform any finalization code. If your enemies have death animations, you'll need to start those. And after any animations are done, you'll need to remove them from the displaylist. One VERY IMPORTANT consideration is that if you do splice out the instances, you will have changed the array, so you must either alter your i,j indices or process from the ends down so that the rearranging doesn't matter.
-
holy * 
could you help me out with some code :s, I understand half of it because of my bad english :s and because I have almost no experience with as3 
I'm really sorry for the trouble
-
Code:
public function checkVirusCollisions() :void{
for (var i:int = arrBullets.length -1; i >= 0; i--){
for(var j:int = arrEnemies.length -1; j >= 0; j--){
var e:Enemy = arrEnemies[j];
var b:Bullet = arrBullets[i];
if (e.hitTestObject(b)) {
arrEnemies.splice(j, 1);
arrBullets.splice(i, 1);
//assumes no death animation
e.parent.removeChild(e);
b.parent.removeChild(b);
}
}
}
}
-
So I just put this code
Code:
public function checkVirusCollisions() :void{
for (var i:int = arrBullets.length -1; i >= 0; i--){
for(var j:int = arrEnemies.length -1; j >= 0; j--){
var e:Enemy = arrEnemies[j];
var b:Bullet = arrBullets[i];
if (e.hitTestObject(b)) {
arrEnemies.splice(j, 1);
arrBullets.splice(i, 1);
//assumes no death animation
e.parent.removeChild(e);
b.parent.removeChild(b);
}
}
}
}
in the place of this code
Code:
public function checkVirusCollisions() :void{
for (var i:int = 0; i < arrBullets.length; i++){
for(var j:int = 0; j < arrEnemies.length; j++){
if (arrEnemies[j].hitTestObject(arrBullets[i])) {
//you had i and j backwards here.
enemieDie(j);
bulletDie(i);
}
}
}
}
??
thanks allot for your help already, I couldn't get this working without you
-
Pretty much. If you had enemieDie and bulletDie functions already that do more than take them off screen, you will need to fit that in somewhere, too.
-
how should those enemieDie and bulletDie functions look like?
thanks
-
You don't need them at all unless there were other side effects to do when those die.
-
Yea it just looks easier to me with the functions *Die()
and because I can't get this code working
Code:
public function checkVirusCollisions() :void{
for (var i:int = arrBullets.length -1; i >= 0; i--){
for(var j:int = arrEnemies.length -1; j >= 0; j--){
var e:Enemy = arrEnemies[j];
var b:Bullet = arrBullets[i];
if (e.hitTestObject(b)) {
arrEnemies.splice(j, 1);
arrBullets.splice(i, 1);
//assumes no death animation
e.parent.removeChild(e);
b.parent.removeChild(b);
}
}
}
}
If I wanna do it with this code
Code:
public function checkVirusCollisions() :void{
for (var i:int = 0; i < arrBullets.length; i++){
for(var j:int = 0; j < arrEnemies.length; j++){
if (arrEnemies[j].hitTestObject(arrBullets[i])) {
//you had i and j backwards here.
enemieDie(j);
bulletDie(i);
}
}
}
}
then wat code should i type for the functions? something the same as the first box of code in this reply?
-
In that case they could be defined like this.
Code:
private function enemieDie(index:int):void{
var e:Enemy = arrEnemies[index];
if (e.parent){
e.parent.removeChild(e);
}
arrEnemies.splice(index, 1);
}
private function bulletDie(index:int):void{
var b:Bullet = arrBullets[index];
if (b.parent){
b.parent.removeChild(b);
}
arrBullets.splice(index, 1);
}
Note that you could refactor that pretty easily into a single method which takes both an array and an index...
-
damn why can't I get this working It doesn't give any errors at all but doesn't work...
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|