@coco875222760 写道:
环境
cocos creator 1.3.3
新建helloworld工程
在工程目录下执行 npm install protobufjs
然后修改 helloworld.js 如下:cc.Class({
extends: cc.Component,properties: { button1: { default: null, type: cc.Button }, temp_count: 0 }, set_label: function(txt) { cc.find('Label', this.button1.node).getComponent(cc.Label).string = txt; }, change_button_text: function() { var VIEW = this; this.temp_count = this.temp_count + 1; // VIEW.set_label( "a2a " + this.temp_count); cc.log("this is ... test"); // https://www.npmjs.com/package/protobufjs var file = './resources/proto/awesome.proto'; var file_url = cc.url.raw(file); cc.loader.load(file_url, function(err, tex) { cc.log('loaded = ', tex); // var proto2json = require('proto2json'); // proto2json.parse(tex, function(err, result) { // // cc.log("loeded,result=", result); // // /** // var translator = new protobuf(result); // // var obj = {}; // obj.awesomeField = "AwesomeString"; //// obj.testErr = "asdfasdf"; // // var buffer = translator.encode('awesomepackage.AwesomeMessage', obj); // // //again, this will *only* decode the protobuf message. you have to remove the riak header yourself // var obj2 = translator.decode('awesomepackage.AwesomeMessage', buffer); // cc.log("decoded = ", obj2); // // **/ // }); var protobuf = require('protobufjs'); cc.log("load protobuf=", protobuf); var pr = protobuf.parse(tex); cc.log("load pr =", pr); var AwesomeMessage = pr.root.lookup("awesomepackage.AwesomeMessage"); cc.log("AwesomeMessage = ", AwesomeMessage); // Create a new message var message = AwesomeMessage.create({ awesomeField: "AwesomeString", testErr: "asdfasdf" }); // Encode a message var buffer1 = AwesomeMessage.encode(message).finish(); // Decode a buffer var message = AwesomeMessage.decode(buffer1); VIEW.set_label(" field = " + message.awesomeField + " \n test = " + message.testErr); }); }, // use this for initialization onLoad: function() { var VIEW = this; VIEW.set_label("asdfasdfasdfasdf"); // this.button1.Label.textKey = "asdfasdf"; }, // called every frame update: function(dt) { },
});
保存执行, 浏览器OK, windows模拟器OK, 但在android平台上报错
01-10 19:53:43.213: D/cocos2d-x debug info(3913): this is ... test
01-10 19:53:43.237: D/cocos2d-x debug info(3913): loaded = // awesome.proto
01-10 19:53:43.237: D/cocos2d-x debug info(3913): package awesomepackage;
01-10 19:53:43.237: D/cocos2d-x debug info(3913): syntax = "proto3";
01-10 19:53:43.237: D/cocos2d-x debug info(3913):
01-10 19:53:43.237: D/cocos2d-x debug info(3913): message AwesomeMessage {
01-10 19:53:43.237: D/cocos2d-x debug info(3913): string awesome_field = 1; // becomes awesomeField
01-10 19:53:43.237: D/cocos2d-x debug info(3913): }
01-10 19:53:43.240: D/ScriptingCore.cpp(3913): ScriptingCore:: compileScript fail:buffer
01-10 19:53:44.916: D/cocos2d-x debug info(3913): this is ... test
01-10 19:53:44.935: D/cocos2d-x debug info(3913): loaded = // awesome.proto
01-10 19:53:44.935: D/cocos2d-x debug info(3913): package awesomepackage;
01-10 19:53:44.935: D/cocos2d-x debug info(3913): syntax = "proto3";
01-10 19:53:44.935: D/cocos2d-x debug info(3913):
01-10 19:53:44.935: D/cocos2d-x debug info(3913): message AwesomeMessage {
01-10 19:53:44.935: D/cocos2d-x debug info(3913): string awesome_field = 1; // becomes awesomeField
01-10 19:53:44.935: D/cocos2d-x debug info(3913): }
01-10 19:53:44.936: D/cocos2d-x debug info(3913): load protobuf= [object Object]
01-10 19:53:44.936: E/cocos js error:(3913): assets/src/project.dev.js line:7241 msg:TypeError: protobuf.parse is not a function跪求高手指点迷津
帖子: 1
参与者: 1