|
|
(同じ利用者による、間の11版が非表示) |
行1: |
行1: |
− | ==サンプルコード==
| + | [[Cocos2dx/AndroidNative連携/メソッド呼出]] |
− | -Classes/NativeLauncher.h
| + | |
− | #ifndef Helloworld_NativeLauncher_h
| + | |
− | #define Helloworld_NativeLauncher_h
| + | |
− | class NativeLauncher
| + | |
− | {
| + | |
− | public:
| + | |
− | static void launchNative();
| + | |
− | };
| + | |
− | #endif
| + | |
| | | |
− | -NativeLauncher.cpp
| + | [[Cocos2dx/AndroidNative連携/引数&戻り値あり]] |
− | #include "NativeLauncher.h"
| + | |
− | #include <jni.h>
| + | |
− | #include "platform/android/jni/JniHelper.h"
| + | |
− | #define CLASS_NAME "org/cocos2dx/cpp/AppActivity"
| + | |
− | void NativeLauncher::launchNative()
| + | |
− | {
| + | |
− | cocos2d::JniMethodInfo t;
| + | |
− | if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "launchNative", "()V")) {
| + | |
− | t.env->CallStaticVoidMethod(t.classID, t.methodID);
| + | |
− | t.env->DeleteLocalRef(t.classID);
| + | |
− | }
| + | |
− | }
| + | |
| | | |
− | -Classes/HelloWorldScene.h
| + | [[Cocos2dx/AndroidNative連携/jni型]] |
− | void menuLogCallback(cocos2d::Ref* pSender);
| + | |
| | | |
− | -Classes/HelloWorldScene.cpp
| + | [[Cocos2dx/AndroidNative連携/コールバック]] |
− | #include "NativeLauncher.h"
| + | |
− | bool HelloWorld::init()
| + | |
− | {
| + | |
− | // 略
| + | |
− | auto logItem = MenuItemImage::create(
| + | |
− | "CloseNormal.png",
| + | |
− | "CloseSelected.png",
| + | |
− | CC_CALLBACK_1(HelloWorld::menuLogCallback, this));
| + | |
− | logItem->setPosition(Vec2(origin.x + visibleSize.width - logItem->getContentSize().width/2 ,
| + | |
− | origin.y + visibleSize.height - logItem->getContentSize().height/2));
| + | |
− | auto logmenu = Menu::create(logItem, NULL);
| + | |
− | logmenu->setPosition(Vec2::ZERO);
| + | |
− | this->addChild(logmenu, 1);
| + | |
− | // 略
| + | |
− | }
| + | |
− | void HelloWorld::menuLogCallback(Ref* pSender)
| + | |
− | {
| + | |
− | NativeLauncher::launchNative();
| + | |
− | }
| + | |
| | | |
− | -proj.android-studio/app/src/org/cocos2dx/cpp/AppActivity.java
| + | [[Cocos2dx/AndroidNative連携/コールバック配列]] |
− | public class AppActivity extends Cocos2dxActivity {
| + | |
− | public static void launchNative()
| + | |
− | {
| + | |
− | System.out.println("launchNative()");
| + | |
− | }
| + | |
− | }
| + | |
− | | + | |
− | -proj.android/jni/Android.mk
| + | |
− | LOCAL_SRC_FILES := hellocpp/main.cpp \
| + | |
− | ../../Classes/AppDelegate.cpp \
| + | |
− | ../../Classes/HelloWorldScene.cpp \
| + | |
− | ../../Classes/NativeLauncher.cpp
| + | |
− | NativeLauncher.cppを追加
| + | |
− | | + | |
− | -proj.android-studio/jni/Android.mk
| + | |
− | LOCAL_SRC_FILES := hellocpp/main.cpp \
| + | |
− | ../../Classes/AppDelegate.cpp \
| + | |
− | ../../Classes/HelloWorldScene.cpp \
| + | |
− | ../../Classes/NativeLauncher.cpp
| + | |
− | NativeLauncher.cppを追加
| + | |
− | | + | |
− | ==参考==
| + | |
− | https://rakuishi.com/archives/5817/
| + | |