-
Bullet off stage - remove
Hi,
I want to remove bullets when they go off stage.
MoveBullet is my function that animates the bullet.
Code:
if(e.target.x > stage.stageWidth){
e.target.removeListener(Event.ENTER_FRAME, moveBullet);
removeChild(MovieClip(e.target));
trace("out");
}
My error output:
TypeError: Error #1006: removeListener is not a function.
at shooting_fla::MainTimeline/moveBullet()
if I comment away the "removeListener line" I get the following error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display:isplayObjectContainer/removeChild()
at shooting_fla::MainTimeline/moveBullet()
What could be the problem?
/S-fish
Last edited by swordfish123; 09-26-2008 at 05:00 AM.
-
lemon juice hurts your eyes
Try:
Code:
e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
MovieClip(parent).removeChild(e.target);
Florian Vanthuyne
WAR AGAINST SOLVED THREADS
mark yours as Resolved under Thread Tools!
-
One small correction: You need to scope parent correctly -
PHP Code:
DisplayObjectContainer(e.target.parent).removeChild(e.target);
Also, you could use weak event listeners and let them clean up after themselves automatically.
-
No this doesn't work for me..I got an error output:
1118: Implicit coercion of a value with static type Object to a possibly unrelated type flash.displayisplayObject.
Any idea? Maby I should try weak event listeners in this case...
-
Can you post the code where you attach the eventListener?
-
Here is some of the code, on mousePress the "shoot" variable is set to true and on release false:
Code:
stage.addEventListener(Event.ENTER_FRAME, onFrameLoop);
//loop for creating new bullets and get the direction
function onFrameLoop(evt:Event){
if (shoot){
var bullet:Bullet = new Bullet();
addChild(bullet);
bullet.xMove = shotSpeed * dir;
bullet.yMove = shotSpeed * dir;
bullet.addEventListener(Event.ENTER_FRAME, moveBullet);
}
}
//function for moving the bullet
function moveBullet(e:Event){
e.target.x += e.target.xMove;
e.target.y += e.target.yMove;
if(e.target.x > stage.stageWidth){
//This does part not work!
//e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
//DisplayObjectContainer(e.target.parent).removeChild(e.target);
}
//trace(e.target.x);
}
-
Give this a try:
PHP Code:
//function for moving the bullet
function moveBullet(e:Event){
var b:Bullet = Bullet(e.target);
b.x += b.xMove
b.y += b.yMove;
if(b.x > stage.stageWidth){
b.removeEventListener(Event.ENTER_FRAME, moveBullet);
b.parent.removeChild(b);
}
}
-
Thank you, this works for me!
Last edited by swordfish123; 09-28-2008 at 12:15 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|