wxDownloader问题

在cocos creator 1.9.1版本中,微信小游戏编译后的res文件上传到阿里云了,删除了本地res,在启动小游戏的时候出现很长一段时间黑屏,由于资源稍微有点大,在wxDownloader文件里面wx.downloadFile的时候没有一个能处理返回值DownloadTask的异步方法,无法接收到下载进度,请问有更好的解决方法吗?

1赞

是否监听pipe能得到加载的百分比?

如果你的场景过大,第一次加载耗时过长,就会产生黑屏,你可能需要将部分资源放到resources下面,再做动态加载进度条加载进来。

我使用了这种方式临时解决这个问题,希望对大家有所帮助

let downloadTask = wx.downloadFile({
    url: remoteUrl,
    success: function (res) {
        if (res.statusCode === 404) {
            cc.warn("Download file failed: " + remoteUrl);
            callback({
                status: 0,
                errorMessage: res && res.errMsg ? res.errMsg : "Download file failed: " + remoteUrl
            });
        }
        else if (res.tempFilePath) {
            // http reading is not cached
            var localPath = wx.env.USER_DATA_PATH + '/' + relatUrl;
            // check and mkdir remote folder has exists
            ensureDirFor(localPath, function () {
                // Save to local path
                wx.saveFile({
                    tempFilePath: res.tempFilePath,
                    filePath: localPath,
                    success: function (res) {
                        // cc.log('save:' + localPath);
                        item.url = res.savedFilePath;
                        if (item.type && non_text_format.indexOf(item.type) !== -1) {
                            nextPipe(item, callback);
                        }
                        else {
                            readText(item, callback);
                        }
                    },
                    fail: function (res) {
                        // Failed to save file, then just use remote url
                        callback(null, null);
                    }
                });
            });
        }
    },
downloader.emit( "download", downloadTask );

在全局做了一个downloader全局变量,用来发送进度

window.downloader = {
    m_cmd: null,
    m_data: null,
    m_callback: null,
    on( cmd, callback ) {
        this.m_cmd = cmd;
        this.m_callback = callback;
    },
    emit( cmd, data ) {
        this.m_data = data;
        if( cmd === this.m_cmd ) {
            this.m_callback( data )
        }
    },
};

简单点就是加一个启动页,里面就一句加载下一个场景的代码,要进度的话是 cc.loader.onProgress回调

大佬,看的不是很懂,这是要写在哪里,要怎么使用呢,可以给个demo 吗?

就想知道,这个localPath怎么来