亲宝软件园·资讯

展开

CocosCreator知识点 整理CocosCreator常用知识点

代码猴儿 人气:6
想了解整理CocosCreator常用知识点的相关内容吗,代码猴儿在本文为您仔细讲解CocosCreator知识点的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Cocos,常用,cocos,知识点,下面大家一起来学习吧。

一、场景加载

二、查找节点

1,节点查找

2,节点其他操作

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

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

三、节点属性设置

常驻节点

四、节点动作

五、计时器

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);
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
}

八、分辨率

获得设备分辨率

九、音频控制

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)

十、设备判断

十一、监听和发射事件

1、触摸监听

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

2、鼠标监听

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

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);

自派送: 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);  
    },

补充:

加载全部内容

相关教程
猜你喜欢
用户评论