您的位置:首页 > 移动开发 > Cocos引擎

整理CocosCreator常用知识点

2021-04-15 04:06 1551 查看

一、场景加载

  • cc.director.loadScene(‘场景名称');//场景跳转
  • cc.director.preloadScene(‘场景名称');//预加载场景
  • cc.director.getScene();//获取当前场景

二、查找节点

1,节点查找

  • node = cc.find(“Canvas/bg”);//路径访问节点 性能消耗相对较大
  • this.node.getChildByName(‘name');//名称获取子节点 性能消耗较小
  • node.getComponent(cc.Label)//获取节点上label属性值
  • this.node; //当前脚本节点
  • this.node.parent; //父节点
  • this.node.getChildByTag(100); //通过标签获取子节点
  • cc.find(“game/test”,this.node); //通过指定节点下的路径获取节点
  • this.node.children; //获取所有子节点
  • node.getChildren(); //获取所有子节点
  • this.node.childrenCount; //获取子节点数量
  • node.getChildrenCount(); //获取子节点数量
  • cc.director.getScene(); //获取场景主节点
  • var sprites = this.node.getComponentsInChildren(cc.Label);//递归查找自身及所有子节点中指定类型的组件

2,节点其他操作

  • cc.instantiate(node);//克隆节点
  • this.node.parent = cc.find(‘Canvas');//绑定父节点
  • this.node.addChild(nodeName,zIndex,tag);//添加子节点,可设置层级和标签
  • this.node.removeChild(nodeName);//移除子节点
  • this.node.removeChildByTag (nodeTag);//通过标签移除子节点
  • this.node.destroy();//销毁节点
  • this.node.isValid;//判定节点是否可用
  • this.node.removeChild(newNode);//移除节点中指定的子节点
  • this.node.removeChildByTag(100);//通过标签移除节点中指定的子节点
  • this.node.removeAllChildren();//移除所有子节点
  • this.node.destroyAllChildren();//销毁所有子节点

3,停止播放动作以及计时器

this.node.cleanup();//停止所有正在播放的动作和计时器

三、节点属性设置

  • node.getPositionX();或 getPositionY() //X轴或Y轴坐标
  • node.getScaleX(); 或getScaleY() //X轴或Y轴缩放比例
  • node.x = 100;//设置节点x轴坐标
  • node.y = 100;//设置节点y轴坐标
  • node.setPosition(x,y); //设置节点坐标
  • node.rotation = 90; //设置节点旋转角度
  • node.scaleX = 2; //设置节点x轴缩放倍数
  • node.scaleY = 2; //设置节点y轴缩放倍数
  • node.setScale(2); //设置节点整体缩放倍数
  • node.width = 100; //设置节点宽度大小
  • node.height = 100; //设置节点高度大小
  • node.setContentSize(100, 100); //设置节点宽高尺寸大小
  • node.anchorX = 1; //设置节点x轴锚点坐标
  • node.anchorY = 0; //设置节点y轴锚点坐标
  • node.setAnchorPoint(1, 0); //设置节点锚点坐标
  • node.opacity = 255; //设置节点透明度大小(0-255)
  • node.setOpacity(20); //设置节点透明度(0~255)
  • node.color = new cc.color(100,100,100,255); //设置节点颜色(R,G,B,透明度)
  • cc.isValid(this.label.node) //判定节点是否存在
  • node.active = false; //关闭节点(隐藏节点)

常驻节点

  • cc.game.addPersistRootNode(myNode); //常驻节点(全局变量)
  • cc.game.removePersistRootNode(myNode); //取消常驻节点

四、节点动作

  • cc.show()//立即显示
  • cc.hide ()//立即隐藏
  • cc.toggleVisibility()//显隐切换
  • cc.fadeIn(1)//渐显效果
  • cc.fadeOut(1)//渐隐效果
  • cc.delayTime(1)//等待1秒
  • node.runAction(cc.moveTo(1,0,0)); //移动到当前节点(时间(s),X轴坐标,Y 轴坐标)
  • node.runAction(cc.scaleTo(1,0.7,0.8));//缩放到当前倍数节点(时间(s),X轴倍数,Y 轴倍数)
  • node.runAction(cc.rotateTo(1,160,160));//旋转到指定角度(时间(s),X轴角度,Y 轴角度)
  • node.runAction(cc.skewTo(1,5,-5));//变化节点倾斜度(时间(s),X轴倾斜度,Y 轴倾斜度)
  • node.runAction(cc.fadeTo(2,0));//变化当前节点的透明度(时间(s),透明度)
  • node.runAction(cc.tintTo(2,255,255,0));//变化当前节点颜色(时间,R,G,B)
  • node.stopAllActions();//停止所有动作
  • var action = cc.moveTo(2, 100, 100);// 创建一个动作(moveTo是移动)
  • node.runAction(action);// 执行指定动作
  • node.stopAction(action);// 停止指定动作
  • cc.sequence(action1,action2); //按顺序连续执行
  • cc.spawn(action1,action2); //同时执行
  • cc.repeatForever(cc.sequence(action1,action2)); //一直重复的动作

五、计时器

start() {
// 定时启动
// 在2S以后启动
this.scheduleOnce(() => {
cc.log("scheduleOnce")
}, 2)

//   频率  次数+1  延迟
this.schedule(() => {
cc.log("schedule")
}, 1, 3, 5)

// 永远执行
let one = this.schedule(() => {
cc.log("schedule")
}, 1, cc.macro.REPEAT_FOREVER, 2)

// 清除所有定时
this.scheduleOnce(() => {
cc.log("scheduleOnce")
this.unscheduleAllCallbacks()
}, 5)

let callb = function () {
cc.log("callb")
}
this.schedule(callb, 0.5)  //默认永远执行

this.scheduleOnce(() => {
cc.log("scheduleOnce")
this.unschedule(callb)
}, 2)
},

六、事件监听

(开始:‘touchstart',移动:‘touchmove',结束:‘touchend',取消:‘touchcancel')

node.on('touchstart',function(event){
this.doSomething();
},this);
  • event.getID();//获取触点的ID
  • event.getLocationX();//获取触摸点的坐标X
  • event.getLocationY();//获取触摸点的坐标Y
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD/TOUCH_ONE_BY_ONE,myfunction},self.node);

七、定义全局变量

window.global= “blobal string”;//任意脚本里可定义全局变量

window.G = {
a: null,
b: null,
};

任意脚本里可访问全局变量(前提是脚本已执行过)
G.a = 0;
G.b = 0;

var something = require(‘something');
cc.game.addPersistRootNode(myNode);//常驻节点,必须位于层级的根节点
module.exports = {
config: 123
}

八、分辨率

获得设备分辨率

  • var equipment= cc.director.getWinSizeInPixels()
  • var equipmentW= equipment.width
  • var equipmentH= equipment.height
  • cc.view.getCanvasSize().width;//获得设备分辨率的宽度
  • cc.view.getCanvasSize().height;//获得设备分辨率的高度
  • cc.director.setDisplayStats(true);//显示帧数信息

九、音频控制

cc.audioEngine.playMusic(this.BGAudio,true);//播放音乐(true循环)
cc.audioEngine.stopMusic()//停止播放
cc.audioEngine.playEffect(this.ClickAudio,false);//播放音效(false代表只播放一次)
cc.audioEngine.stopEffect(音效变量名);//停止指定音效(需要先把音效赋值给变量)
cc.audioEngine.AllEffects();//停止所有音效
cc.audioEngine.setMusicVolume(参数); //设置背景音乐的音量(范围是0到1)
cc.audioEngine.setEffectsVolume(参数); //设置音效的音量(范围是0到1)

十、设备判断

  • cc.sys.isNative //是否是本地
  • cc.sys.isBrowser //是否是网页
  • cc.sys.isMobile //是否是移动系统
  • cc.sys.platform //正在运行的平台
  • cc.sys.language //当前运行系统的语言
  • cc.sys.os //当前正在运行的系统
  • cc.sys.OS_IOS //是否是IOS系统
  • cc.sys.OS_ANDROID //是否是android系统
  • cc.sys.OS_WINDOWS //是否是windows系统
  • cc.sys.openURL(‘Http://www.baidu.com'); //打开网页

十一、监听和发射事件

  • this.node.pauseSystemEvents(true);//暂停节点系统事件
  • this.node.resumeSystemEvents(true);//恢复节点系统事件
  • this.node.targetOff(this);//移除所有注册事件

1、触摸监听

开始'touchstart',
移动'touchmove',
结束'touchend',
取消'touchcancel'

  • var pos = event.getLocation();//获取触摸点的坐标(包含X和Y)
  • var x = event.getLocationX();//获取触摸点的X坐标
  • var y = event.getLocationY();//获取触摸点的Y坐标
  • var a = event.getID();//获取触点的ID

2、鼠标监听

鼠标按下'mousedown',
移入节点'mouseenter',
节点中移动'mousemove',
移出节点'mouseleave,
‘松开鼠标'mouseup'

  • event.getScrollY();//获取滚轮滚动的 Y 轴距离,只有滚动时才有效
  • event.getLocation();//获取鼠标位置对象,对象包含 x 和 y 属性

3、输入框监听

获得焦点'editing-did-began',
文字变化'text-changed',
失去焦点'editing-did-ended',
按下回车'editing-return'

4,属性变化监听

位置'position-changed',
宽高 ‘size-changed',
旋转'rotation-changed',
缩放'scale-changed'

5、ScrollView控件监听

滚动中'scrolling',
停止滚动'scroll-ended'

6、用户自定义事件

监听: this.node.on(“自定义事件名称”, function(target) , this);

  • this.node.on(‘事件名',function,this);//注册监听
  • this.node.emit(‘事件名');//发送监听广播
  • this.node.off(‘事件名',function,this);//关闭监听

自派送: emit(“事件名称”, [detail]); 只有自己能够收到

onLoad: function () {
// 接收者
// 事件类型,是你自定义的字符串;
// 回掉函数: function(e) {} e--> cc.Event.EventCustom的实例
this.node.on("pkg_event", function (e) {
console.log("pkg_event", e);
}, this);
// 派发者,只能传递给自己,不会向上传递
this.node.emit("pkg_event", { name: "hanbao" });
},

冒泡派送: dispatchEvent(new cc.Event.EventCustom(“name”, 是否冒泡传递));

onLoad: function () {
// 接收者
// 事件类型,是你自定义的字符串;
// 回掉函数: function(e) {} e--> cc.Event.EventCustom的实例
this.node.on("pkg_event", function (e) {
console.log("pkg_event", e.detail);
}, this);
},
start: function () {
this.node.emit("pkg_event", { name: "hanbao" });  //这里会派发一次给自己
// //这里派发给全局 发给这个体系;
// true/false, true向上传递, false不向向上传递
var e = new cc.Event.EventCustom("pkg_event", true);
e.detail = { name: "haobao" };
this.node.dispatchEvent(e);
},

补充:

  • cc.director.pause();//暂停
  • cc.director.resume();//继续
  • cc.director.end();//退出整个应用
  • node.getLocalZOrder();//层级获取
  • node.setLocalZOrder(1);//层级改变
  • cc.find(‘canvas/map' + num)//读取带变量的路径

以上就是整理CocosCreator常用知识点的详细内容,更多关于CocosCreator知识点的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Cocos 常用 知识点