A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Having trouble with bullets and enemies... should be very simple

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    9

    Having trouble with bullets and enemies... should be very simple

    Hello, I was wondering if any of you could clear up a really big confusion I'm having. I simply want to have a player be able to shoot bullets at randomly spawning enemies. I havent been able to do this in days, so I'm finally asking for help.

    Everything occurs in my .as file called PlayScreen. PlayScreen creates the player, asteroids, and bullets. My first approach was to create two Arrays in PlayScreen, one for bullets and one for enemies. Then addChild(enemy) and add the enemy object to the Array. Same with the bullets for when you shoot. But I wasn't able to get the hitTesting right, I kept getting errors and didn't understand how to tell a certain asteroid to explode when it gets hit by the bullet.
    My second approach was to create a bulletContainer movieclip class in PlayScreen, and do bulletContainer.addChild(bullet), while having enemies appear in PlayScreen. But it seems I'd have to go into the enemy class and try to hitTest it againt the bulletContainer, but I don't understand how to access the bulletContainer class from the enemy class. I thought making it public static var would work, but it doesn't.

    How do you access non-static variables in one class from other classes?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    To access any object, you need a reference to it. You could get that reference by having a variable set, or by getting it from another object you already have a refernce to which has one. But don't do either of those.
    Your Enemies should not be testing themselves against bullets. Something that knows about both enemies and bullets should handle that logic.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    To access any object, you need a reference to it. You could get that reference by having a variable set, or by getting it from another object you already have a refernce to which has one. But don't do either of those.
    Your Enemies should not be testing themselves against bullets. Something that knows about both enemies and bullets should handle that logic.

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    9
    So from what I understand, I need to have two arrays, one for bullets and one for enemies. Then I run a for each loop, for each object in the enemy array hitTest against each object in the bullet array... I can do that, but the problem I run into is telling that specific instance of the bullet and the enemy to explode and disappear. What does the code for that look like?
    If I try this:

    Code:
    		public function eFrame(event:Event):void
    		{
    			for each(projectile in projectileArray)
    			{
    				for each(asteroid in asteroidArray)
    				{
    					if (asteroid.hitTestObject(projectile))
    					{
    						removeChild(asteroid);
    						removeChild(projectile);
    					}
    				}
    			}
    
    		}

    I get:


    Code:
    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    	at flash.display::DisplayObjectContainer/removeChild()
    	at PlayScreen/eFrame()

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You get that error when you try to remove an object from something other than its parent. Broadly, that happens in two ways:
    1. It is actually a child of another parent. For instance, if your projectiles were added to a projectile container, you'd have to remove them from that:
    Code:
    projectileContainer.removeChild(projectile);
    This may or may not apply in your case.
    2. It is a child of no parent, because it was already removed. This definitely applies in your case because after you remove it from the display, you are not removing it from the array. It will still be in the array on the next frame and your code will probably try to remove it a second time. You should process the arrays in reverse so that the index shifting only affects those you've already processed, and you'll need to splice out the removed element when you remove it from the display.

    Code:
    public function eFrame(event:Event):void	{
      for (var i:int = projectileArray.length; i >=0; i--){
        var projectile:Projectile = projectileArray[i];
        for (var j:int = asteroidArray.length; j >=0; j--){
          var asteroid:Asteroid = asteroidArray[j];
          if (asteroid.hitTestObject(projectile))	{
            removeChild(asteroid);
            asteroidArray.splice(i, 1);
            removeChild(projectile);
            projectileArray.splice(j,1);
            break; //don't process any more asteroids because this projectile was used up on this one
          }
        }
      }
    }

  6. #6
    Junior Member
    Join Date
    Nov 2010
    Posts
    9
    that seems to make sense, but it has a problem with any if statement inside the for loops. I get the error:

    Code:
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    	at PlayScreen/eFrame()

    It seems to be checking a non-existent asteroid against a non-existent projectile, do I have to set up a listener after spawning asteroids and projectiles that tells it to run the if statement so they exist first?

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Oh, whoops. I started each at the length, but it should have started at length -1.

  8. #8
    Junior Member
    Join Date
    Nov 2010
    Posts
    9
    Worked! Thanks a lot

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