cocos creator 公告轮播
//适用于需要轮播公告系统,本文仅是前端写法
cc.Class({
extends: cc.Component,
properties: {
maskNode:cc.Node,
label:cc.Label,
_startPos:cc.Vec2,
contentArr:[""],
},
onLoad () {
this._startPos=cc.v2(this.maskNode.width/2,0);
if(this.contentArr.length == 0){
this.node.active=false;
}
console.log(this.label.node.position);
this.label.node.position = this._startPos;
this.requestCallBack();
},
requestCallBack:function(){
this.startScroll("需要先更新标签的宽度,不然下一帧才更新");
},
startScroll:function(content){
let self = this;
if(content == null || content.length == 0)
{
return;
}
this.node.active = true;
this.contentArr.push(content);
if(self.label.node.getActionByTag(0) != null && this.label.node.getActionByTag(0).isDone() == false)//如果正在播放只插入数据
{
return;
}
let scrollFunc = function()
{
if(self.contentArr.length > 0)
{
self.label.string = self.contentArr.shift()
//需要先更新标签的宽度,不然下一帧才更新,这里取到的值就会是原来的值,导致宽度计算错误
self.label._updateRenderData(true)
self.label.node.position = self._startPos
let distance= self.label.node.width + self.label.node.parent.width
let duration = distance / 100
let seq = cc.sequence(
cc.moveBy(duration,cc.v2(-distance,0)),
cc.callFunc(function(){
self.label.string = ""
self.label.node.position = self._startPos
scrollFunc()
})
)
seq.setTag(0)
self.label.node.runAction(seq)
}
else
{
self.requestCallBack();
// self.node.active = false
}
}
scrollFunc();
},
// onDestroy()
// {
// if(this.label.node.getActionByTag(0) != null )
// {
// this.label.node.stopAction(this.label.node.getActionByTag(0))
// }
// }
});