[AS3] Some problems in simple space shooter game

Subscribe to [AS3] Some problems in simple space shooter game 7 posts

Sign in to reply


 
avatar for Hudacik Hudacik 10 posts
Flag Post
/*Contains functions for shooting, ship creation, enemy creation, background */

package
{
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.Stage;
	
	public class documentClass extends MovieClip
	{
		
		public var lod:Ship;
		public var pozadie:Background;
		public var nepriatel:Enemy;
		public var strela:Bullet;
		public var explozia:Explosion;
		
		private var enemyReload:uint = 30;
		
		function documentClass():void
		{
			strela = new Bullet(1000,1000); 
			addChild(strela); //takes care of null pointer reference runtime errors
			pozadie = new Background();
			pozadie.x = 508;
			pozadie.y = -58;
			pozadie.cacheAsBitmap = true;
			addChild(pozadie);
			lod = new Ship(150,150);
			addChild(lod);
			stage.addEventListener(MouseEvent.CLICK, fireBullet);
			stage.addEventListener(Event.ENTER_FRAME, createEnemies);
		}
		private function fireBullet(event:MouseEvent)
		{
			strela = new Bullet(lod.x+23, lod.y);
			strela.cacheAsBitmap = true;
			addChild(strela);
		}
		private function createEnemies(e:Event)
		{
			enemyReload++;
			if(enemyReload >= 50)
			{
				nepriatel = new Enemy();
				nepriatel.x = 600;
				nepriatel.y = Math.random()*500;
				nepriatel.addEventListener(Event.ENTER_FRAME, collisions);
				nepriatel.addEventListener(Event.ENTER_FRAME, garbage);
				addChild(nepriatel);
				enemyReload = 0;
			}
			
		}
		private function collisions(e:Event)
		{
			if(nepriatel.hitTestPoint(strela.x,strela.y,true))			
			{
				trace("enemy hit!");
				explozia = new Explosion(nepriatel.x, nepriatel.y);
				explozia.addEventListener(Event.ENTER_FRAME, removeExplosion);
				addChild(explozia);
				removeChild(nepriatel);
				removeChild(strela);
			}
		}
		private function garbage(e:Event)
		{
			if(nepriatel.x < 100)
			{
				trace("hey!");
				removeChild(nepriatel);
			}
		}
		private function removeExplosion(e:Event)
		{
			if(explozia.currentFrame == explozia.totalFrames)
			{
				removeChild(explozia);
			}
		}
	}
}

First of all, after checking for collision, it removes the bullet and the enemy, but I start getting:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at documentClass/removeExplosion()
every frame. It does, however, remove the things I want it to first.
Secondly, the collision only works sometimes, especially when I shoot a large amount of bullets, they just pass through enemies, but I can’t determine what is causing this, because sometimes it works fine.
I’m new to AS3, can somebody help me out with this?

 
avatar for MCOBigBen MCOBigBen 256 posts
Flag Post

If you use tags < pre > and </ pre > before and after your code, it will be easier for folks to read and identify the problem.

I’m afraid I don’t know AS3 enough to be able to help.

{edit} looks like you figured it out while I was typing. You can edit your original post if you don’t want to double post.

 
avatar for illegal illegal 61 posts
Flag Post

Hi Hudacik

First off you could always edit your original question.

Secondly your trying to remove a explosion from the stage when it doesn’t exist

 
avatar for Hudacik Hudacik 10 posts
Flag Post

illegal: How do I prevent it? I need to remove them somehow.

 
avatar for CuriousGaming CuriousGaming 394 posts
Flag Post

In your collisions function, you are referring to nepriatel, which is always going to be the most recent enemy you’ve added to the screen. strela is the most recent bullet you’ve added to the screen. If one of these obejcts is removed after a collision, those functions are trying to look at objects which aren’t there any more.

You will need to put your enemies and bullets into arrays, and use for loops to check through each of them.

 
avatar for Hudacik Hudacik 10 posts
Flag Post

Thank you, I’m going to try it.

 
avatar for KMAE KMAE 439 posts
Flag Post

Huda, I had your second problem I think on one of my games. This game was in AS2, but the concept should be the same. Hopefully this is similar enough to your problem to help. When shooting bullets at enemies I had the same enemy getting hit multiple times so some of the bullets weren’t going through. What I did was add a hit flag, so if a bullet hit it it went to “1” or “0”. Then in my hit test I would check that not only did a bullet hit the enemy movieclip but also that it wasn’t “hit” previously.

Sign in to reply