Accessing an Object from Class type(+) method in iPhone & Cocos2d?

大兔子大兔子 提交于 2019-12-23 19:25:25

问题


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.


回答1:


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



回答2:


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



回答3:


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....



来源:https://stackoverflow.com/questions/9076699/accessing-an-object-from-class-type-method-in-iphone-cocos2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!