cocos2d-x js与Java互调
frameworks\runtime-src\proj.android\src\org\cocos2dx\javascript的文件夹下新建JStest.java
package org.cocos2dx.javascript;
import java.util.logging.Logger;
public class JStest {
public static void hello(String msg){
Logger log = Logger.getLogger("result");
log.info(msg);
}
public static int sum(int a){
return a + 2;
}
}
因为cocos 封装好了,一层胶水层,直接调用就可以了
在JS中的调用
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/JStest", "hello", "(Ljava/lang/String;)V", "this is a message from js");
var result = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/JStest", "sum", "(I)I", 3);
cocos-js toast代码:转载的,原地址:点击打开链接
新建Toast.js
var ToastLayer = cc.Layer.extend({
contentLabel:null,
ctor:function (content) {
this._super();
var size = cc.winSize;
this.contentLabel = new cc.LabelTTF(content, "Arial", 23);
this.contentLabel.setColor(cc.color(255,0, 0));
this.contentLabel.setAnchorPoint(0,0);
this.contentLabel.setPosition(80, 100);
this.contentLabel.setDimensions(cc.size(860,70));
this.contentLabel.textAlign = cc.TEXT_ALIGNMENT_CENTER;
this.contentLabel.verticalAlign = cc.VERTICAL_TEXT_ALIGNMENT_CENTER;
this.addChild(this.contentLabel, 1);
this.setCascadeOpacityEnabled(true);
},
//设置Toast显示内容
setContent:function(content) {
this.contentLabel.setString(content);
},
//设置Toast显示时间,包括显示持续时间和淡出持续时间
show:function(delay, sec) {
var delayOut = delay || 1.5;
var timeOut = sec || 0.5;
this.setOpacity(255);
this.runAction(cc.sequence(cc.delayTime(delayOut), cc.fadeOut(timeOut)));
}
});
引用代码:
//新建Layer,添加到场景(Scene)中
var toastLayer = new ToastLayer("Welcome to Toast");
toastLayer.setOpacity(0); //初始设置Toast层不显示
this.addChild(toastLayer);
//调用Toast类
//设置Toast显示内容
toastLayer.setContent("显示内容");
//显示Toast
toastLayer.show();
cocos-js 进度条代码:
var to1 = cc.ProgressTo(0.01,100);
var progressCooling = new cc.ProgressTimer(cc.Sprite(res.progress_jpg));
progressCooling.setType(cc.ProgressTimer.TYPE_BAR);
// progressCooling.setPercentage(0); // 回复到0
progressCooling.x = cc.winSize.width / 2;
progressCooling.y = cc.winSize.height / 2 - 200;
progressCooling.setVisible(true);
progressCooling.runAction(cc.RepeatForever(to1));
this.addChild(progressCooling, 5);
cocos-js 创建json代码:
将对象转换为json字符串: JSON.stringify 将json字符串转为对象:JSON.parsevar s={"id":2}; var params = JSON.stringify(s);