在一个alert Prefab的脚本上,properties中设置了label,想在使用时修改里面Label的位置,但是node为null,setPosition也没成功,直接设置x,y也不行。具体代码如下:
cc.Class({
extends: cc.Component,
properties: {
alertType: {
default: null,
type: cc.Sprite
},
alertContent: {
default: null,
type: cc.Label
}
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
show ({content, type}) {
// console.log(this.alertContent)
let aType = this.alertType.getComponent(cc.Sprite)
aType.trim = false
console.log(this.alertContent, this.alertContent.getComponent(cc.Label))
this.alertContent.string = content
switch (type) {
case 'success':
cc.loader.loadRes('Alert/icon_tick', cc.SpriteFrame, (error, icon) => {
aType.spriteFrame = icon
})
if (this.alertContent.node.width > (this.node.width / 2 - 20)) {
this.node.width = 110 + this.alertContent.node.width
}
break
case 'warning':
cc.loader.loadRes('Alert/icon_warning', cc.SpriteFrame, (error, icon) => {
aType.spriteFrame = icon
})
if (this.alertContent.node.width > (this.node.width / 2 - 20)) {
this.node.width = 110 + this.alertContent.node.width
}
break
case 'error':
cc.loader.loadRes('Alert/icon_delete', cc.SpriteFrame, (error, icon) => {
aType.spriteFrame = icon
})
if (this.alertContent.node.width > (this.node.width / 2 - 20)) {
this.node.width = 110 + this.alertContent.node.width
}
break
case 'info':
default:
aType.spriteFrame = null
this.alertContent.node.x = 0
this.alertContent.node.y = 0
if (this.alertContent.node.width > (this.node.width - 60)) {
this.node.width = this.alertContent.node.width + 60
}
break
}
this.scheduleOnce(() => {
this.hide()
}, 1.5)
},
hide () {
this.node.destroy()
},
start () {
},
// update (dt) {},
});