The attached DisplayObject must be a child of the caller

I am a newbie and I have tried and tried this for several weeks now and I cannot handle it. The simple code below gives the message "Error on the supplied DisplayObject must be a child of the caller."

var square = new squareObj;
addChild(square);
addEventListener(Event.ENTER_FRAME, removeSquare);

function removeSquare(evt:Event):void {
    removeChild(square)
}

squareObj is a movie clip in the library that is exported for AS. How can this code be modified to work correctly? I assume that this is my knowledge at the proper level, which I do not experience, so any links to good textbooks on this subject are appreciated.

Regards H

+3
source share
1 answer

, . , DisplayObject, , ( ).

:

var square = new squareObj;
addChild(square);
addEventListener(Event.ENTER_FRAME, removeSquare);

function removeSquare(evt:Event):void {
    if (contains(square)) {
        removeChild(square)
    }
}

, this

var square = new squareObj;
addChild(square);
addEventListener(Event.ENTER_FRAME, removeSquare);

function removeSquare(evt:Event):void {
    removeEventListener(Event.ENTER_FRAME, removeSquare)
    removeChild(square)
}

, , - , . , , , , .

+6

Source: https://habr.com/ru/post/1744844/


All Articles