1.获取当前场景名称
cc.director.getScene().name;
2.监听屏幕事件 on是开启,off是关闭
this.node.on(cc.Node.EventType.TOUCH_START, ()=>{}, this);
this.node.on(cc.Node.EventType.TOUCH_END, ()=>{}, this);
this.node.on(cc.Node.EventType.TOUCH_CANCEL, ()=>{}, this);
this.node.on(cc.Node.EventType.TOUCH_MOVE, ()=>{}, this);
3.包围盒:判断this.point是否包含在this.node的区域
//触摸屏幕获取到的是世界坐标,需要转换为局部坐标再判断
var pos = new cc.Vec2(event.getLocationX(), event.getLocationY());
this.point = this.node.convertToNodeSpaceAR(pos);
this.node.getBoundingBox().contains(this.point);
4.显示倒计时,实时更新时间
//传入需要倒计时的时间,单位秒,返回格式 5:00
getSecond(_secondNow) {
var _hour = parseInt(_secondNow / 3600);
var _hourLabel = null;
if (_hour >= 10) {
_hourLabel = _hour;
} else
_hourLabel = "0" + _hour;
var _minite = parseInt((_secondNow - _hour * 3600) / 60);
var _miniteLabel = null;
if (_minite >= 10) {
_miniteLabel = _minite;
} else
_miniteLabel = "0" + _minite;
var _second = parseInt(_secondNow - _hour * 3600 - _minite * 60);
var _secondLabel = null;
if (_second >= 10) {
_secondLabel = _second;
} else
_secondLabel = "0" + _second;
// var _string = _hourLabel + ":" + _miniteLabel + ":" + _secondLabel;
var _string = _miniteLabel + ":" + _secondLabel;
return _string;
},
5.如果一个节点上挂了两个或者多个animation,并且需要在不同时机调用,调用方法:
let anim1 = this.anim.getComponent(cc.Animation);//这里anim是挂载anim动画的节点
anim1.play("这里需要填的是节点上挂的动画名字");
监听回调:
anim1.on('stop', ()=>{}, this);
anim1.on('finished', ()=>{}, this);
play : 开始播放时
stop : 停止播放时
pause : 暂停播放时
resume : 恢复播放时
lastframe : 假如动画循环次数大于 1,当动画播放到最后一帧时
finished : 动画播放完成时
6.进度条默默缩减
this.schedule(() => {
this.jindu.progress != 0 && (this.jindu.progress -= 0.005)
}, 0.01)
7.监听spine播放完成
let spine = this.kuangda.getComponent(sp.Skeleton);
let track = spine.setAnimation(0, 1, false);//1是指定的时间线
if (track) {
// 注册动画的结束回调
spine.setCompleteListener((trackEntry, loopCount) => {
spine.setAnimation(0, 1, true);
});
}