bearcatjs / bearcat

powered by enhanced javaScript objects
http://bearcatjs.org/
Other
591 stars 110 forks source link

cocos2d-js环境,导入bean失败! #150

Closed zj8487 closed 9 years ago

zj8487 commented 9 years ago

测试环境基于bearcat-cocos2d-js-example。

1,npm install bluebird --save,并修改client.js如下:

require('./bearcat-bootstrap.js');
var Promise = require('bluebird');
var bearcat = require('bearcat'); // 依赖bearcat库
window.bearcat = bearcat; // using browserify to resolve npm modules
window.Promise = Promise; 

cc.game.onStart = function() {
    cc.view.adjustViewPort(true);
    cc.view.setDesignResolutionSize(800, 450, cc.ResolutionPolicy.SHOW_ALL);
    cc.view.resizeWithBrowserSize(true);
    var self = this;
    //load resources
    bearcat.createApp();
    bearcat.use(['helloWorldScene']);
    bearcat.start(function() {
        var resourceUtil = bearcat.getBean('resourceUtil');
        var g_resources = resourceUtil.getResources();
        cc.LoaderScene.preload(g_resources, function() {
            var helloWorldScene = bearcat.getBean('helloWorldScene');
            cc.director.runScene(helloWorldScene.get());
        }, self);
    });

};

2,在bearcat-cocos2d-js-example/app-client目录中添加个testBean.js 如下:

"user strict";

function TestBean () {
    this.$id = "testBean";
    this.$init = "init";
}

TestBean.prototype.init = function () {
}

TestBean.prototype.logonServer = Promise.coroutine(function* () {
    return;
});

bearcat.module(TestBean, typeof module !== 'undefined' ? module : {});

3,grunt命令,Promise.coroutine 这里报错:undefine is not a function.

分析应该是bearcat扫描的时候,var Promise = require('bluebird');不生效
fantasyni commented 9 years ago

不是的,这个第三方库,不能这么使用的,需要用一个类似 requireUtil 这个代理,通过注入这个,来拿到 Promise ,具体可以参考 bearcat-browserify-jquery

zj8487 commented 9 years ago

即使使用requireUtil的方式,还是不支持我上面的函数定义方式啊

fantasyni commented 9 years ago

恩,因为你这个是加载文件直接运行的,是不支持

fantasyni commented 9 years ago

不过这个 Promise 是全局的,可以向 cocos2d-js 这样,事先加载个 umd 模式的 Promise 到 global 应该是可以的,bearcat 是不处理这个的,但是可以自己配置

zj8487 commented 9 years ago

你的意思是放到jslist里面?

fantasyni commented 9 years ago

就像bearcat一样啊,require 进来 然后 window.bearcat = bearcat 即可。。。