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;}}




原创粉丝点击