Cocos2d-x. How to add action on the sprite after the previous one was finished - action

I add action on the sprite.
auto moveBy = MoveBy::create(2, Vec2(moveX, moveY));
_Spr1->runAction(moveBy);
I want to add another action on touch, but I want that the second one started after the first one is finished.
And if I tap two times before first action stops, I want to create sequence with one old action and two new ones.

How about queue your following actions, wait the current actions finish, and then add them?
Check if animations are still running

auto moveBy1 = MoveBy::create(2, Vec2(moveX, moveY));
auto moveBy2 = MoveBy::create(2, Vec2(moveX, moveY));
auto *seq = Sequence::create(moveby1,moveby2, NULL);
_Spr1->runAction(seq);

Related

Qt : how to automate looped slider movement in the background?

What's the most straightforward approach to have a slider move in a loop at a given speed when a button is pressed? I'm guessing it involves forking a thread that sends the appropriate signals periodically to the slider. Is there an canonical approach example for doing this?
I would suggest to use QPropertyAnimation to do the job. just set the start value, the end value and the curve you want the value to change
QPropertyAnimation *animation = new QPropertyAnimation(slider,"sliderPosition");
//set the duration (how long the animation should run - will change value faster when shorter)
animation->setDuration(1000);
//set the start value - in this case check if value in range of the slider
animation->setStartValue(slider->minimum());
//same as start value
animation->setEndValue(slider->maximum());
//easingCurve defines if it goes straight or bouncing n stuff
animation->setEasingCurve(QEasingCurve::OutCubic);
// as coyote mentioned, you can loop the animation as well (credit him as well ;))
// -1 defines to run forever
animation->setLoopCount(loopCount)
animation->start();
This would just be a matter of updating the slider's position on a timer. So, create a timer and on each update, call QSlider::setValue.
When the value is at maximum, set it back to the minimum and continue.
QSlider* pSlider = new QSlider;
QButton * pButton = new QButton("Go");
QTimer* pTimer = nullptr; // C++ 11 nullptr
// On button click, start a timer
connect(pButton, &QButton::clicked(), [=](){
// exit if already running
if(pTimer)
return;
pTimer = new QTimer;
connect(pTimer, &QTimer::timeout, [=](){
if(pSlider->value()+1 > pSlider->maximum())
pSlider->setValue(pSlider->minimum());
else
pSlider->setValue(++pSlider->value());
});
pTimer->start(1000); // update every second
});
You can use QTimer. Minimal example:
QSlider *sl = new QSlider;
QTimer *ttt = new QTimer;
sl->setValue(0);
connect(ttt,&QTimer::timeout,[=]() {
sl->setValue(sl->value() + 5);
});
sl->show();
ttt->start(500);
I used here C++11 (CONFIG += c++11 to .pro file) and new syntax of signals and slots, but of course you can use old syntax if you want.
There's no canonical approach to my knowledge.
The timers are given in the other answers, but you can also use the animation framework, and adjust the speed by adjusting the duration of the animation.
You can set the loopcount to how many times you want the animation to run, for example 1000000 to make it run a long time.

c++ Qt, wait for second click

I am using Qt creator to make a simple drawing program in c++. I have a mouseevent, that should get the coordinates to the nearest point (dot) allready existing from the point where mouse was clicked, and put them to line starting point coordinates. Thats done. But now the event should wait for a second mouse click to get the nearest dot coordninates again and put them to end point of line. But instead it does not wait for the second input and puts the same first point for the line endpoint also.
How can I make mouseevent take two click inputs, not do everything right away? Is it even possible? Thank you in advance.
You cannot make one event require two clicks (excluding, of course, the double click event). You can, however, establish a state within your application, where the first click starts the state and the second completes it. Pseudo-code to manage this:
// in your constructor:
StartingClickPoint = INVALID; // indicate that we have not entered our special state
// in your mouse event handler:
if (StartingClickPoint == INVALID) {
// we're only now starting this state; we don't have enough information to complete it
StartingClickPoint = CurrentClickPoint;
return;
}
else {
// complete the state handling and arm for the next pair of clicks
EndingClickPoint = CurrentClickPoint;
DoSomethingWithTheClickPoints();
StartingClickPoint = INVALID; // reset for the next pair of clicks
}

Cocos2d v3 group actions like SpriteKit

Desired grouping behaviour
How would I group CCAction in order to execute them all at the same time while knowing when all of the actions in the group are complete?
Exact SpriteKit equivalent
+ (SKAction *)group:(NSArray *)actions
Use case
I want to use this in a board game.
I am moving my game elements around my board with CCActions. At the end of all of these animation I would like to a CCActionCallFunc as a callback to update game logic and permit user interaction once more.
Ideally I would like to add the CCActionGroup and the CCActionCallFuncin a CCActionSequence and have them execute sequentially.
Maybe you could try:
CCActionMoveTo *moveTo; // action to group
CCActionScaleBy *scale; // action to group
CCNode *node;
// Init the above actions...
CCActionSpawn *groupAction = [CCActionSpawn actionWithArray:#[moveTo, scale]];
CCActionSequence *sequence = [CCActionSequence actionWithArray:#[groupAction, [CCActionCallFunc actionWithTarget:self selector:#selector(allDone)]]];
// allDone is your method to run...
[_node runAction:sequence];
Since cocos2d doesn't employ any kind of multithreading, you can just fire the actions sequentially:
[node runAction:action1];
[node runAction:action2];
[node runAction:action3];
[node runAction:action4];
Just add a CCCallBlock to the longest running sequence that runs a block. If all actions run the same length, you would add the call block to the action4 sequence because that'll finish last.
In Sprite Kit there wouldn't be any guarantee which of action1 through action4 finishes last, in cocos2d there is: the last one to be run will also be the last to finish, assuming they all run for the same duration.
This is when they other actions will also have ended. Alternatively you can create a sequence for each group at the end of each they all run a CCCallBlock that increases a counter, and the last block where the count is equal to the number of grouped actions performs the actual "end of group actions" code.

How do I progressively load a widget in QT?

I have a custom widget which displays many items in rows:
void update(){ //this is a SLOT which is connected to a button click
QVBoxLayout *layout = this->layout();
if (layout == NULL){
layout = new QVBoxLayout;
this->setLayout(layout);
} else {
QLayout_clear(layout); //this is a function that I wrote that deletes all of the items from a layout
}
ArrayList *results = generateData(); //this generates the data that I load from
for (int i = 0; i < results->count; i++){
layout->addWidget(new subWidget(results->array[i]));
}
}
The problem is that there are about 900 items and a profile reveals that simply adding the child object to the layout takes 50% of the time (constructing takes the other 50%). Overall it takes about 3 seconds to load all of the items.
When I click on the button to load more data, the entire UI freezes for the 3 seconds and then all of the items appear together when everything is done. Is there a way to progressively load more items as they are being created?
The first trick is, as Pavel Zdenek said, to process only some of the results. You want to process as many together so that the overhead (of what we're going to do in the next step) is low, but you don't want to do anything that would make the system seem unresponsive. Based on extensive research, Jakob Nielsen says that "0.1 seconds is about the limit for having the user feel that the system is reacting instantaneously", so as a rough estimate you should cut your work into roughly 0.05 second chunks (leaving another 0.05 seconds for the system to actually react to the user's interactions).
The second trick is to use a QTimer with a timeout of 0. As the QTimer documentation says:
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 that means that a timer with a timeout of 0 will be executed next, unless there is something else in the event queue (for instance, a mouse click). Here's the code:
void update() {
i = 0; // warning, this is causes a bug, see below
updateChunk();
}
void updateChunk() {
const int CHUNK_THRESHOLD = /* the number of things you can do before the user notices that you're doing something */;
for (; i < results->count() && i < CHUNK_THRESHOLD; i++) {
// add widget
}
// If there's more work to do, put it in the event queue.
if (i < results->count()) {
// This isn't true recursion, because this method will return before
// it is called again.
QTimer::singleShot(0, this, SLOT(updateChunk()));
}
}
Finally, test this a little bit because there's a gotcha: now the user can interact with the system in the "middle" of your loop. For instance, the user can click the update button while you're still processing results (which in the above example means that you would reset the index to 0 and reprocess the first elements of the array). So a more robust solution would be to use a list instead of an array and pop each element off the front of the list as you process it. Then whatever adds results would just append to the list.
#Adri is generally right, the twist is that the "another thread" must be the UI thread again. The point is to allow UI thread's event loop to keep spinning. The fast and dirty way is to put QCoreApplication::processEvents() in your for() cycle. Dirty because, as the doc says, it should be called "ocassionally". It might have some overhead even if there are no UI events, and you are messing Qt's performance optimization as to when and how often spin the loop. Slightly less dirty would be to call it only ocassionally, after chunks of result.
Cleaner and proper way is to create a private slot, which pops one result element (or chunk, to speed up), adds to the layout and increments index. Then it will recall itself until end of results. The gotcha is to define connect() with forced connection type Qt::QueuedConnection, so it will get deferred after already queued UI events (if any).
And because you run in only one thread, you don't need any locking over results.
Adding example per OP's request:
While #TomPanning solution is correct, it kind of hides the real solution behind QTimer which you don't need - you don't need any timing, you just need a specific non-timer behavior upon specific parameter value. This solution does the same thing, minus the QTimer layer. On the other hand, #TomPanning has a very good point about the plain ArrayList not being very good data storage, when interaction can happen in between.
something.h
signals: void subWidgetAdded();
private slots: void addNextWidget();
ArrayList* m_results;
int m_indexPriv;
something.cpp
connect(this,SIGNAL(subWidgetAdded()),
this,SLOT(addNextWidget(),
Qt::QueuedConnection);
void addWidget() {
// additional chunking logic here as you need
layout->addWidget(new subWidget(results->array[m_indexPriv++]));
if( m_indexPriv < results->count() ) {
emit subWidgetAdded(); // NOT a recursion :-)
}
}
void update() {
// ...
m_results = generateData();
m_indexPriv = 0;
addNextWidget(); // slots are normal instance methods, call for the first time
}

CCMoveBy behaviour

I'm getting stuck to implement some Cocos2D animations for my Tetris clone(that works perfectly, no logic bugs, i just want to perform some smooth animation when deleting rows).
The current code(no animation) just drops the block position, like this:
block.position = ccp(block.position.x, block.position.y - kBlockSize);
This happens in a for loop for, classic tetris programming. But when i try to animate, like this:
id move = [CCMoveBy actionWithDuration:0.5f position:(0, -kBlockSize)];
[block runAction:move];
Some blocks just moves down once, even tough the action may be called multiple times for the same block(when breaking more than one row for example)...
Why that happens ? I know it's a little bit confusing, but the point is that i'm doing the same stuff and getting different results...i could post more code to help clarify!
Thanks!
I'm quite sure actions are parallel operations so you could be calling a CCMoveBy action before a previous one is completed. Some alternatives I have used are...
Monitor for when the action completes by using a CCSequence finishing with a CCCallFunc action that sets a flag. Something like...
id myAction = [[CCSequence runWithActions:[CCMoveBy actionWithDuration:0.5f position:(0, -kBlockSize)], [CCCallFunc actionWithTarget:self selector:#selector(myFunc)], nil]
Roll your own solution using a velocity variable in a tick or update function where you can get a hold of delta time/# of ticks since the last update
Hope some of that helps.
Thank you guys, those answers help me a lot!
I've tried CCSequences before posting here, but without success.
The problem was the following:
Inside the CCSequence that deletes a row, i have 2 actions: the first one fades out the entire row of blocks(duration of x seconds), and the second one drops all the blocks above the row(duration of y seconds).
This works fine if ONLY ONE row needs to be deleted, because if there is more than one row, the next CCSequence starts nearly the same time the previous, reading a incorrect position of the blocks above, leading to a incorrect cascade of blocks.
I solved that using a longer CCSequence, that takes a CCCallFuncND as the last argument:
id fadeOutSequence = [CCSequence actions:fadeout, destroyBlocks, notifyFadeFinish, nil];
//inside method specified for notifyFadeFinish:
id dropAbove = [CCSequence actions: dropBlocks, notifyDropFinish, nil];
//inside method specified for notifyDropFinish
//start a new delete sequence, if there is more rows to delete.
Now going to implement gravity mode, thanks again!