Hello everyone i want to call member function in non member function in Own class without object in C++?
for example in HelloWorldScene.cpp
void non_MemberFunc()
{
MemberFunc();
//How Run MemberFunc without object?
}
void HelloWorldScene::MemberFunc()
{
//do somthing
}
bool HelloWorldScene::init()
{
//code
}
mfeu
December 5, 2015, 10:03pm
2
You can define your function as static…
e.g.: in your HelloWorldScene.h you define:
static void non_MemberFunc();
Obviously you have to implement the function in HelloWorldScene.cpp:
void HelloWorld::non_MemberFunc() {
CCLOG("i am a fancy static function");
}
bool HelloWorldScene::init() {
HelloWorld::non_MemberFunc();
}
Hope this helps
iQD
December 5, 2015, 10:13pm
3
He wants to do the opposite.
You can’t do that, as a member function always needs an object.
1 Like
mfeu
December 5, 2015, 10:22pm
4
Oh, sorry - I misread what he wanted.
@iQD you are right, this isn’t possible.
iQD
December 5, 2015, 10:27pm
5
I don’t even get, why creating an object would be a problem for @maleking .
Why you don’t want/are able to create an object?
Thank you for responding
I use socket programming with JNI in my multiplayer game
in my game ,Server send a string to clients and in Java side clients get string and call C++ non static function but My problem is that in non static function i can’t Update UI
for example i have Deck class for create multiple card but when i create object in non-static function and Java Side call this function the sprite getting Black (i dont know why this happens)
is there any solution for my problem?
I think the problem is that your JNI is calling your method from another thread, try running it in cocos main thread instead.
Whats your mean about "JNI calling your method from another thread "?
Can you tell me more detail ?
I meant the method you are calling in Java, on what thread is it called ? is it on main thread ?
i search for this but still i have no idea that what is Main Thread in cocos2d-x and How i call this ?
HelloWorldScene.cpp
JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_Receive(JNIEnv* env,
jobject thiz, jstring jstr) {
const char* str = env->GetStringUTFChars(jstr, NULL);//get String from Server
if(str=="play")//if Server send "play" string run a CCaction on sprite
{
Deck *deck=new Deck(thisclass_layer);
deck->play();
}
}
void HelloWorldScene::MemberFunc()
{
//do somthing
}
bool HelloWorldScene::init()
{
//code
}
Deck.h
Deck::Deck(cocos2d::Layer *layer) {
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
centerX = visibleSize.width / 2;
centerY = visibleSize.height / 2;
height = visibleSize.height + origin.y;
width = visibleSize.width;
p0 = Vec2(centerX, 10);
p1 = Vec2(10, centerY);
p2 = Vec2(centerX, height);
p3 = Vec2(width, centerY);
std::string str;
cocos2d::Sprite* mySprite;
auto spritecache = SpriteFrameCache::getInstance();
spritecache->addSpriteFramesWithFile("asset.plist");
for (int i = 0; i < 52; i++) {
str = to_string(i) + ".png";
mySprite = Sprite::createWithSpriteFrameName(str);
mySprite->setPosition(Vec2(150,150));
mySprite->setTag(cards[i]);
if (i % 4 == 0)
mySprite->setName("p0");
if (i % 4 == 1)
mySprite->setName("p1");
if (i % 4 == 2)
mySprite->setName("p2");
if (i % 4 == 3)
mySprite->setName("p3");
mySprite->setAnchorPoint(Vec2(0.5, 0.5));
_mySprites.insert(i, mySprite);
layer->addChild(mySprite,52-i);
}
}
void Deck::Play() {
actionFinished();
}
void Deck::Next() {
if (counter < 52)
counter += 1;
switch (counter%4) {
case 0:
curPos = p0;
curRotate=0;
break;
case 1:
curPos = p1;
curRotate=270;
break;
case 2:
curPos = p2;
curRotate=180;
break;
case 3:
curPos = p3;
curRotate=90;
break;
default:
// Code
break;
}
}
void Deck::actionFinished() {
if(counter==51)
return;
auto jump = JumpBy::create(0.5, Vec2(0, 0), 100, 1);
RotateTo * rotate = RotateTo::create(0.2f, curRotate);
auto moveBy = MoveTo::create(0.5, curPos);
auto callbackJump = CallFunc::create([]() {
log("Jumped!");
});
auto callbackRotate = CallFunc::create([]() {
log("Rotated!");
});
CCCallFunc* startNext = CCCallFunc::create(this,
callfunc_selector(Deck::actionFinished));
auto actCallback = CallFunc::create(this,
callfunc_selector(Deck::actionFinished));
auto nextCallback = CallFunc::create(this, callfunc_selector(Deck::Next));
auto seq = Sequence::create(jump, moveBy, rotate, nextCallback, actCallback,
nullptr);
_mySprites.at(counter)->runAction(seq);
CCLOG("action completed!");
}