「Cocos2dx/AndroidNative連携/メソッド呼出」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==サンプルコード== -Classes/NativeLauncher.h #ifndef Helloworld_NativeLauncher_h #define Helloworld_NativeLauncher_h class NativeLauncher { public: stati...」) |
細 (Admin がページ「Cocos2dx/AndroidNative実行/メソッド呼出」を「Cocos2dx/AndroidNative連携/メソッド呼出」に、リダイレクトを残さずに移動しました) |
||
(同じ利用者による、間の6版が非表示) | |||
行25: | 行25: | ||
-Classes/HelloWorldScene.h | -Classes/HelloWorldScene.h | ||
− | void menuLogCallback(cocos2d::Ref* pSender); | + | |
+ | class HelloWorld : public cocos2d::Layer | ||
+ | { | ||
+ | public: | ||
+ | static cocos2d::Scene* createScene(); | ||
+ | virtual bool init(); | ||
+ | // a selector callback | ||
+ | void menuCloseCallback(cocos2d::Ref* pSender); | ||
+ | void menuLogCallback(cocos2d::Ref* pSender); | ||
+ | // implement the "static create()" method manually | ||
+ | CREATE_FUNC(HelloWorld); | ||
+ | }; | ||
-Classes/HelloWorldScene.cpp | -Classes/HelloWorldScene.cpp | ||
行63: | 行74: | ||
NativeLauncher.cppを追加 | NativeLauncher.cppを追加 | ||
− | -proj.android-studio/jni/Android.mk | + | -proj.android-studio/app/jni/Android.mk |
LOCAL_SRC_FILES := hellocpp/main.cpp \ | LOCAL_SRC_FILES := hellocpp/main.cpp \ | ||
− | ../../Classes/AppDelegate.cpp \ | + | ../../../Classes/AppDelegate.cpp \ |
− | ../../Classes/HelloWorldScene.cpp \ | + | ../../../Classes/HelloWorldScene.cpp \ |
− | ../../Classes/NativeLauncher.cpp | + | ../../../Classes/NativeLauncher.cpp |
NativeLauncher.cppを追加 | NativeLauncher.cppを追加 | ||
+ | |||
+ | ==実行順序== | ||
+ | #AppDelegate.h | ||
+ | #AppDelegate.cpp | ||
+ | #HelloWorldScene.h | ||
+ | #HelloWorldScene.cpp | ||
+ | #NativeLauncher.h | ||
+ | #NativeLauncher.cpp | ||
==参考== | ==参考== | ||
https://rakuishi.com/archives/5817/ | https://rakuishi.com/archives/5817/ | ||
+ | |||
+ | ==cocos error: undefined reference toエラー== | ||
+ | Android.mkにファイルを追加した確認する | ||
+ | |||
+ | ==jni関数公式== | ||
+ | https://docs.oracle.com/javase/jp/8/docs/technotes/guides/jni/spec/functions.html |
2017年4月11日 (火) 10:09時点における最新版
サンプルコード
-Classes/NativeLauncher.h
#ifndef Helloworld_NativeLauncher_h #define Helloworld_NativeLauncher_h class NativeLauncher { public: static void launchNative(); }; #endif
-Classes/NativeLauncher.cpp
#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
class HelloWorld : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); // a selector callback void menuCloseCallback(cocos2d::Ref* pSender); void menuLogCallback(cocos2d::Ref* pSender); // implement the "static create()" method manually CREATE_FUNC(HelloWorld); };
-Classes/HelloWorldScene.cpp
#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
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/app/jni/Android.mk
LOCAL_SRC_FILES := hellocpp/main.cpp \ ../../../Classes/AppDelegate.cpp \ ../../../Classes/HelloWorldScene.cpp \ ../../../Classes/NativeLauncher.cpp
NativeLauncher.cppを追加
実行順序
- AppDelegate.h
- AppDelegate.cpp
- HelloWorldScene.h
- HelloWorldScene.cpp
- NativeLauncher.h
- NativeLauncher.cpp
参考
https://rakuishi.com/archives/5817/
cocos error: undefined reference toエラー
Android.mkにファイルを追加した確認する
jni関数公式
https://docs.oracle.com/javase/jp/8/docs/technotes/guides/jni/spec/functions.html