Cocos2d-x调用Java最终实例代码
来源:互联网 发布:mysql设置默认值 编辑:程序博客网 时间:2024/04/28 07:35
HelloWorldScene.cpp
#include "HelloWorldScene.h"#include "SimpleAudioEngine.h"#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include <jni.h>#include "platform/android/jni/JniHelper.h"#include <android/log.h>#endifusing namespace cocos2d;using namespace CocosDenshion;CCScene* HelloWorld::scene(){ // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) ); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34); // ask director the window size CCSize size = CCDirector::sharedDirector()->getWinSize(); // position the label on the center of the screen pLabel->setPosition( ccp(size.width / 2, size.height - 20) ); // add the label as a child to this layer this->addChild(pLabel, 1); // add "HelloWorld" splash screen" CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // position the sprite on the center of the screen pSprite->setPosition( ccp(size.width/2, size.height/2) ); // add the sprite as a child to this layer this->addChild(pSprite, 0); // JNI call test#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)//////////////////////////////////////////////////////////////////// #1 : 静态,无参数,无返回值// #2 : 静态,有参数,无返回值// #3 : 静态,有参数,有返回值// #4 : 非静态,无参数,无返回值// #5 : 非静态,有参数,无返回值// #6 : 非静态,有参数,有返回值////////////////////////////////////////////////////////////////// JniMethodInfo minfo; // #1if (JniHelper::getStaticMethodInfo(minfo,"com/chen/FunctionTester","TF_S_NP_NR", "()V")){ minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID); } // #2 if(JniHelper::getStaticMethodInfo(minfo, "com/chen/FunctionTester","TF_S_HP_NR","(I)V")) { minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID,5); } // #3 if (JniHelper::getStaticMethodInfo(minfo, "com/chen/FunctionTester","TF_S_HP_HR","(I)I")){jint __result;__result = minfo.env->CallStaticIntMethod(minfo.classID, minfo.methodID, 5);}// 非静态方法的调用需要从一个静态方法中获得非静态方法所属的对象。也就是说,调用了一// 个返回值类型为java.lang.Object的静态方法jobject jobj;// #4if (JniHelper::getStaticMethodInfo(minfo, "com/chen/FunctionTester", "getInstance", "()Ljava/lang/Object;")){jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);if (JniHelper::getMethodInfo(minfo, "com/chen/FunctionTester","TF_US_NP_NR", "()V")){ minfo.env->CallVoidMethod(jobj, minfo.methodID);}} #endif return true;}void HelloWorld::menuCloseCallback(CCObject* pSender){ CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0);#endif}
FunctionTester.java
package com.chen;public class FunctionTester {// 在调用非静态方法的时候,需要获得方法属于哪一个对象,所以必须有一个静态的实例。// 而且,这个实例没有办法直接访问,所以必须定义一个静态方法来获得这个实例// 这个实例必须以Object类型进行输出public static FunctionTester tester = null;public static Object getInstance(){if (tester == null){tester = new FunctionTester();}return tester;}/////////////////////////////////////////////////////////////////////////// #1 : 静态,无参数,无返回值// #2 : 静态,有参数,无返回值// #3 : 静态,有参数,有返回值// #4 : 非静态,无参数,无返回值// #5 : 非静态,有参数,无返回值// #6 : 非静态,有参数,有返回值/////////////////////////////////////////////////////////////////////////// #1public static void TF_S_NP_NR(){System.out.println("CALLED: TF_S_NP_NR"); }// #2public static void TF_S_HP_NR(int _para){System.out.println("CALLED: TF_S_HP_NR, PARA: " + _para);}// #3public static int TF_S_HP_HR(int _para){System.out.println("CALLED: TF_S_HP_HR, PARA: " + _para);return 0;}// #4public void TF_US_NP_NR(){System.out.println("CALLED: TF_US_NP_NR");}// #5public void TF_US_HP_NR(int _para){System.out.println("CALLED: TF_US_HP_NR, PARA: " + _para);}// #6public int TF_US_HP_HR(int _para){System.out.println("CALLED: TF_US_HP_HR, PARA: " + _para);return 0;}}
- Cocos2d-x调用Java最终实例代码
- Cocos2d-x调用Java最终实例代码
- Jni调用,cocos2d.x调用java代码
- Cocos2d-x游戏开发之Cocos2dx通过JNI调用Android的Java代码(webView实例)
- Cocos2d-x调用Java
- Android游戏开发cocos2d-x调用Java层代码
- Cocos2d-x中通过JNI进行C++调用Java代码
- cocos2d-x 使用Jin调用java层代码的问题
- Cocos2d-x中通过JNI进行C++调用Java代码
- Cocos2d-x中通过JNI进行C++调用Java代码
- Cocos2d-x中通过JNI进行C++调用Java代码
- Cocos2d-x中通过JNI进行C++调用Java代码
- Cocos2d-x利用jni调用java层代码
- Cocos2d-x利用jni调用java层代码
- Cocos2d-x中通过JNI进行C++调用Java代码
- Cocos2d-x利用jni调用java层代码
- Cocos2d-x中通过JNI进行C++调用Java代码
- Cocos2d-x利用jni调用java层代码
- jQuery中事情的动态绑定
- 通过Wifi 实现语音对讲的资料总结
- 前向声明(Forward declaration)
- java中集成CKEditor和CKFinder
- 经常用的开发工具
- Cocos2d-x调用Java最终实例代码
- 聊天机器人
- import-module的注意事项与NDK_MODULE_PATH的配置
- Loser应该知道的6个残酷人生事实 (转载)
- 让vim在远程终端(Xshell4/SecureCRT)下得到像gui一样的colorscheme显示效果
- 扩展 sp_helpindex, 增加 INCLUDE 和筛选索引的筛选条件
- ORACLE 死锁解决办法
- HDU 2520 我是菜鸟,我怕谁
- Oracle With Clause和优化场景