dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: How to check if a clip is on the scene

  1. #1
    Member
    Join Date
    Jan 2008
    Posts
    31

    How to check if a clip is on the scene

    Hi
    I would like to know if there is a method, a property or a function to know if a movieclip is on the scene of my flash file.

    Here is the thing: I would like to use the removeChild function on some movieclips but only when they are actually on the scene so that I don't get error messages.

    Does something like this exist:

    Code:
    if(my_movie_clip.is_on_the_scene){ 
          removeChild(my_movie_clip); 
    }

    The only solution I found would be to deal with a addEventListener(Event.ADDED_TO_STAGE, my_function) to check a variable that would equal 1 if the clip was added on the scene and that would equal 0 if the clip was removed from the scene... but I'm searching for something more simple.

    thanks for your help

  2. #2
    Member
    Join Date
    Jan 2008
    Posts
    31
    I found the solution using this:
    Code:
     if(myclip.stage==null){ 
       trace('off the scene'); 
    }
    if(myclip.stage!=null){ 
       trace('on the scene'); 
    }
    cheers

  3. #3
    Junior Member
    Join Date
    Feb 2008
    Posts
    24
    My first idea was this:
    Code:
    try {
          removeChild(my_movie_clip);
    } catch(e:Error) {}
    But yours is better.

  4. #4
    Member
    Join Date
    Jan 2008
    Posts
    31
    FYI, this also works
    Code:
    function isOnTheScene(my_mc:DisplayObject):Boolean { 
       return (my_mc.stage != null); 
    }

  5. #5
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    I once wrote this ; it's enough to check if(yourMc) { } :

    PHP Code:
    // add a stage enter frame event listener
    stage.addEventListener(Event.ENTER_FRAMEonEnter);

    function 
    onEnter(event:Event):void
    {
        
    // check if "yourMovieClip" movie clip exists on the current frame
        // if it exists, then trace the number of the frame that hold the movie clip
        // else, display a message "NULL" ( doesn't exist on the current frame )
        
    (this.yourMovieClip) ? trace(this.currentFrame) : trace("NULL");

        
    // if the animation has reached it's last frame
        
    if (this.currentFrame == this.totalFrames)
        {
            
    // remove the stage's enter frame event listener
            
    stage.removeEventListener(Event.ENTER_FRAMEonEnter);
        }
    // end of onEnter 
    Hope it helped.

    EDIT: I uploaded an example:
    Last edited by fx.barrett; 10-01-2008 at 03:21 AM.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  6. #6
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,046
    Quote Originally Posted by derari
    My first idea was this:
    Code:
    try {
          removeChild(my_movie_clip);
    } catch(e:Error) {}
    But yours is better.
    That's exactly what I do just to suppress the error. Then I don't need to worry about actually checking if it's a child. Basically it's saying, remove it if it's there, if not, oh well.

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