How to synchronized 1 method in C++ ?

Hi all , sorry my english not good .

I have problem with method be call 2 time in a same time .
I think i need synchronized method to solve this problem , but i don’t find synchronize where :frowning: .

And if C*+ not have synchronize , anyone have idead to solve my problem :(.
I’m newbie in C*+ :frowning: .

1 Like

GVBox Production wrote:

Hi all , sorry my english not good .
>
I have problem with method be call 2 time in a same time .
I think i need synchronized method to solve this problem , but i don’t find synchronize where :frowning: .
>
And if C*+ not have synchronize , anyone have idead to solve my problem :(.
>
I’m newbie in C*+ :frowning: .

Hi,

What do you mean “in the same time”?
Do you mean “in the same frame?”

Regards!

Stefano Campodall’Orto wrote:

What do you mean “in the same time”?
Do you mean “in the same frame?”
I think he works with the threads and he wants to execute one void after another (or at the “same time”) but he doesn’t know how to control it. That’s why he need to synchronize those functions :slight_smile:

GVBox Production
Could you please tell us more about your tasks? What do you want to synchronize? Do you use threads?

No i mean at the same time :frowning: .

I create 1 game like Fruit ninja .

When i touch 1 fruit i will be run function beStreak() .

in function beStreak() i do

void Bird::beStreak() {
if(HP > 0) {
HP = 0;
// do something
}
}

but sometime beStreack() be call 2 time at the same time .
In java if i want to synchronize 1 method just write : public synchronized void abc()

abc() never be call 2 time at the same time . It’s will wait to done task in abc it’s do task abc 1 more time .

sr my english is bad :((

GVBox Production wrote:

No i mean at the same time :frowning: .
>
I create 1 game like Fruit ninja .
>
When i touch 1 fruit i will be run function beStreak() .
>
in function beStreak() i do
>
> void Bird::beStreak() {
> if(HP > 0) {
> HP = 0;
> // do something
> }
> }
>
but sometime beStreack() be call 2 time at the same time .
In java if i want to synchronize 1 method just write : public synchronized void abc()
>
abc() never be call 2 time at the same time . It’s will wait to done task in abc it’s do task abc 1 more time .
>
sr my english is bad :((

Could you explain what do you want do? :slight_smile: So we can suggest you the correct way! :slight_smile:
Do you use parallels process (threads)? If yes, why?

void Bird::beStreak() {
if(isAlive()) {
setAutoRotate(false);
HP = 0;
BIRD_STATUS = BIRD_STATUS_STEAKDED;
>
this~~>stopAllActions;
this~~>setRotation(0);
>
this~~>initWithSpriteFrameName.c_str);
>
// call feather here
>
CCMoveTo jumpAction = CCMoveTo::create,this~~>getPositionY + 70));
CCCallFunc downingFunc = CCCallFunc::create);
>
CCActionInterval
seq = CCSequence::create;
>
this~~>runAction;
}
}
>
void Bird::downing {
BIRD_STATUS = BIRD_STATUS_DOWNING;
downX = this~~>getPositionX;
>
this~~>stopAllActions;
this~~>setRotation;
>
float pDuration = GVMath::countDistance,0,this~~>getContentSize.height/2)/;
>
CCMoveTo
downAction = CCMoveTo::create, this~~>getContentSize().height/2 - 10));
CCCallFunc stunFunc = CCCallFunc::create);
>
CCActionInterval
seq = CCSequence::create(downAction,stunFunc,NULL);
this~~>runAction;
>
CCArray downAnimationArray = new CCArray;
CCString
nameFrame_1 = CCString::createWithFormat.c_str);
CCSpriteFrame spriteFrame_1 = CCSpriteFrameCache::sharedSpriteFrameCache~~>spriteFrameByName);
CCString nameFrame_2 = CCString::createWithFormat.c_str);
CCSpriteFrame
spriteFrame_2 = CCSpriteFrameCache::sharedSpriteFrameCache~~>spriteFrameByName);
downAnimationArray~~>addObject;
downAnimationArray~~>addObject;
>
CCAnimation
downAnimation = CCAnimation::createWithSpriteFrames;
this~~>runAction(CCAnimate::create(downAnimation));
}
>
void Bird::stun() {
BIRD_STATUS = BIRD_STATUS_STUNNING;
>
this~~>stopAllActions;
>
int randomWay = %2-1);
float height = 50 + downX/10;
float endX = this~~>getPositionX + randomWay*;
float endY =this>getContentSize.height;
float pDuration = GVMath::countDistanceEllipse,this~~>getPositionY,endX,endY,height);
>
CCJumpTo
jumpAction = CCJumpTo::create,height,1);
CCRotateBy rollAction = CCRotateBy::create;
CCCallFunc
releaseFunc = CCCallFunc::create);
>
CCActionInterval *seq = CCSequence::create;
>
this~~>runAction(CCRepeatForever::create(rollAction));
this~~>runAction;
>
this~~>initWithSpriteFrameName((nameBird+“choang1.png”).c_str());
}

beStreak() be call when i touch sprite Bird , i have problem is something my bird runAction beStreak() then it’s stop , not run downing() like sequense i create , i think my method beStreak() called 2 time : 1 time it run sequense , 1 time it stopAction :frowning: .

And i want to know how to pause game correctly ?

I must use :

CCDirector::sharedDirector()->stopAnimation();
CCDirector::sharedDirector()->pause();

and use CCMenu for create PauseMenu ? is that right ?

GVBox Production wrote:

And i want to know how to pause game correctly ?
>
I must use :
>
CCDirector::sharedDirector()->stopAnimation();
CCDirector::sharedDirector()->pause();
>
and use CCMenu for create PauseMenu ? is that right ?

I have generated a “little framework” where manage the sprites (multi-ratio), scenes and personal director (with sound HSS library etc).
Every scene have a update function, so on head of this function, I write

if(_pause) { …. }

and I call pause’s update. Every sprites are inside at my layer and the layers are updated inside the scene.
But this is my system, I think there are many modes to do this.

ah, and I don’t use CCDirector::sharedDirector()->stopAnimation(); or CCDirector::sharedDirector()->pause();.
This is used when the app goes in background, because stopAnimation, reduces frame rate and rendering.

Regards!

Stefano Campodall’Orto wrote:
>

I have generated a “little framework” where manage the sprites (multi-ratio), scenes and personal director (with sound HSS library etc).
Every scene have a update function, so on head of this function, I write
>
if(_pause) { …. }
>
and I call pause’s update. Every sprites are inside at my layer and the layers are updated inside the scene.
But this is my system, I think there are many modes to do this.
>
ah, and I don’t use CCDirector::sharedDirector()->stopAnimation(); or CCDirector::sharedDirector()>pause;.
This is used when the app goes in background, because stopAnimation, reduces frame rate and rendering.
>
Regards!
Where i can see update function of my scene and overide it ?
This is way i create 1 update method for my scene :
this
>schedule(schedule_selector(GameScene::update),0.001f);

Thanks for your reply !

and if i pause update this scene how can i create button to resume it ?

This button must be addChild in another scene ? like HUD ?

And how can i create HUD for my scene ?

This is my first project in cocos2d-x and C++ so i have many question :frowning: .

GVBox Production wrote:

and if i pause update this scene how can i create button to resume it ?

when the game is in pause state, you can show a Layer with inside a button resume and update only this layer.

>

This button must be addChild in another scene ? like HUD ?
>
And how can i create HUD for my scene ?
>
This is my first project in cocos2d-x and C++ so i have many question :frowning: .

ok!
Now I write a pause system that work for me and not for cocos2d-x in classic mode:

there is few layer…

class MySpritesLayer : public frw::res::manager {

…to draw the sprite games
};

there is a resume button…

class MyResumeButton : public frw::res::button {

…pressed resume game
void onPresed() { frw::message::manager::getInstance()->sendMessage(“GAME”, kMessageGameResume);

};

there is a pause menu

class MyPause : public frw::res::menuPopup {

inside create the resume button

MyPause() {

*resumeButton = new MyResumeButton;
push;
}
…destroy etc…
};

there is a scene:
class MyScene : public frw::core::scene, public frw::message::listener, {
virtual void initialize {
…create layers
*pause = false;

*layerSprites = new MySpritesLayer;
*layerPause = new MyPause();
*layerPause~~>hide; // hide the pause in first time
push;
push;
}

virtual void finalize {
… destroy everything
}
virtual void update {
if {
*layerPause->update; // update layer pause and button resume
return;
}

*layerSprites~~>update; // update sprites
}
virtual void onMessage {
switch {
// when arrived the resume message
case kMessageGameResume: {
*pause = false;
*layerPause~~>hide;
break;
}

// when arrived the pause message
case kMessageGamePause: {
*pause = true;
*layerPause~~>show;
break;
}
}

}

bool*pause;

};

however, I don’t use cocos2d-x in the classic mode, I have generate the framework because I want have the control for my layer updates (I am old school of game programming :))
So, I am not the correct person to explain cocos2dx in the classic mode :slight_smile:

Regards!