如何删除createJS中绘制的文本
How to delete drawn text in 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 是我的全局文本实例
在初始页面上我这样称呼它:
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)。
谁能帮帮我?
你可以使用
finish_containerbox.removeAllChildren();
或者让你的函数 'drawString' return 实例
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);
要基于@Barman 的回答,您需要保存对实例的引用,以便稍后删除它们。
如果您要处理不确定数量的实例,那么您可能需要使用数组。
var textInstances = [];
// ...
textInstances.push(this.drawString(...)); // drawString should return the instance
// ...
while (textInstances.length) {
var text = textInstances.pop();
text.parent.removeChild(text);
}
我有这个功能
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 是我的全局文本实例
在初始页面上我这样称呼它:
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)。
谁能帮帮我?
你可以使用 finish_containerbox.removeAllChildren();
或者让你的函数 'drawString' return 实例 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);
要基于@Barman 的回答,您需要保存对实例的引用,以便稍后删除它们。
如果您要处理不确定数量的实例,那么您可能需要使用数组。
var textInstances = [];
// ...
textInstances.push(this.drawString(...)); // drawString should return the instance
// ...
while (textInstances.length) {
var text = textInstances.pop();
text.parent.removeChild(text);
}