1. cc.Director对象
游戏里面控制管理整个游戏全局对象,包括了场景切换等,为cc.Director对象;
导演对象全局只有一个cc.director,大写的为类, 小写的cc.director为全局的导演对象;
cc.director来获取导演对象实例;
游戏中各种管理对象都可以通过cc.director获取,比如物理引擎管理,Action管理, 碰撞检测管理等;
2. 常用接口
getWinSize: 适配后的逻辑大小;
getWinSizeInPixels: 获取窗口的像素大小;
getScene: 获取当前的逻辑场景,场景对象下面是Canvas;
setDisplayStats: 是否显示左下角FPS信息;
getCollisionManager: 获取碰撞检测管理对象;
getPhysicsManager :获取物理引擎管理对象;
loadScene(scene_name):加载场景,场景的名字,系统会加载对应的场景
preloadScene(scene_name):预加载场景
3. 资源加载策略
h5资源加载的过程:
(1)从服务器上下载来来资源,并把资源加载到内存中,所以你在做h5游戏,你要把你当前游戏中要用到的资源先加载下来,否者的话,你在运行的时候去加载就来不及了(h5卡住);
三种资源加载策略:
1>: h5的小游戏:采用全部提前绑定好所有的资源。编写预加载脚本preload.js,
将要加载的资源手动关联到第一个启动的场景上面;
2>: 添加等待界面,预加载下一个场景,然后再进行切换,提前关联好下一个场景要的资源;
cc.loader.onProgress = function ( completedCount, totalCount, item ){
console.log("completedCount:" + completedCount + ",totalCount:" + totalCount );
};
3> 嫌手动关联麻烦,在场景切换中加入过渡场景,代码来加载场景的资源:
cc.loader.loadResAll("textures", function (err, assets) {
});
代码加载资源会导致setting.js文件过大,一般尽量少在代码里面加载资源;
例子
home_scene.js
// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
wait: {
default: null,
type: cc.Node,
},
progress_lable: {
type: cc.Label,
default: null,
},
},
// LIFE-CYCLE CALLBACKS:
onLoad () {
this.wait.active = false;
this.progress_lable.string = "0%";
},
start () {
},
goto_road: function() {
//切换场景时如果直接切换过去,下一个场景会加载资源等待加载的时候会卡住一段时间
//我们在这里加上等待界面
this.wait.active = true;
//预加载
cc.director.preloadScene("roadmap_scene", function(completedCount, totalCount, item) {
console.log("completedCount:" + completedCount + ",totalCount:" + totalCount);
var per = Math.floor(completedCount * 100 / totalCount);
this.progress_lable.string = per + "%";
}.bind(this), function() {
cc.director.loadScene("roadmap_scene");
});
//end
},
// update (dt) {},
});
preload.js
// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
cc.Class({
extends: cc.Component,
properties: {
// foo: {
// // ATTRIBUTES:
// default: null, // The default value will be used only when the component attaching
// // to a node for the first time
// type: cc.SpriteFrame, // optional, default is typeof default
// serializable: true, // optional, default is true
// },
// bar: {
// get () {
// return this._bar;
// },
// set (value) {
// this._bar = value;
// }
// },
//预加载资源
img_array: {
default: [],
type: cc.SpriteFrame,
},
atlas_array: { //图集资源
default: [],
type: cc.SpriteAtlas,
},
sound_array: {
default: [],
url: cc.AudioClip,
},
prefab_array: { //预制体资源
default: [],
type: cc.Prefab,
},
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
start () {
},
// update (dt) {},
});
工程截图