A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [RESOLVED] Another removeChild problem...

  1. #1
    Senior Member
    Join Date
    Jun 2002
    Posts
    400

    resolved [RESOLVED] Another removeChild problem...

    Friends,

    This little bit of code is in a click event handler that among many other things is supposed to load and unload an image. Well, it loads the image just fine, but on the second click through the image remains. What am I missing?

    Thanks.

    Actionscript Code:
    switch (clickNumber) {
            case 0 :
                var mainImageLoader:Loader = new Loader();
                mainImageLoader.load(new URLRequest("large/"+logoArray[firstCard]+".jpg"));
                addChild(mainImageLoader);
                mainImageLoader.x = 640;
                mainImageLoader.y = 65;
                clickNumber++;
                break;
            case 1 :
                removeChild(mainImageLoader);
                break;
            default :
                trace("ERROR: Not 0 or 1");
        }

  2. #2
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    mainImageLoader.parent.removeChild(mainImageLoader );
    ~calmchess~

  3. #3
    Senior Member
    Join Date
    Jun 2002
    Posts
    400
    That didn't seem to work. It's as if the mainImageLoader disappears after it's left the switch statement...

    Actionscript Code:
    switch (clickNumber) {
            case 0 :
                var mainImageLoader:Loader = new Loader();
                mainImageLoader.load(new URLRequest("large/"+logoArray[firstCard]+".jpg"));
                addChild(mainImageLoader);
                mainImageLoader.x = 640;
                mainImageLoader.y = 65;
                clickNumber++;
                trace(mainImageLoader);
                break;
            case 1 :
                trace(mainImageLoader);
                mainImageLoader.parent.removeChild(mainImageLoader );
                break;
            default :
                trace("ERROR: Not 0 or 1");
        }
    Outputs...
    [object Loader]
    null
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at concentration_fla::MainTimeline/mouseDowned()

  4. #4
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    I cannot determine from the code if a swtich statement would cause this issue can you use if statements instead of switch statment to see if it straightens up?
    ~calmchess~

  5. #5
    Senior Member
    Join Date
    Jun 2002
    Posts
    400
    Same output in an IF statement, weird.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It's because your mainImageLoader variable is local to the function. After the first execution, that variable falls out of scope. In the second execution, a new variable mainImageLoader is created, which is not the same one as the first execution. Since it is not initialized when clicknumber is not 0, you get a nullpointer error.

    The easiest way to fix it is to move the declaration of mainImageLoader outside the function.

  7. #7
    Senior Member
    Join Date
    Jun 2002
    Posts
    400
    5TonsOfFlax... still saving my bacon after all these years... thanks.

Tags for this Thread

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