不让精灵移除屏幕外 重写setPosition方法

-(void) setPosition:(CGPoint)pos
{
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    float halfWidth = contentSize_.width * 0.5f;
    float halfHeight = contentSize_.height * 0.5f;
    
    // Cap the position so the Ship's sprite stays on the screen
    if (pos.x < halfWidth)
    {
        pos.x = halfWidth;
    }
    else if (pos.x > (screenSize.width - halfWidth))
    {
        pos.x = screenSize.width - halfWidth;
    }
    
    if (pos.y < halfHeight)
    {
        pos.y = halfHeight;
    }
    else if (pos.y > (screenSize.height - halfHeight))
    {
        pos.y = screenSize.height - halfHeight;
    }
    
    // must call super with the new position
    [super setPosition:pos];
}
posted @ 2012-05-19 22:40  高笑228  阅读(188)  评论(0编辑  收藏  举报