¿Accediendo a un método de tipo Object from Class (+) en iPhone y Cocos2d?

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;
}

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.

preguntado el 31 de enero de 12 a las 08:01

i dont quite understand.. Why cant you declare it in your .h file? The only way to access is if the variable is in the .h file.. -

no that would be a instance variable which wont be accessed in a class method.. only in instance method -

3 Respuestas

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

@interface HUDLayer : CCLayer
{
    NSString* aString;
}

@property (nonatomic, retain) NSString* aString;

+(void)modifyInstanceString:(HUDLayer*)hud;

// 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 (or wherever)

HUDLayer* hud = [[HUDLayer alloc] init];
[HUDLayer modifyInstanceString:hud];

Respondido 01 Feb 12, 00:02

There is no reason you can't declare the object as a variable in your .h file:

HUDlayer *hud;

That's exactly how methods of that class would access it. If you're getting errors I can only assume the layout of your .h file is wrong. I normally set up scene and layer like so:

@interface ExampleLayer : CCLayer
{       
        //vars
}

//methods

@end

@interface ExampleScene : CCScene 
{   
    ExampleLayer *layer;
}

@end

Respondido el 31 de enero de 12 a las 14:01

Only static variables can be used in Static Methods... Declare the object in .h file as Static HUDlayer *hud; and access it with class name... Still M not able to understand why you are creating so many static methods as they are very heavy and are preloaded even of you don't need that class....

Respondido el 31 de enero de 12 a las 15:01

It is inaccurate to say that only static variables can be used in static methods(, or class methods). Objects allocated on heap can be used here. And if it returned that object reference to the caller, and was then added to the view hierarchy(, and was retained implicitly), later routines could retrieve from the hierarchy. - ZhangChn

found a better way around with help of jer answer.. only two static methods are there which i guess can make do - hemant

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.