A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: Enemies in array, AS3

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    10

    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

  2. #2
    Junior Member
    Join Date
    Jan 2011
    Posts
    10

    Unhappy

    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?

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    10
    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

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    10
    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

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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);
         }
       }
      }
    }

  8. #8
    Junior Member
    Join Date
    Jan 2011
    Posts
    10
    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

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  10. #10
    Junior Member
    Join Date
    Jan 2011
    Posts
    10
    how should those enemieDie and bulletDie functions look like?
    thanks

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You don't need them at all unless there were other side effects to do when those die.

  12. #12
    Junior Member
    Join Date
    Jan 2011
    Posts
    10
    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?

  13. #13
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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...

  14. #14
    Junior Member
    Join Date
    Jan 2011
    Posts
    10
    damn why can't I get this working It doesn't give any errors at all but doesn't work...

  15. #15
    Junior Member
    Join Date
    Jan 2011
    Posts
    10

    resolved

    ah lolz I found why it wasn't working

    I had this code in comment:
    Code:
    addEventListener(Event.ENTER_FRAME, checkCollision);
    so no wonder the collisonDetection wasn't working

    thanks a lot "5TonsOfFlax" I couldn't have done this without you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center