这里利用添加动画作为一个演示示例
/**
* 工具类
* 作用:将所有的通用的接口封装在此 ----> 类似工具箱
*/
module.exports = {
//因为是一个对象,所以需要键值对的形式 键名 : 键值
/**
*
* @param {添加动画的节点} aniNode
* @param {精灵帧数组} frames
* @param {clip文件名---->string} aniName
* @param {播放模式} wrapMode
* @param {播放速度} speed
* @returns
*/
addAnimationClip: function (aniNode, frames, aniName, wrapMode, speed) {
let animation = aniNode.getComponent(cc.Animation);
if (!animation) {
//如果不存在组件
animation = aniNode.addComponent(cc.Animation);
}
//frames 这时一个 SpriteFrame的数组
let clip = cc.AnimationClip.createWithSpriteFrames(frames, frames.length);
clip.name = aniName;
clip.wrapMode = wrapMode;
clip.speed = speed;
animation.addClip(clip);
return animation;
},
//添加动画
addHitAnima(){
let frames = gameData.storeHouse.atlas["bulletHit"].getSpriteFrames();
let animation = gameData.tools.addAnimationClip(this.node,frames,"bulletHit",cc.WrapMode.Normal,4);
},