noobsee
noobsee

Reputation: 962

How to delete drawn text in createJS

I have this function

var textHowTo;
this.drawString = function(textToDraw, props, color, posX, posY, containerbox, lineW, aligns)
    {
        console.log("Draw String");
        var textContent_1 = textToDraw;
        textHowTo = new createjs.Text(textContent_1, props, color);

        var w = ( textHowTo.getMeasuredWidth() ) * textHowTo.scaleX;
        var h = ( textHowTo.getMeasuredHeight() ) * textHowTo.scaleY;

        //textHowTo.regY = h / 2;
        textHowTo.textAlign = aligns; 
        if (lineW > 0)
            textHowTo.lineWidth = lineW;

        //textHowTo.font = 'assets/fonts/Elite Hacker (Corroded).ttf';
        textHowTo.x = posX;
        textHowTo.y = posY;
        containerbox.addChild(textHowTo);
    }

textHowTo is my global text Instance

and on the init page I called it like this :

this.GS_Gameplay_Init = function ()
{
    module.drawString( TEXT.EN.GP_TEXT_TUTORIAL_1 , "30px Hacker", "#ffffff", (FAR_ANCHOR<<1)+50, (FAR_ANCHOR<<1) + 100, finish_containerbox, 300, 'center');

    module.drawString( TEXT.EN.GP_TEXT_TUTORIAL_2 , "15px Hacker", "#ffffff", (FAR_ANCHOR<<1)+50, (FAR_ANCHOR<<1) + 250, finish_containerbox, 200, 'center');
}

My question how do I remove both of them? I have tried using this :

finish_containerbox.removeChild(textHowTo);

But only the last text(TEXT.EN.GP_TEXT_TUTORIAL_2 ) removed.

Anyone can help me?

Upvotes: 0

Views: 441

Answers (2)

gskinner
gskinner

Reputation: 2488

To build on @Barman's answer, you need to save references to your instances so you can remove them later.

If you're dealing with an indeterminate number of instances, then you'll probably want to use an array.

var textInstances = []; // ... textInstances.push(this.drawString(...)); // drawString should return the instance // ... while (textInstances.length) { var text = textInstances.pop(); text.parent.removeChild(text); }

Upvotes: 1

Barman
Barman

Reputation: 11

You could use finish_containerbox.removeAllChildren();

or have your function 'drawString' return the instance this.myText1 = this.drawString(..); this.finish_containerbox.addChild(this.myText1); this.myText2 = this.drawString(..); this.finish_containerbox.addChild(this.myText2);

and later use these vars to remove the instance: this.finish_containerbox.removeChild(this.myText1); this.finish_containerbox.removeChild(this.myText2);

Upvotes: 1

Related Questions