I am trying to create a new class that has many sprites on it , and they perform some actions one after another, and do some other cool stuff on screen.
this whole "show" will be in some class,that appears on screen when i need it .
On my main class Main which is a CCScene, i want to have the ability , to just call that animation class whenever i want to , and that it will be added to my main screen and perform its "show" .
some class :
//FADE ACTIONS
fadeInAction=[CCFadeIn actionWithDuration:0.5];
fadeOutAction=[CCFadeOut actionWithDuration:0.5];
fadeOutFastAction=[CCFadeOut actionWithDuration:0.1];
//circle animation parts
for (int i=0;i<12;i++)
{
circlePartsArray[i]=[CCSprite spriteWithFile:[NSString stringWithFormat:#"c%d.png",i+1]];
circlePartsArray[i].position=ccp(winSize.width/2,winSize.height/2);
[self addChild:circlePartsArray[i]];
}
Then from my main class i want to see on my screen, all the performance that someClass do- the sprites added, than they will fade etc..
How would i define that class, and add it to my main scene ?
Extend the CCNode class. Overwrite the CCNode Class' onEnter method, which you will use to create and add the child sprites, and start the animation. This way whenever you add the Node to your scene, it will begin the animation (probably using action sequences). Your animation action sequences should end with a CCCallBlock action, which you will use to clean up and remove the Node from its parent.
Related
I'm having trouble with overloading mouseMoveEvent.
I have subclassed QGraphicsView and overloaded mousePressEvent, mouseMoveEvent and mouseReleaseEvent. I am using these events to draw a custom QGraphicsItem - which is a Line. (mousePress - sets the start point of the line, mouseMove makes the line follow the cursor, second mousePress sets the end point of the line and mouseRelease stops the drawing of the line.)
I have also created another custom item - Node. The node is drawn with a mousePress event. I have 2 flags to differentiate between drawing Lines and Nodes. The ItemIsMovable flag of the Node is set to true and I have reimplemented mouseMoveEvent in the Node class to make it move (I change its coordinates and repaint it. And it worked fine.)
The problem is - when I implemented the mouseMoveEvent in my subclass of QGraphicsView(for the drawing of the Line) - the mouseMoveEvent of the Node class stopped working and the Nodes aren't moving anymore. How can I fix this?
Thank you for your time, your help will be appreciated.
You need to call base class (QGraphicsView) implementation from your implementation. Otherwise mouse events will not be processed by QGraphicsView and will not be passed to the scene and its items.
void MyView::mousePressEvent(QMouseEvent* e) {
QGraphicsView::mousePressEvent(e);
//your implementation
}
Hi I'm making an application that pulls data from a WFS and then displays those layers of data on a QGraphicsView on a widget. At the moment all layers are rendered and added to the same view meaning if I want to turn a layer of it means re-rendering all of it except that layer.
At the moment im adding a QGraphicsScene with Ellipse Items and Polygon Items added to it, to the graphics scene. I'm wondering if its possible to add multiple scenes to a graphics view or layers to a scene or something that would allow me to just hide/show certain points/polygons from a check box or something that simply hides a layer?
I know this is kind of vague but I'd appreciate any help.
Thanks.
You only need one QGraphicsScene, but the key here is that all QGraphicsItems and QGraphicsObjects can be parented.
If you create a single QGraphicsItem or QGraphicsObject as a parent object, it doesn't need to draw anything, but can be used as the root for a layer's items.
Therefore, subclass from QGraphicsItem to create a QGraphicsItemLayer class that doesn't render anything and add all the ellipses, polygons etc that are required in the same layer as children of that QGraphicsItemLayer.
When you want to hide a layer, just hide the parent QGraphicsItemLayer object and all its children will be hidden too.
-------- Edited --------------
Inherit from QGraphicsItem: -
class QGraphicsItemLayer : public QGraphicsItem
{
public:
virtual QRectF boundingRect()
{
return QRectF(0,0,0,0);
}
virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *)
{
}
};
Create a layer item:
QGraphicsItemLayer* pLayer = new QGraphicsItemLayer;
Add the objects you want to the layer, note that pLayer is passed as the parent
QGraphicsEllipseItem = new QGraphicsEllipseItem(pLayer);
Assuming you've created the QGraphicsScene with a pointer to it called pScene: -
pScene->addItem(pLayer);
Then when you want to hide the layer
pLayer->hide();
Or display the layer: -
pLayer->show();
Another way to go is QGraphicsItemGroup
Something like:
// Group all selected items together
QGraphicsItemGroup *group = scene->createItemGroup(scene->selecteditems());
...
// Destroy the group, and delete the group item
scene->destroyItemGroup(group);
So you can treat group as a layer and since group is also QGraphicsItem have all features like show()/hide() etc.
UPDATE: Changing Z-val for a group will allow you to implement things like 'move layer to top/bottom'
I think you could try to partition your objects according to z value: see setZValue.
Then introduce a mapping between layer id and indexing. A simple QStringList could do.
Of course, there are many details and variations that a practical solution will need to account for.
I want to create a rectangular board using QT. This board will be updated when a step is performed. For example on step x, the text in cell 5,6 updates from "not explored" to "explored".
I have looked through the QT documentation and found the class QGraphicsView. How can i use QGraphicsItem to simulate a cell where text can be written?
I am also open for alternatives.
Technically this could also be done by customizing a QTableView/Widget, but QGraphicsScene is more robust for custom graphics and performance.
From a high level view, you can either create a composite object representing a "Cell" item, or you can subclass a QGraphicsItem and do all the custom painting yourself.
When creating a composite object, that would just be a QGraphicsItem "Cell" subclass which contains maybe a QGraphicsRectItem and a QGraphicsTextItem as members, set to the cell instance as a parent. This will keep the child items translating with the parent cell item.
When creating a completely custom QGraphicsItem, you would define all the painting inside of the paint() method, which would draw a rectangle, and text taken from a value set on the instance.
Is it possible to add global layer in Cocos2d, which is not affected by scene transitions?
As I can see, it should be above all scenes hierarchy.
There's an old and short discussion on Cocos2d forum, but there's no answer:
http://www.cocos2d-iphone.org/forum/topic/8071
UPD. 'by scene transitions' I mean 'by animated scene transitions'.
You can use the notificationNode property of CCDirector to place a CCNode (ie.CCLayer, CCLabel, etc.) that will remain above scenes, even during transitions. Something like this:
CCLayer *layer = [CCLayer node];
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Test" fontName:#"Marker Felt" fontSize:32];
[layer addChild:label];
[[CCDirector sharedDirector] setNotificationNode:layer]; // Layer should be placed here
[layer onEnter]; // Schedule for updates (ie. so that CCActions will work)
It's meant for notification purposes (ads, etc.) so I wouldn't suggest trying to do anything too fancy from this node.
My gut says no, my brain says maybe.
The documentation says "It is a good practice to use and CCScene as the parent of all your nodes."
I can't test this right now, but looking at the inheritence diagram of CCNode, it looks like the logic of CCNode and CCScene differs only by anchor point. So, you might be able to create a CCLayer to use as your root layer, and add two children to it - A root CCScene, and a CCLayer for your GUI (with a higher Z order).
However, scene transitions may still be tricky, as you generally call CCDirector replaceScene, which works on the root scene you give it. If you give it the CCScene child of your root CCLayer, it may not draw the CCLayer and its GUI child. If you give it the root CCLayer, you are in the same situation as before.
I would still give it a try.
You can create a CCLayer subclass and turn it into a singleton. You add it to the scene like any other child node.
Whenever you transition from one scene to another, you can then remove the layer from the old scene and add it to the new scene as a child. This will only work if you don't use scene transition animations.
The alternative is to not use the CCDirector replaceScene method, and instead design your app to run as a single scene that never changes. To "fake" the changing of a scene you will use two layers, one global layer and another layer that contains your current scene nodes. If you want to transition you can animate the layer with CCActions to, for example, slide out of the screen while sliding in a new layer with a different node hierarchy. All you really lose is the convenience of the CCSceneTransition classes for animating scene changes.
I've a class that extends QWidget and contains a QLabel (lblBackground). I've overriden paintEvent function too.
I want to draw something on top of lblBackground however paintEvent method is called before the QLabel is drawn. Thus my custom drawings are overwritten.
Is there a way to change drawing order?
Painting the children on top of their parent is the common thing to do. That being said you could try one of the following options:
extend QLabel itself to paint whatever you want
try to set the Qt::WA_TranslucentBackground flag on the QLabel and having an alpha channel, so that the underlying parent (QWidget) would shine through
if you are only using the QLabel to paint some background, maybe you can get rid of it and paint the desired background first thing in the QWidget's paintEvent()?
If you want to use label as a background then just create your custom widget as a child of your label. May be split some window frame related tasks if any (to be implemented as a parent of the label) and drawing/controls/etc (to be child of the label).