I'm trying to do the tutorial for this:
https://www.makegameswith.us/gamernews/384/build-your-own-2048-with-spritebuilder-and-cocos2d
I want to add CCSprite for the above tutorial. I laid out the CCSprite in Spritebuilder and turn their visibility off at first. When the respective tile appear, only then the CCSprite will be visible. For example, when Tile A first appear, then CCSprite A will appear. When Tile A merge with another Tile A, Tile B will appear and so does CCSprite B and so on.
The problem is that the switch case used to determine the tile value is located at Grid.m but the CCSprite added to Spritebuilder is under the CCNode of MainScene class. If i don't put custom class for CCSprite, XCode couldn't find the member variable. If i put custom class of "Grid" for CCSprite, XCode would return error of "This class is not key value coding-compliant for the key spriteFrame." What should i do here?
Assuming in your CCB the root node is a CCNode (A) and you add a child CCSprite (B) to it, then:
if specified, the custom class of node A must be a subclass of CCNode
if specified, the custom class of node B must be a subclass of CCSprite
Regarding the variable assignment:
the variable of node A should be empty (at best it will create a reference to itself)
if needed, the variable of node B should be set to "doc root" and given a legal ObjC variable name (ie _nodeB)
Now when you set the custom var of node B and it is set to "doc root", you must add a correspondingly named property or ivar to the custom class for node A. Which means node A must have a custom class for "doc root" variables to work. Let's name it CustomClassA:
#interface CustomClassA : CCNode
{
CCSprite* _nodeB;
}
#end
Assuming you made node B use a custom class (inherited from CCSprite) you can also specify the custom class:
#class Grid;
#interface CustomClassA : CCNode
{
Grid* _grid;
}
#end
And add #import CCSpriteCustomClassB.h to the implementation file of CustomClassA.
Note that variable assignment does work without using a custom class in the node where you enter the variable name, but the root node must use a custom class for variable assignment to work.
PS: I'm not entirely clear on the "owner" variable setting. Normally owner will be nil, it's only non-nil when you specify it in code using CCBReader's load:owner: method. This serves some purpose but I haven't found and can't think of a use case for it at the moment.
Related
I am wondering if there is a way to do this.
I create a Qt Application (using Creator 3.6.1, Qt 5.6.0).
I add a widget to the main window. For example a QGraphicsView called myView.
I create a C++ class derived from QGraphicsView (called DerivedView)
code of DerivedView class:
class DerivedView : public QGraphicsView {
...
I would like my new DerivedView class to control this widget. I can access a pointer to the object through ui->myView. Is there any way to do get my derived class to work with the already instantiated QGraphicsView?
DerivedView * dView = ui->myView;
Or do I need to not derive my class from QGraphicsView and just add a pointer as a data member?
class DerivedView {
QGraphicsView * gv;
...
You should promote your QGraphicsView to DerivedView, for this follows the following steps.
Right click on QGraphicsView and select promote to ..:
And add the name of the class and header
And press add.
And then press on promote.
After this, ui->myView is already a member of the DerivedView class
I want to know which method of child get called when we remove any Node from its
parent.
I created MyLayer by extending Layer class and then add my own sprite MySprite which extend Sprite class of cocos2d-x framework. I need to decrease a counter when child removed so i need a method which i'd override in my MySprite class.
I use this method to remove.
parent->removeChild(child);
Here parent is MyLayer and child is MySprite pointer.
If the child is running these two methods will be called:
child->onExitTransitionDidStart();
child->onExit();
If you remove the child with cleanup = true(which is the default value), child->cleanup(); will be also called.
So the best solution for you would be to override virtual void onExit(); function of the child. And in the overridden method don't forget to call Node::onExit(); or whatever your superclass will be.
I just cannot grasp the enum in class concept syntactically.
I am trying to disable QTextEdit's frame:
//in a header for my custom class where the main element is the textField
QTextEdit* textField;
...
//displaying it myCustomClass.cpp
textField = new QTextEdit(this);
textField->Shape = QFrame::NoFrame;
I get the error "invalid use of enum::Qframe::Shape". What is the correct syntax and why?
That's invalid C++: there's no such "Shape" member for QTextEdit. Moreover, Qt uses proper encapsulation, so the shape is not exposed by a member variable.
You have to call a method which sets the frame shape, and surprisingly enough, it's called setFrameShape!
textField->setFrameShape(QFrame::NoFrame);
I have a pretty complex problem... In Qt I have a custom class (named FotoGebouw) that inherits from QGraphicsItem, it also contains a pointer to another custom class (named Gebouw). If I want to acces the selected items from the scene, in other words the "FotoGebouw"items, I first have to cast them to QGraphicsItems. But this way, I seem to lose the pointer (called linkGebouw) that they were pointing to.
Does anyone know a way to get the FotoGebouw items that are selected from the scene, while I can still get the
QList<QGraphicsItem *>bordSceneGebouwen=bordscene->selectedItems();
FotoGebouw *teVerplaatsenFoto=dynamic_cast<FotoGebouw *>(bordSceneGebouwen[0]);
Gebouw *teVerplaatsen=teVerplaatsenFoto->linkGebouw;
I recently met a strange problem of my little program and it would be great if you help me to get the reason of this behavior.
My task is quiet simple - I want to use Qt Graphics Framework to show some objects and I want Box2D to calculate bodies position. So my class hierarchy looks like the following:
I have 1 base abstract class B2DObject. It contains some Box2D staff + some common parameters for its successors (names, some flags, etc.). It also has couple of pure virtual functions that will be reimplemented in successor classes.
Then I implement some classes that represent basic shapes: circles, rectangles, polygons, etc. I am doing it in the following way:
class ExtendedPolygon : public B2DObject, public QGraphicsPolygonItem { ... };
class ExtendedCircle : public B2DObject, public QGraphicsEllipseItem { ... };
etc.
(for those who are not familiar with Qt, QGraphics***Item is inherited from QGraphicsItem).
Also I inherited QGraphicsScene and reimplemented its mousePressEvent. In this function I request an object placed at some point on the screen using QGraphicsScene::itemAt function (which returns QGraphicsItem*), convert it to B2DObject* and try to get some internal field from this object:
void TestScene::mousePressEvent (QGraphicsSceneMouseEvent *event)
{
QGraphicsItem* item = itemAt (event->scenePos ());
if (item)
{
B2DObject* obj = reinterpret_cast < B2DObject* > (item);
QString objName = obj->Name(); // just for example,
// getting other internal fields has
// the same effect (described below)
// use retrieved field somehow (e.g. print in the screen)
}
// give the event to the ancestor
}
Unfortunately, dynamic_cast will not work here because these classes are completely unrelated.
Then I create necessary objects and add it to my scene:
ExtendedPolygon* polygon = new ExtendedPolygon (parameters);
polygon->setName (QString ("Object 1"));
...
TestScene scene;
scene.addItem (polygon);
(for those who are not familiar with Qt, here is the prototype of the last function:
void QGraphicsScene::addItem(QGraphicsItem *item);
I guess it just stores all items in internal index storage and calls QGraphicsItem::paint (...) when item needs to be repainted. I suppose QGraphicsScene doesn't make any significant changes to this item).
So my problems start when I run the program and click on an item on the screen. TestScene::mousePressEvent is called (see a piece of code above).
Mouse click position is retrieved, item is found. Casting works fine: in the debugger window (I'm using Qt Creator) I see that obj points to ExtendedPolygon (address is the same as when I add the item to the scene and in the debugger window I can see all the fields). But when I get some field, I receive garbage in any case (and it does not matter, what I'm trying to get - a QString or a pointer to some other structure).
So first of all, I would like to get any advice about my multiple inheritance. In 95% of cases I try to avoid it, but here it is very effective in the programming point of view. So I would appreciate it if you provide me with your point of view about the architecture of the classes hierarchy - does it even suppose to work as I expect it?
If on this level everything is quite fine, then it would be great if someone gets any idea why doesn't it work.
I have some ideas about workaround, but I really would like to solve this problem (just in order not to repeat the same error anymore).
Looks like I've found the root cause of my problem. It was just lack of knowledge regarding how multiple inheritance really works on data layer.
Let's assume that we have 2 basic classes, A and B. Each of them provides some internal data fields and some interfaces.
Then we create a derived class AABB, inheriting both A and B:
class AABB : public A, public B {...}
AABB could add some additional data fields and reimplement some of the interfaces, but it is not necessary.
Let's create and object of class AABB:
AABB* obj = new AABB ();
For example, obj points at address 0x8416e0. At this address starts data from ancestor class A. Data from ancestor class B starts with some offset (it should bw equal to sizeof (A)), for example, at 0x841700.
If we have some function f (B* b), and if we pass a pointer at AABB object to that function (like this: f (obj), obj is created above), actually not obj start address is passed, but rather a pointer at a start of B data section of AABB object.
Thus this misunderstanding of multiple inheritance inner works has led me to the problem I've got.
I guess Qobjects and multiple inheritance has been already treated. As an example: QObject Multiple Inheritance