How to pause a scheduled selector? - cocos2d-iphone

Is There is any way to pause a particular selector, like :
[self schedule:#selector(funcA:) interval:0.05];
is it possible to pause this scheduler and then resume.
Another question: is there any way to check whether a scheduler is running???

You can pause all of the schedulers using:
[[CCDirector sharedDirector] pause]
or:
[self pauseSchedulerAndActions];

You can use
[self unschedule:#selector(funA:)];

Possibly you can make a bool variable in your class that will identify if your object is scheduled or not. And in your funcA check the variable value. This solution will be more efficient then unscheduling and scheduling again.

Add the scheduler to a dummy CCNode:
[dummyNode schedule:#selector(funcA:) interval:0.05];
Then you can use:
[dummyNode pauseSchedulerAndActions];
It will only pause schedulers in that node, which is your scheduler.

Related

How can I stop a long for loop when the widget (QDialog) running it is closed without multithreading?

I have a quite lengthy foreach loop in a QDialog. It basically looks like this:
foreach (xxx, xxx) {
... doSomeStuff ...
QApplication::processEvents();
if (m_cancelMapLoading) {
break;
}
}
m_cancelMapLoading is set to true by clicking a "Cancel" button. The QApplication::processEvents(); makes this possible.
This works quite fine, but if the dialog is closed as long as that foreach loop still runs, it continues running. I tried to set m_cancelMapLoading to true in each function closing the dialog, but this does not help.
I also tried to test not only for m_cancelMapLoading being true, but also for isVisible(). This actually stops the dialog, but it re-opens it at once without the GUI elements in it.
Unfortunately, QtConcurrent::run etc. can't be used for the function, because the data structures that are manipulated by the foreach loop are not thread safe.
Is there a convenient way to solve this?
You can use a QTimer and Qt's parent-child structure to your advantage here. QTimer with a timeout value of zero has a special meaning in Qt
As a special case, a QTimer with a timeout of 0 will time out as soon
as all the events in the window system's event queue have been
processed. This can be used to do heavy work while providing a snappy
user interface:
So you could do something like
void Dialog::beginDoingStuff()
{
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(processData());
m_timer->start(0);
}
void Dialog::processData()
{
// Perform one cycle of your process here
}
This will perform the processData() function in the same thread as the rest of the dialog, and when the dialog is destroyed by being closed, the timer will be deleted (because it's parent is the dialog), meaning the processing will stop.
A good and quite easy way to unload your GUI from heavy processing is assigning it to another thread or QtConcurrent.
You could then either poll a "should-I-terminate-yet?" variable or terminate the thread manually when it is no longer needed.
I highly recommend a parallel processing since offers better control rather than doing a "DoEvents"-like queue emptying.
We actually managed to solve the problem by connecting the dialog's finished signal to the click slot of the cancel button. This actually stops the loop in all circumstances.
We also introduced starting the function by a QTimer (for a nicer implementation not blocking the function where it's started), but this does not stop the loop (perhaps because we don't destroy the dialog when it's closed).
Thanks for all help :-)

pause/ play a game in cocos2d android?

How to pause a game in cocos2d android? I searched many tutorials but i did not found anywhere.I tried below code, it is working when paused from game scene.But it not working while resuming the game from pause scene
in gameScene :
CCDirector.sharedDirector().onPause();
in pause Scene :
CCDirector.sharedDirector().onResume();
For pausing game scene I use:
CCDirector.sharedDirector().pause();
And for resuming:
CCDirector.sharedDirector().resume();
Try it with these functions. :)
Thanks for your reply. I used the same thing.But it wont stop schedulers and actions.
But finally i got the way we have to pause.
1.To pause we have to use pushScene() method as follows:
CCScene pauseScene = CCScene.node();
pauseScene.addChild(new PauseLayer);
CCDirector.sharedDirector().pushScene(pauseScene);
When you have done this, gamescene will get stored in the stack and now running scene will be pause scene.
2.To resume we have to use popScene() as follows:
CCDirector.sharedDirector().popScene();
3.If you are moving to LevelSelection form pause then along with above lines use these lines also:
CCDirector.sharedDirector().getRunningScene().removeAllChildren(true);
CCDirector.sharedDirector().replaceScene(levelSelectScene);
Declare a BOOL variable, then when you detect user pauses game, make this variable to NO and in your update method put this as a condition above to the code you want to pause on your game paused.
-(void)update:(ccTime)dt
{
if(isGameNotPaused)
{
// your code
}
}

how to change GUI while processing

I am new to Qt Programming, but I have basic on C++.
I want to update my GUI while it is processing, example:
while (....)
{
do some calculation...
if (condition fulfill)
change the color of label.
}
However, I realise that I failed to get the result I want (update the GUI while processing). The GUI will only update after the while loop.
Why is it so? Anyone can help?
In addition, I wish to "slower" the color change since the processing is too fast and I can't see the animation. Any idea to do it?
Thank you very much!
Clarification:
Actually I wish to update the GUI while I am processing...Meaning that, if I have 100 iteration, after each iteration I wish to update the GUI immediately.
Use a QTimer. This will allow you to control the speed of your animation and keep your UI responsive.
You have to place your processing code to another thread and update the gui, because like this GUI will be waiting for your process to end and will refresh after its end
read more here:
http://www.qtcentre.org/threads/41545-How-to-refresh-GUI-while-heavy-processing-is-ongoing
http://www.qtcentre.org/threads/32416-Update-GUI-from-another-thread
Forcing the Qt GUI to update
You don't necessarily need a thread.
Calling QApplication::processEvents() will process pending events, including any redraws you may have caused during your processing.
If you wish to animate the color to indicate that the system is currently working, you might want to use QApplication::setOverrideCursor to show a waitCursor, or a QProgressDialog instead.

cocos2d 1.0 beta transition between two scenes and the program just quit?

Hi am new to xcode and cocos2d development. I have a question about switch scenes. I have one class is called "GameScene" and has a schedule call "scheduleTarget" to add a target on the scene every 0.3 second. When I switch from GameScene to GameOverScene or ManuScene using scene transition method, the program just quit without any error message.
I tried to clean up the memory by release the objects and call dealloc and onExit.
I also tried to unschedule the call "ScheduleTarget". But nothing helps.
Anybody know what is my problem?
Thanks
I figure out the problem. I assign the dealloc the variable a value. :(
I am so happy this issue has been solved.

C++ Dialog box With timer

I couldn't find a simple tutorial on how to make a dialog box with decrementing timer. I don't need the timer to be accurate or actually reflect my program's inner timer.
Ended using SetTimer : http://msdn.microsoft.com/en-us/library/ms644906%28VS.85%29.aspx
Thanks!
This countdown timer tutorial doesn't help? Source code is included, and you probably can fit the code to "decrement" the timer, or show time info instead.