title: Cocos2D sprite constrained to screen
body: <p>I'm having a problem with Cocos2D. I'm trying to have a sprite movable on screen (which I've been able to do), but stop it from being clipped by the edge of the screen. </p>
  
  <p>If I'm correct, sprites are imported to the layer as rectangles (spriteWithFile:@"filename.ext" rect:CGRectMake(0, 0, X, Y);). </p>
  
  <p>The only thing I've been able to figure out so far is to setup my code to detect if the rectangle of the sprite has gone outside of the "background" layer, and adjust it accordingly if it does. </p>
  
  <p>This is what I have so far, but it doesn't seem to be working correctly: </p>
  
  <pre><code>- (void)panForTranslation:(CGPoint)translation {    
  
      CGSize winSize = [CCDirector sharedDirector].winSize;
  
      float maxX = winSize.width - selSprite.contentSize.width/2;
      float minX = selSprite.contentSize.width/2;
      float maxY = winSize.height - selSprite.contentSize.height/2;
      float minY = selSprite.contentSize.height/2;
  
      if (selSprite.position.x &gt; maxX)
      {
          selSprite.position = ccp(maxX, selSprite.position.y);
      }
      else if (selSprite.position.x &lt; minX)
      {
          selSprite.position = ccp(minX, selSprite.position.y);
      }   
  
      if (selSprite.position.y &gt; maxY)
      {
          selSprite.position = ccp(selSprite.position.x, maxY);
      }
      else if (selSprite.position.y &lt; minY)
      {
          selSprite.position = ccp(selSprite.position.x, minY);
      }
  
      CGPoint newPos = ccpAdd(selSprite.position, translation);
      selSprite.position = newPos;
  
  }
  </code></pre>
  
  <p>Is there something completely obvious that I'm missing? My sprites keep moving off screen if I drag them, and I can't figure it out for the life of me.</p>