How can I remove a physic node safely in cocos2d-x - c++

I add some nodes that move around the screen. at some point, when I press a button (MenuItemImage), I need to change layer (not scene). So, I try to remove all moving nodes' PhysicBody from the PhysicWorld and then, remove nodes themselves. It works usually, but sometimes, I get crash from inside of PhysicsBody::update function. at that point, this refers moving object PhysicBody. and, it seems parent is NULL. But, I try all PhysicBody from the World. So, after I remove, they should not be drawn (I guess this is causing the problem)
It is too obvious that there are some concurrency problem. but, I don't want to touch cocos2d-x dispatcher. so, what is the proper way to remove a PhysicBody and node from the world.
now, I call removeFromWorld() for PhysicBody and removeChildren for Cocos2d node. Even, I put some delay between these two operation, I still get crash sometimes.

Related

Using <curses.h> in multiple classes

I have three different classes. Each class is responsible for drawing a specific thing using ncurses.
I must draw all the three things at once. One of the classes is responsible for a board, and the other two classes draws something inside the board.
I got it to work, but the problem is that whenever I use clear, it clears the board and the other two things; I want the board to stay and never get erased. I want to clear the drawing that only the specific class is responsible for.
For example, let's say I have a board, and I have a person class and a dog class. When I call the draw method in the person class, it draws me the person inside the board, but whenever I move it to a different point, it draws a new person, but it never clears the old person.
Same thing with the dog unless I use the clear method from the curses.h but it erases and clears everything including the board and the dog.
However, I only want to erase the person and not everything. Is there any built-in method to use from ncurses other than clear or erase, or anything that clears the whole screen?
As Ivan Rubinson suggested in the comments, the usual approach is to clear everything then redraw everything every frame/repaint.
According to the documentation, clear() should clear the whole screen:
The clear(), erase(), wclear() and werase() functions clear every position in the current or specified window.
The clear() and wclear() functions also achieve the same effect as calling clearok(), so that the window is cleared completely on the next call to wrefresh() for the window and is redrawn in its entirety.
Your main loop / drawing code should look something like this (pseudocode):
clear(); // clear everything
// redraw everything
for(auto& widget : drawables)
{
widget.draw();
}
// display
refresh();
You can do this by creating a window for each of the objects, and removing them when they're done. That way, you need only clear the window that you're removing, refresh it and then delete it.
That is, use newwin, waddstr, wrefresh, wclear and delwin rather than addstr, refresh and clear.

Changing scenes in Minko

Is there a standard way to change between scenes in Minko? Specifically, I'm imagining each scene as a different level, and when the user completes some task the entire level changes.
I know I could just update all my meshes and whatnot but this feels poor; is there a way I can build a root node for a new scene and then switch the Canvas to using that root node instead (as well as force a rererender, since all the objects will have changed)?
Your second idea is fine. You can create a separate root Node with its own SceneManager sharing the Canvas. Add your new scene to this Node. When you're ready to switch, change the SceneManager you use in the enterFrame signal to render. This should trigger a re-render, upload textures, calls component added signals...
In Minko, there is no global singleton or anything that would prevent from having to completely separate scenes. Each SceneManager will reference its own AssetLibrary. This way, if you switch scenes and remove references to the previous SceneManager, the assets will be released from memory.

Trouble removing sprites from parent node

I'm making an endless running game in which users dodge obstacles, and I'm working on producing the obstacles right now. The plan I had where I'm spawning these obstacles is as follows:
obstacle->setPosition(CCPointMake(this->getContentSize().width, this->getContentSize().height*.75));
obstacle->setScale(.5);
this->addChild(obstacle);
_obstacles->addObject(obstacle);
obstacle->runAction(CCMoveBy::create(2.0, CCPointMake(-(this->getContentSize().width + obstacle->getContentSize().width/2), 0)));
obstacle->removeFromParent();
I set the position, set it's scale, add it to the scene, run an action on it so that it moves across the screen from right to left, add it to an array called _obstacles to be used elsewhere, and then I remove it from the scene so as to save memory. However, the problem is that once I try implement this, the obstacle doesn't show up at all as if it's nowhere to be seen. When I don't call obstacle->removeFromParent() it shows up and performs the action. What am I doing wrong here? If I don't call removeFromParent(), what do I call? Is there a problem in my code not related to removeFromParent()?
The reason that obstacle doesn't apeear at all is that it is removed as it start moving. You just have to create a sequence of move action and function call with obstacle as parameter and than remove this obstacle in that function , so that obstacle will be removed after moving out of screen.
CCCallFuncN *myCallFunc = CCCallFuncN::create(this, callfuncN_selector(CLASS_NAME::removeObstacles));
obstacle->runAction(CCSequence::create(CCMoveBy::create(2.0, CCPointMake(-(this->getContentSize().width + obstacle->getContentSize().width/2), 0)),myCallFunc,NULL));
Method to remove obstacle from array and from parent view
void CLASS_NAME::removeObstacles(CCObject* pSender){
// Type cast pSender to obstacle type e.g if obstacle is of CCSprite type.
CCSprite *tempObstacle = (CCSprite *)pSender;
_obstacle.pop_back(tempObstacle);
tempObstacle->removeFromParent();
}
Don't forget to replace CLASS_NAME by your class name
I would need more info on what removeFromParent is doing and how you use _obstacles
I'm taking a guess on how the parent is structured. I would guess that it has a list of children and when the parent is updated/render, it calls those functions for its children. If removeFromParent removes the object from its parent list, then that would explain why it is not being render or used in the scene. If you have a setup like this, then you should only call removeFromParent before destroying the parent object

VTKActor not visible after render but visible on camera->resetview()

I am working on a qt-vtk project. We have a line drawing function. where straight lines are created between two mouse click position. But once actor is created it is not visible. I was calling render function just after adding the actor. But it didn't work. But if i do camera->resetview() lines become visible , but entire perspective changes. Where am i doing wrong ?
thanks
Rwik
This may not be relevant to you, but I had this exact same problem (in ActiViz [managed VTK]) and wrangled with it for a week, so I hope this helps someone out there. It turned out to be a problem with the location of the lines we wanted to draw on the canvas; they were too far away from the camera (on the Z axis) to be visible.
For us, we were trying to draw a cross on the viewing area wherever the user clicked. The data points were there, as were the actors and whatnot, but they would only be visible in the scene if you called resetCamera() and thusly changed the camera's configuration.
Initially, I blamed the custom interactor that we had to add to cirvumvent the default interactor's swallowing of MouseUp events (intended behavior). Investigation revealed that this seemed unlikely.
After this I shifted the blame onto the camera under the suspicion that perhaps the reset call was making a call to some kind of update method which I wasn't aware of. I called resetCamera() and then reverted the camera values to what they were initially.
When this was successfully done, it eventuated that the crosses would appear when the camera zoomed out and then disappear again as soon as it was set back, and it was at this point I realized that it was something to do with the scene.
At this point, I checked the methods we were using to retrieve the mouse location in 3D and realized that the z value was enormous and it was placing the points too far away as a byproduct of VTK's methods to convert 2D locations on the control to 3D locations in the scene and vice versa.
So after all that, a very mundane and avoidable mistake that originated from the methods renderer.DisplayToWorld() and WorldToDisplay().
This might not be everyone's problem, but I hope I've spared someone a week of fiddling around with VTK.
I think that's a bit hard to help, without see the code, but have you tried using
ui->qvtkwidget->update();
, where ui is the instance of your class derived from QMainWindow?

How to change touch priority on overlapping sprites

Is there any way to change the touch priority for cocos2d iOS sprites? What I have are multiple cards on the screen and they are arrayed in an arc, just like it would when you hold them in your hands. So in this setup, they overlap, and I need to recognize on which card the touch was made. I could measure the coordinates of each vertex of cards and determine the visible area of a card and then check if the touch was made inside that area (couldn't I?) but I thought there would be an easier way to deal with this, say changing the touch priority? Which means that the card closest to the screen would have the highest priority and it'll keep decreasing along the way into the background, so that even if the touch was made on 2 sprites at once (the above and below one), it would be registered only on the sprite with higher priority.
Reading on the internet only revealed ways to change the priority for a sprite and layer so that it defines whether the touch was made on the layer or the sprite, but that's not what I want.
As far as I know, by default you get exactly that behavior, the sprites closer (on the z ax) to you have priority. However, I think they pass down the event to the ones behind them as well. So, what i think you need to do is to eat the event when it gets to any of your sprites. To do that, just return NO when overwriting the "touchBegin" method. Hope it helps.