如何删除createJS中的绘制文本

时间:2015-04-21 11:49:10

标签: text createjs

我有这个功能

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 是我的全球文字实例

并在init页面上我这样称呼它:

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');
}

我的问题如何删除它们? 我试过用这个:

finish_containerbox.removeChild(textHowTo);

但只删除了最后一个文本(TEXT.EN.GP_TEXT_TUTORIAL_2)。

任何人都可以帮助我?

2 个答案:

答案 0 :(得分:1)

你可以使用     finish_containerbox.removeAllChildren();

或者你的功能&#39; drawString&#39;返回实例     this.myText1 = this.drawString(..);     this.finish_containerbox.addChild(this.myText1);     this.myText2 = this.drawString(..);     this.finish_containerbox.addChild(this.myText2);

以后使用这些变量删除实例:     this.finish_containerbox.removeChild(this.myText1);     this.finish_containerbox.removeChild(this.myText2);

答案 1 :(得分:1)

要构建@ Barman的答案,您需要保存对实例的引用,以便以后删除它们。

如果您正在处理不确定数量的实例,那么您可能想要使用数组。

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