弹窗背景随文字长度变化

const {ccclass, property} = cc._decorator;

@ccclass
export class Toast extends cc.Component {

@property(cc.Node)
bg: cc.Node = null;
@property(cc.Label)
label: cc.Label = null;

public constructor() {
    super();
}

onLoad() {
}

** init(text: string) {**
** this.label.overflow = cc.Label.Overflow.NONE;**
** this.label.string = text;**
** let labelWidth = this.label.node.width;**
** this.bg.width = labelWidth + 45;**


** }**

public show(text: string, delay?: number) {
     this.init(text);
    let delayTime = 3;
    if (delay) {
        delayTime = delay;
    }
    this.node.opacity = 150;
    this.node.runAction(cc.sequence(
        cc.fadeIn(0.5),
        cc.delayTime(delayTime),
        cc.fadeOut(1),
        cc.callFunc(this.dismiss, this)
    ));
}

private dismiss(): void {
    this.node.destroy();
}

}
为啥背景不会随文字长度变化而改变长度了呢?感觉有点懵,不知道问题出在哪里,之前是可以的,希望大神们给点意见

label,赋值后,大小要下一帧才有变化。

不是很明白,刚接触cocos,那请问我这个应该如何去修改呢?

可以将设置背景尺寸的代码写在update里面

可以用scheduleOnce延迟0.1秒,在里面更改背景大小,或者在runAction里,经过一小段时间,再加个cc.callFunc里面更改背景大小

延迟可以,但是窗口弹出时效果不好,会闪现一下

可以实现,但是不知道每一帧都去刷新会不会影响整个项目的运行效率

scheduleOnce传入0,就会在下一帧执行

1赞

我试试:2: