/*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?