Hi everyone. I have benn programming in AS2 for a while, but I’m moving to AS3, and I’m having some trouble with syntax and mostly with management of parameters and variables (actually I tried to work with functions in packages without good results T.T).
The game has four different types of shooters, and all of them use the same bullet (shoot function). I have succeeded on disappearing the target (a coin in this case) when it is hit by the bullet, but the problem is that I can’t quite find the way to dissapear the bullet properly. I keep receiving this message:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at eco_adm9_fla::MainTimeline/moveBullet()
Here’s the code, I hope you find it useful in a way.
PS Help would rrrreallly be appreciated! Thank you
var second_u:int=0;
var second_d:int=0;
var i:int=0;
var car:int=0;
var shootAllow:Boolean=true;
var timer1:Timer=new Timer(1000);
var timer_gun:Timer=new Timer(800);
var Bullet= new shoot_ball_mc();
var Shooter= new shooter1_mc();//First time Shooter
Shooter.x=mouseX;
Shooter.y=350;
addChild(Shooter);
// Checks the clock function every 1000 milisecond (1 second)
timer1.start();
timer_gun.start();
timer1.addEventListener(TimerEvent.TIMER, clock1);
timer_gun.addEventListener(TimerEvent.TIMER, shooter_clock);
timer1.addEventListener(TimerEvent.TIMER, DisplayCoins);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemove);
stage.addEventListener(KeyboardEvent.KEY_DOWN, change_shooter);
stage.addEventListener(KeyboardEvent.KEY_DOWN, shoot);
function shooter_clock(e:TimerEvent):void {
shootAllow=true;
}
// Function that increments the timer
function clock1(evt:TimerEvent):void {
// every time this function is checked increment second by one
second_u+=1;
if (second_u==10) {
second_u=0;
second_d+=1;
}
timer_txt.text=“00:”+second_d+second_u+“s”;
}
function mousemove(e:MouseEvent):void {
Shooter.x=mouseX;
Shooter.y=350;
}
// for the different shooters
function change_shooter(e:KeyboardEvent):void {
switch (e.keyCode) {
case 65 :
removeChild(Shooter);
Shooter = new shooter1_mc();
addChild(Shooter);
Shooter.x=mouseX;
Shooter.y=350;
break;
case 83 :
removeChild(Shooter);
Shooter = new shooter2_mc();
Shooter.x=mouseX;
Shooter.y=350;
addChild(Shooter);
break;
case 68 :
removeChild(Shooter);
Shooter = new shooter3_mc();
Shooter.x=mouseX;
Shooter.y=350;
addChild(Shooter);
break;
case 70 :
removeChild(Shooter);
Shooter = new shooter4_mc();
Shooter.x=mouseX;
Shooter.y=350;
addChild(Shooter);
break;
}
}
//======= creating bullets =================
function shoot(e:KeyboardEvent):void {
if (e.keyCode==Keyboard.SPACE && shootAllow==true) {
shootAllow=false;
Bullet.x=Shooter.x;
Bullet.y=Shooter.y;
addChild(Bullet);
Bullet.addEventListener(Event.ENTER_FRAME, moveBullet);
}
}
function moveBullet(e:Event):void {
trace(DisplayCoins);
e.target.y-=18;
if (e.target.y<=-10) {
e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
removeChild(MovieClip(e.target));
}
}
//==================================
//to create the coins
function DisplayCoins(e:Event) {
var newCoin= new coin_mc();
newCoin.x=50;
newCoin.y=100;
addChild(newCoin);
newCoin.addEventListener(Event.ENTER_FRAME, Coins_move);
newCoin.removeEventListener(Event.ENTER_FRAME, shoot);
}
function Coins_move(e:Event):void {
e.target.x+=3;
if (e.target.x>=400 || e.target.hitTestObject(Bullet)) {
e.target.removeEventListener(Event.ENTER_FRAME, Coins_move);
removeChild(MovieClip(e.target));
removeChild(MovieClip(Bullet));
}
}