Iphone 如何定义边?

Iphone 如何定义边?,iphone,background,cocos2d-iphone,scroll,edges,Iphone,Background,Cocos2d Iphone,Scroll,Edges,我已经编写了一个代码,用于在Cocos2d中滚动背景以创建相机效果,但我无法阻止相机超出背景的边缘。我的背景是一幅1440*1080的图像。我的代码是: +(id) scene { CCScene* scene = [CCScene node]; TileDemo* layer = [TileDemo node]; [scene addChild: layer]; return scene; } -(id) init { if ((self = [

我已经编写了一个代码,用于在Cocos2d中滚动背景以创建相机效果,但我无法阻止相机超出背景的边缘。我的背景是一幅1440*1080的图像。我的代码是:

+(id) scene

{
    CCScene* scene = [CCScene node];
    TileDemo* layer = [TileDemo node];
    [scene addChild: layer];
    return scene;
}

-(id) init

{

    if ((self = [super init]))

    {
        self.isTouchEnabled = YES;
        CCSprite *Nivel1 = [CCSprite spriteWithFile:@"Nivel1.png"];
        Nivel1.position = ccp(0.5f, 0.5f);
        [self addChild:Nivel1 z:0 tag:1];
    }

    return self;

}

-(void) dealloc

{
    [super dealloc];

}

-(void) registerWithTouchDispatcher

{

    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

}

-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event

{
    return YES;
}

-(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent*)event

{

}

-(void) ccTouchCancelled:(UITouch*)touch withEvent:(UIEvent*)event

{

}

-(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent*)event

{
    CGPoint touchLocation = [touch locationInView: [touch view]];
    CGPoint prevLocation = [touch previousLocationInView: [touch view]];

    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
    prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation];

    CGPoint diff = ccpSub(touchLocation,prevLocation);

    CCNode* node = [self getChildByTag:1];
    CGPoint currentPos = [node position];
    [node setPosition: ccpAdd(currentPos, diff)];
}

@end

您必须计算精灵的最大和最小位置,并在
touchMoved:withEvent:
方法中进行检查。在我看来,当精灵的主播点位于(0.f,0.f)而不是标准(0.5f,0.5f)时,计算这些值更容易。那么你的位置将会被改变

CGPoint maxPos = ccp(0.f, 0.f);
CGPoint minPos = ccp((spriteWidth - screenWidth) * (-1), (spriteHeight - screnHeight) * (-1));
然后在设置之前,只需检查精灵的新位置是否在有效范围内