title: Change Sprite with Buttons - Cocos2d
body: <p>I have 2 buttons (left and right) and a sprite sheet that contains all 55 images.  I was wondering, what is the best way to go through each sprite using the buttons?</p>
  
  <p>E.G.
  Press the left button and the first sprite is added. Press the right button, the first sprite is removed and the second sprite to added. So on and so forth until it reaches the last image.</p>
  
  <p>This is the Sprite Sheet</p>
  
  <pre><code>#import "cocos2d.h"
  
  
  @interface GameScene : CCLayer {
  
  CCSpriteBatchNode *pspriteSheet
  
  }
  
  +(CCScene *) scene;
  
  @property (nonatomic, retain) CCSprite *p;
  
  @end
  ----------------------------------------------------
  #import "GameScene.h"
  
  
  @implementation GameScene
  
  @synthesize p = _p;
  
  
  +(CCScene *) scene
  {
      CCScene *scene = [CCScene node];
      GameScene *layer = [GameScene node];
  
      [scene addChild: layer];
      return scene;
  }
  
  -(id) init
  {
      if ((self = [super init]))
      {
  [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
           @"PAnim.plist"];
  
          pspriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"PAnim.pvr.ccz"];
  
          [self addChild:pspriteSheet z:0];
  }
      return self;
  }
  
  - (void) dealloc
  {
      CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
  
      _p = nil;
  
      [CCSpriteFrameCache purgeSharedSpriteFrameCache];
      [CCTextureCache purgeSharedTextureCache];
  
      [super dealloc];
  }
  </code></pre>
  
  <p>Should I just keep adding them and removing them?</p>
  
  <p>E.G.</p>
  
  <pre><code>-(void)buttons:(CGPoint)touchLocation
  {
      if (CGRectContainsPoint(leftB.boundingBox, touchLocation) &amp;&amp; tapP == YES &amp;&amp; paused == NO) {
          if (count == 1)
          {
              _p = [CCSprite spriteWithSpriteFrameName:@"p1.png"];
              [pspriteSheet addChild:_p];
              count = 2;
              _p.position = ccp(240, 215);
          }
  if (CGRectContainsPoint(rightB.boundingBox, touchLocation) &amp;&amp; tapP == YES &amp;&amp; paused == NO) {
          if (count == 2)
          {
              [pspriteSheet removeChild:_p cleanup:YES];
  
              _p = [CCSprite spriteWithSpriteFrameName:@"p2.png"];
              _p.position = ccp(240, 215);
              [pspriteSheet addChild:_p];
  
              count = 3;
          }
  }
  </code></pre>
  
  <p>Here is where the "buttons" method is called</p>
  
  <pre><code>    - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
  { 
      CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
  
      [self buttons:touchLocation];
  
      return TRUE;
  }
  </code></pre>