Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So I built my first game app, and it was a lot of fun. I'm really trying to expand on some of the ideas, but I'm running in to some serious variable scope issue. What I want to do is encapsulate a CCSprite as an instance variable, and then modify it in my "HelloWorldLayer"

In my header:

@interface HelloWorldLayer : CCLayer
{
    CCSprite *mySprite;
}

and in my HelloWorldLayer.m I run into the problem where if I try to do something like

- (id)init
{
   CCSprite mySprite = [CCSprite initWithFile:@"spriteFile.png"];
}

I run into either

  1. Memory management issues
  2. Variable scope issues (overwriting etc.)

What I want to do is be able to declare the sprite, and then change certain properties in other methods such as ccTouchesMoved. How can I do this?

P.S. I'm sorry if the code is a little rough around the edges, I have been writing Ruby for the last two weeks and I'm a little out of it with reference to objective.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Your declaration is correct, however, your instantiation should not have the CCSprite class name in front. Just

mySprite = [[CCSprite spriteWithFile:@"spriteFile.png"] retain]

is fine. If you're using ARC, then you can leave out the retain.

I normally like to just use properties though so I can refer to them easier and also have the option of accessing the ivar from another class.

@interface HelloWorldLayer ()
@property (nonatomic, strong) CCSprite *mySprite;
@end

@implementation HelloWorldLayer
- (id) init {
    if ((self = [super init])) {
        self.mySprite = [CCSprite spriteWithFile:@"spriteFile.png"];
    }
    return self;
}

- (void) someMethodName {
    // You can do anything with the sprite anywhere else in the app.
    self.mySprite.position = ccp(200, 200);

    // You won't need to retain or release it anywhere in the implementation
    // as long as you're using ARC.
}
@end 
share|improve this answer

mySprite is declared as member variable. You redeclared again in the init function as local variable so it won't overwrite the member variable. I suggest you should not redeclare it again as local variable as you will use it in somewhere else.

You will need to retain varible created from initWith, e.g.,

 mySprite = [[CCSprite initWithFile:@"spriteFile.png"] retain];

You will need to release it when you no longer use it.

share|improve this answer
    
OK. Cool. I'll let you know how that works –  Brian Wheeler Jan 20 '13 at 23:10
    
I'll need to release it when I no longer need it, how do I go about doing that? –  Brian Wheeler Jan 20 '13 at 23:37
    
[mySprite release]; –  neo Jan 21 '13 at 1:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.