views:

32

answers:

1

Stuck on what I figure is simple thing here. Basically I need to pass a pointer to an object as an argument into an instance method of another class. Said differently: I have a class that creates "Things" and I have an instance of another class that I want to receive the "Things."

Working with Cocos2D frameworks. The Things are a custom subclass of CCSprite, and the instance that receives them is a CCLayer.

I figure I'm misunderstanding something basic about ivars or maybe properties here. Any pointers in the right direction would be appreciated.

Here's the interface for ThingLayer, which should receive the "thing":

@interface ThingLayer: CCLayer {
 CCTextureAtlas *textureAtlas; 
 ThingLayer *thingLayer;
 NSMutableArray *ThingsArray;
}


- moveThingtoLayer:(Thing*)athing;

@end

And here's how I'm trying to message to the instance, from outside the class:

 [ThingLayer moveThingtoLayer:thing];

I realize I'm asking the class here, not the instance... which is giving me "may not respond to..." errors. But this isn't working either (asking name of instance)...

 [thingLayer moveThingtoLayer:thing];

Any obvious answers?

+1  A: 

Looks like you should have

ThingLayer *thingLayer = [[ThingLayer alloc] init]; 
[thingLayer moveThingtoLayer: thing];

As a side thought, you most likely want to init a new thing in thingLayer so that instance owns the Thing, and release thing after calling moveThingToLayer.

blu
Side thought helped me fix it. I was thinking about it wrong- I had a separate class that was basically just a method for producing things, and when I brought that method inside ThingLayer, the messaging problem went away. Thanks!The code you have above makes sense as well... but just so I'm clear, would that pointer refer to an existing instance of ThingLayer class, or to a new doodad? If it's a new one, then I smell layer creep...
Macgeo
You would create a new object. Think about this, if its the same object, what happens if something outside of the containing class modifies the instance? Even worse, what would happen if something else called release on the object?
blu