我有一个类方法,我在其中创建和返回类对象.但我想在同一个类中访问该对象的某些属性.作为一个类方法,我不能在 .h 文件中声明变量,然后在其他方法中访问它.以下是代码如何在下面的实例方法中访问 backsprite 或 hudlayer 对象的值??
i have a class method in which i am creating and returning the class object. But i want to access certain properties of that object in the same class. Being a class method i cannot declare the variable in .h file and later access it in other methods. Following is the code How can i access the values of backsprite or hudlayer object in the instance method below??
// class 1
+(id)HUDWithBackgroundSprite:(NSString *)spriteName withRect:(CGRect)rect atPoistion:(HUDPosition)pos
{
HUDlayer *hud = [[HUDlayer alloc] init];
CCSprite *backSprite = [CCSprite spriteWithFile:spriteName];
[backSprite setContentSize:CGSizeMake(rect.size.width,rect.size.height)];
[backSprite setPosition:ccp(rect.origin.x,rect.origin.y)];
[backSprite setTag:100];
[hud addChild:backSprite];
[hud setTag:2];
return [hud autorelease];
}
// access it here
-(void)AddButtonToHUDWithImage:(NSString *)imageName selector:(SEL)selector withDisabledImage:(NSString *)disbdImageName
{
HUDlayer *hud = (HUDlayer *)[self getChildByTag:2];
CCMenuItem *menuItem1 = [CCMenuItemImage itemFromNormalImage:imageName selectedImage:imageName disabledImage:disbdImageName target:self selector:@selector(selector)];
CCMenu *menu = [CCMenu menuWithItems:menuItem1,nil];
CCSprite *sprite = (CCSprite *)[hud getChildByTag:100];
CGPoint pos = sprite.position;
//pos =[[CCDirector sharedDirector]convertToGL:pos];
[menu setPosition:ccp(pos.x+160,pos.y-30)];
[hud addChild:menu];
}
// class 2
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
HUDlayer *hud = [HUDlayer node];
// [scene addChild:hud z:1];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [[[HelloWorldLayer alloc]initWithHUD:hud]autorelease];
// add layer as a child to scene
[scene addChild: layer];
id mainHuds = [HUDlayer HUDWithBackgroundSprite:HUDBackground withRect:CGRectMake(160,450, 300,60) atPoistion:pos_Top];
SEL callOnClick;
[mainHuds AddButtonToHUDWithImage:ButtonBackground selector:callOnClick withDisabledImage:disabledBackground];
[scene addChild: mainHuds];
// return the scene
return scene;
}
我无法通过标签访问它,因为这里没有添加 + 方法中的 HUDlayer 对象,而是在调用该方法的另一个类中.
i am not able to access it via tags either as HUDlayer object in + method is not added here but in the other class which calls the method.
如果你有一个指向该类实例的指针/引用,你绝对可以在一个类的静态方法中访问该类的实例/成员变量.这是一个简单的例子...抱歉,如果有错别字...
You can absolutely in a static method of a class access instance/member variable of that class IF you have a pointer/reference to an instance of that class. Here is a simple example... sorry if there are typos...
//HUDLayer.h
// HUDLayer.h
@interface HUDLayer : CCLayer
{
NSString* aString;
}
@property (nonatomic, retain) NSString* aString;
+(void)modifyInstanceString:(HUDLayer*)hud;
//HUDLayer.m
// HUDLayer.m
@implementation HUDLayer
@synthesize aString;
-(id)init
{
if ((self=[super init]))
{
self.aString = @"initial value";
}
CCLOG(@"Initial value = %@", self.aString);
return self;
}
+(void)modifyInstanceString:(HUDLayer*)hud
{
hud.aString = @"Something else";
CCLOG(@"Modified aString via static method = %@", hud.aString);
}
@end
//HelloWorldLayer.m(或任何地方)
// HelloWorldLayer.m (or wherever)
HUDLayer* hud = [[HUDLayer alloc] init];
[HUDLayer modifyInstanceString:hud];
这篇关于从 iPhone & 中的类类型(+)方法访问对象Cocos2d?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!