游戏开发—关于preloadScene预加载在场景切换界面的实用逻辑+动态加载资源

游戏开发—关于preloadScene预加载在场景切换界面的实用逻辑
游戏开发—关于关于几个动态加载的使用

引擎:CocosCreater。 语言: JS

一.提前将资源缓存到内存中一直机制,就像cocos2d一样我们经常提前将图片或图集资源提前加载到内存中中这样实用效率比较高,缺点时消耗内存。
下面的代码就是creater中场景切换的一个案例供其参考:
大是大非

cc.Class({
    extends: cc.Component,
    properties: {
        _completedCount:0,
        _totalCount:1,
        _currentCount:0,
        _bCompleted:false,

        progressBarLabel:{
            default:null,
            type:cc.Label
        },

        progressBar:{
            default:null,
            type:cc.ProgressBar
        },
    },

    onLoad() {
        this.progressBar.node.active = false;
        let btn = this.node.getComponent(cc.Button);
        btn.node.on('click',this.onTouchBg,this); 
    },

    onTouchBg() {
        this.progressBar.node.active = true;
        let onLoaded = function(){
            console.log('load scene over...');
        };
        let item;
        let onProgress = function(_completedCount,_totalCount,item){
            this.progressBarLabel.string = Math.floor(_completedCount/_totalCount*100) + '%';
            let progress = _completedCount/_totalCount;
            this.progressBar.progress =  progress;
            if(progress == 1){
                cc.director.loadScene("game");
            }
        }.bind(this);

        cc.director.preloadScene('game',onProgress,this._completedCount,this._totalCount,item,onLoaded);
    },
});

下面为几个游戏中常用的动态加载资源比如:声音+json文件+粒子等
想必大家都知到这些简单的东西,但对于动态加载新手往往存在一个问题,JS中关于this的指向问题;
也就是下面代码中 var Pthis = this;
大家可以去百度理解js中this的指向;理解后在搜索一下(JS中this关键字改变指向的三种方法(apply、call、bind))学习一下也就明白了

//音效
cc.loader.loadRes('music/boss/ray/flashSkill', cc.AudioClip, function (err, clip) {
    let flashSkill_ID = cc.audioEngine.playEffect(clip, false);
    cc.audioEngine.setVolume(flashSkill_ID, 1);
});
//json
this.config_heros = null;
var Pthis = this;
cc.loader.loadRes('gameconfig/heros', function (err, jsonAsset) { Pthis.config_heros = jsonAsset.json.heros;});
//粒子
this.pParnode = null;
var self = this;
cc.loader.loadRes("particle/pingdao", cc.ParticleAsset, function(err, Asset){
    if (err) { return;}
    self.pParnode = new cc.Node();
    self.pParnode.addComponent(cc.ParticleSystem);
    self.pParnode.getComponent(cc.ParticleSystem).file = Asset
} ),
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值