A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Bullet off stage - remove

  1. #1
    Member
    Join Date
    Jun 2004
    Posts
    83

    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.

  2. #2
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    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!

  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    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.

  4. #4
    Member
    Join Date
    Jun 2004
    Posts
    83
    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...

  5. #5
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Can you post the code where you attach the eventListener?

  6. #6
    Member
    Join Date
    Jun 2004
    Posts
    83
    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);
    }

  7. #7
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Give this a try:

    PHP Code:
    //function for moving the bullet
    function moveBullet(e:Event){
        var 
    b:Bullet Bullet(e.target);
        
    b.+= b.xMove
        b
    .+= b.yMove;

        if(
    b.stage.stageWidth){
            
    b.removeEventListener(Event.ENTER_FRAMEmoveBullet);
            
    b.parent.removeChild(b);
        }


  8. #8
    Member
    Join Date
    Jun 2004
    Posts
    83

    Thumbs up

    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center