「Cocos2dx/property」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→サンプルコード) |
|||
| 行132: | 行132: | ||
A/libc: Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 17727 (Thread-6094) | A/libc: Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 17727 (Thread-6094) | ||
E/SurfaceComposerClient: update : Can't get IGlobalScreenshot | E/SurfaceComposerClient: update : Can't get IGlobalScreenshot | ||
| + | |||
| + | ==error: undefined reference to collect2: error: ld returned 1 exit statusなエラーが出る場合== | ||
| + | androidのコンパイルしようとしてる場合であれば、app/jni/Android.mkにcppファイルを追加してるか確認 | ||
2017年4月17日 (月) 15:33時点における版
目次
サンプルコード
SampleModule.h
#ifndef Helloworld_SampleModule_h
#define Helloworld_SampleModule_h
#include "cocos2d.h"
class SampleModule
{
public:
int level;
static SampleModule* create();
};
#endif
SampleModule.cpp
#include "SampleModule.h"
USING_NS_CC;
SampleModule* SampleModule::create()
{
return new SampleModule();
}
HelloWorldScene.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
SampleModule* sample = SampleModule::create();
// SampleModule* sample = new SampleModule();でもできる
sample->level = 101;
log("level=%d", sample->level);
CC_SAFE_DELETE(sample);
}
参考:https://teratail.com/questions/71179
サンプルコード(ゲッターセッターでpropertyにアクセスする場合)
SampleModule.h
#ifndef Helloworld_SampleModule_h
#define Helloworld_SampleModule_h
#include "cocos2d.h"
class SampleModule
{
public:
int level;
static SampleModule* create();
void setName(const char*);
const char* getName();
private:
const char* mName = "hoge_property"; // 通常property
};
#endif
SampleModule.cpp
#include "SampleModule.h"
USING_NS_CC;
SampleModule* SampleModule::create()
{
return new SampleModule();
}
void SampleModule::setName(const char* name)
{
mName = name;
}
const char* SampleModule::getName()
{
return mName;
}
HelloWorldScene.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
SampleModule* sample = SampleModule::create();
sample->setName("hoge_setget");
log("%s", sample->getName());
CC_SAFE_DELETE(sample);
}
サンプルコード(staticのセッターゲッターでstatic propertyにアクセス)
static変数の宣言はcpp側でないとエラーになった。
-SampleModuleStatic.h
#ifndef Helloworld_SampleModuleStatic_h
#define Helloworld_SampleModuleStatic_h
#include "cocos2d.h"
class SampleModuleStatic
{
public:
static void setName(const char*);
static const char* getName();
};
#endif
-SampleModuleStatic.cpp
#include "SampleModuleStatic.h"
USING_NS_CC;
static const char* mNameStatic = "hoge_property_static";
void SampleModuleStatic::setName(const char* name)
{
mNameStatic = name;
}
const char* SampleModuleStatic::getName()
{
return mNameStatic;
}
-HelloWorldScene.cpp
bool HelloWorld::init()
{
SampleModuleStatic::setName("hoge_setget_static");
log("%s", SampleModuleStatic::getName()); // hoge_setget
// 略
}
存在しないプロパティを使った場合のエラーログ
A/libc: Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 17727 (Thread-6094) E/SurfaceComposerClient: update : Can't get IGlobalScreenshot
error: undefined reference to collect2: error: ld returned 1 exit statusなエラーが出る場合
androidのコンパイルしようとしてる場合であれば、app/jni/Android.mkにcppファイルを追加してるか確認
