I have one CCSprite class named Fruit and two CCNode classes named FruitManager and GameScene. FruitManager is a child of GameScene, and all of the Fruit class instances are being initialized in an NSMutableArray in FruitManager. I have tried many different ways of getting the instances of the Fruit class to draw to the GameScene class, but none of them have worked. NSMutableArray is being initialized as:
NSMutableArray *fruitArray;
fruitArray = [[NSMutableArray alloc]init];
Below that I am adding objects of type Fruit to it. Those Fruit objects are also being added as children to the FruitManager class. I know for a fact that the array is initializing and retaining the objects properly, but I just don't know why they aren't drawing to GameScene.
Sorry but have you added fruit objects in your array to the GameScene
Something like
for (int i=0; i < [fruitArray count]; i++) {
[self addChild:[fruitArray objectAtIndex:i] z:2];//self or GameScene z optional
}
The animation manager in my Fruit class was being set to nil. I was not initializing my Fruit correctly in the init method. Thanks everyone!
Related
I am working on making my own designer widget that looks and functions like qt. But now I needed to know how the property is created. I knew we can get the properties of an widget using QMetaObject and QMetaProperty but my question is will I be able to get the class name of each property. Like object name property comes from QObject and geomentry property comes from QWidget. Is it so that i should hard code myself to model or is there a way to get the classInfo from property. I have attached the image which im trying to achieve any response or related post is appreciated.
thanks,
In order to create mapping between classes and their properties you need to traverse through your object's class hierarchy. Here is the sample code that crates such mapping:
// Create properties per class mapping for the initialObject.
const QMetaObject *mo = initialObject->metaObject();
QMap<QString, QStringList> propertyMap;
do {
QStringList properties;
for(int i = mo->propertyOffset(); i < mo->propertyCount(); ++i) {
properties << QString::fromLatin1(mo->property(i).name());
}
propertyMap[mo->className()] = properties;
} while (mo = mo->superClass());
Now the propertyMap contains all properties sorted by the class names they belong to.
I create my own class:
class myTile : public QGraphicsItem{}
I add these myTile in a QGraphicsScene. Every myTile has a value and a getter for the value. Now I want to get a myTile and read its value.
I tried collidingItems() and itemAt(), but the problem is: These two method can only return QGraphicsItem. The QGraphicsItem doesn't have my return value method in myTile. Is there a way to get a custom item?
Thank you in advance!
myTile *t = (myTile*)myScene->itemAt(i);
t->myMethod();
Is it possible to access b2Fixture and b2Body properties in one class in another class in such a way that joints could be created. If yes, how can it be done. Please Help
Create property to get access to your b2Body object. Then you can get list of b2Fixtures for this b2Body.
in your .h file
#interface MyClass
{
b2Body* m_body;
}
#property (nonatomic, readonly) b2Body* body;
#end
in your .mm file (you must use .mm extension to be able to use c++ classes and methods)
#implementation MyClass
#synthesize body = m_body;
- (id) init
{
self = [super init];
if( self != nil )
{
// create your b2Body here and
}
return self;
}
#end
After this all MyClass instances will have property body, that can be accessed as
myClassInstance.body
or
[myClassInstance body];
I am developing a iphone game app using Cocos2d.
I find this is an excellent solution to my problem of storage an integer to the sprite's property:
sprite.userData = 123;
However, sprite.userData can store only one piece data. If I need to store three pieces of data, what is the best way of doing it?
'userData' is actually void pointer not retained by the 'Node' class:
void *userData_;
As such it can points to any data structure or class (or even a C function).
Your best bet, for one or nine custom variables, is to subclass CCSprite and have your custom variables as class variables of the new class, using properties to read/write them publicly.
Like a well-versed Objective-C programmer would do.
To explicitly answer your question: You can set an NSDictionary or NSArray to userData, which themselves can contain more than one item. But if you're going that far: read above.
Extend the ccsprite class into userdata class and use that class for any no of variables you want to create... however you will need to use that class for all checks and conditions ....
Here take the code
Userdata.h
//
// UserData.h
//
//
#import "CCSprite.h"
#import "Constants.h"
#class GameLayer;
#interface UserData : CCSprite {
int userDataType;
int tag;
int parentTag;
GameLayer *gameLayer;
BOOL readyForDeletion;
}
#property(nonatomic) int userDataType;
#property(nonatomic) int tag;
#property(nonatomic) int parentTag;
#property(nonatomic, assign) GameLayer *gameLayer;
#property(nonatomic) BOOL readyForDeletion;
-(id) initWithSpriteName:(NSString *)spriteName;
#end
and the userdata.mm
//
// UserData.mm
//
//
#import "UserData.h"
#implementation UserData
#synthesize userDataType;
#synthesize tag;
#synthesize parentTag;
#synthesize gameLayer;
#synthesize readyForDeletion;
-(id) initWithSpriteName:(NSString *)spriteName {
if (self = [super initWithFileName:spriteName]) {
}
return self;
}
-(void) onExit {
[super onExit];
}
-(void) dealloc {
[super dealloc];
}
#end
I have a basic class for detecting collisions but I can't figure out how to see what bodies are colliding to trigger the appropriate event. Lets say I have a pong game and in it a ballBody and topwallBody. How would I figure out if these are colliding or not. Here is the class I'm using just to give you an idea.
class MyListener : public b2ContactListener
{
void BeginContact(b2Contact* contact)
{
b2Fixture* fixtureA = contact->GetFixtureA();
b2Fixture* fixtureB = contact->GetFixtureB();
b2Body* body1 = fixtureA->GetBody();
b2Body* body2 = fixtureB->GetBody();
cout << "started";
}
void EndContact(b2Contact* contact)
{
cout << "ended\n";
}
};
MyListener listener;
world.SetContactListener(&listener);
It looks like I can get the bodies in the pointers but I have no idea how to compare them to other bodies.
When you create the bodies, set the userdata to something meaningful, but be consistent :) A good tip is to always have the same kind and type of data in there, an entity id or reference to an actor.
Straight from the documentation:
b2BodyDef bodyDef;
bodyDef.userData = &myActor;
So if you went this road you would get the actor from the b2Body and inform it of the collision, or do something else.
Also from the docs:
b2Fixture* fixtureA = myContact->GetFixtureA();
b2Body* bodyA = fixtureA->GetBody();
MyActor* actorA = (MyActor*)bodyA->GetUserData();
In the code above you would have the actor/entity and could do whatever you would like... actorA.explode().
Being consistent will likely save you from insanity. If one sticks all kinds of data in there it'll become really hard knowing what data is in what body. Plus you can't really handle the contacts in any generic way.
The answer Skurmedel gave helped me immensely on this. I thought I would add a little bit of information from what I was doing to solve this.
I, like the OP, wanted to check what was hitting what. I have hearts bouncing around inside the walls of the game screen and wanted to know if they are hitting other hearts, or the walls.
I used this code to view the contact
world.setContactListener(new ContactListener() {
#Override
public void beginContact(Contact contact) {
Gdx.app.log("GameScreen", "Contact Made! Fixture A: " + contact.getFixtureA().getBody().getUserData().toString());
Gdx.app.log("GameScreen", "Contact Made! Fixture B: " + contact.getFixtureB().getBody().getUserData().toString());
}
And within my heart object I overrode the toString method to simply return 'Hear' for now. I am setting the userData for the body as the whole sprite object, so I have flexibility with the body in the object itself.
Not having my actual class references for the floor, walls, and ceiling I simply set the userData as 'Floor' etc.
GameScreen: Contact Made! Fixture A: Ground
GameScreen: Contact Made! Fixture B: Heart
Using this method, and beefing it up later, I will be able to change how the objects react based on who they are contacting.