关闭

cocos2d-x js与Java互调

1673人阅读 评论(0) 收藏 举报
分类:

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.parse 
var s={"id":2}; var params = JSON.stringify(s);
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4189次
    • 积分:92
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论