Reputation: 1
I know this is a basic questions but it some how does not work.
I have an enemy
class which has in its .h
an instance variable :
@interface Enemy : CCLayer
{
CCSprite *enemy1;
CGSize winSize ;
}
I have another class which is the game class , and which wants to get access to the enemy1
.
So, this game class is importing the enemy.h
,and can use its functions , BUT i cant get its pointer enemy1
, i have tried with no luck :
Enemy *enemy=[[Enemy alloc]init];
[enemy enemy1]//error
enemy.enemy1 //error
Upvotes: 3
Views: 102
Reputation: 1209
You ought to use @property instead of creating ivars.
.h
@property (nonatomic) CCSprite *enemy1;
.m
@synthesize enemy1;
Upvotes: 2
Reputation: 13713
you need to define a getter for your enemy1
member. Most recommended way is to declare it as a property :
@property (nonatomic, readonly) CCSprite *enemy1;
And then synthesize it in the implementation file :
@implementation Enemy
@synthesize enemy1;
The @property
syntax declares how your member will be treated (at this case only with read permissions and no writing from the outside of the class)
The @synthesize
syntax generates the getter and setter methods for you depending on how the property has been declared (and since the property is readonly only the getter will be generated)
You could create a getter method yourself but synthesizing make this job for you (including correctly retaining and releasing when necessary)
You can find more information here
Upvotes: 1
Reputation: 8090
Since ivars by default are protected (means visible only to child classes), you should first declare them as public:
@interface Enemy : CCLayer
{
@public
CCSprite *enemy1;
CGSize winSize ;
}
After that you will be able to access ivars via standart C syntax like this: enemy->enemy1
.
But why not use properties?
Upvotes: 2
Reputation: 23001
I would suggest you should add readonly properties to access those instance variables.
@property (nonatomic,assign,readonly) CCSprite *enemy1;
@property (nonatomic,assign,readonly) CGSize winSize;
Upvotes: 4