如何使用滑动手势平滑视差滚动

时间:2012-03-23 11:25:36

标签: iphone xcode cocos2d-iphone gesture-recognition parallax

我添加了example提供的手势类来支持手势认知。 我在顶层中有4个精灵要在向左/向右滑动时移动,并且在底层有3个精灵要滑动。

我有以下代码

-(id) init
{
// Some Stuff
[Gestures sharedGestures].swipeTolerance = 40;
[Gestures sharedGestures].pointResetLimit = 10;
[Gestures sharedGestures].delegate = self;
[Gestures sharedGestures].useX = NO;
[Gestures sharedGestures].useCircle = NO;
[Gestures sharedGestures].useSquare = NO;
for (int i=1; i<6; i++) 
    {
        NSString *str=[NSString stringWithFormat:@"Howtoplay_0%d.png",i];
        topLayer[i]=[CCSprite spriteWithFile:str];
        [topLayer[i] setPosition:ccp(640/2,480/2)];
        [self addChild:topLayer[i] z:1];
        [topLayer[i] setVisible:NO];
    }
    [topLayer[1] setVisible:YES];
    [topLayer[1] setPosition:ccp(320/2,480/2)];

    for (int i=1; i<4; i++) 
    {
        NSString *str=[NSString stringWithFormat:@"HowtoplayB_0%d.png",i];
        backLayer[i]=[CCSprite spriteWithFile:str];
        [backLayer[i] setPosition:ccp((320*i)/2,480/2)];
        [self addChild:backLayer[i] z:-1];

    }
    [backLayer[1] setPosition:ccp(320/2,480/2)];
}

-(void) registerWithTouchDispatcher{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:YES];
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
[[Gestures sharedGestures] reset];
return TRUE;
}

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint point = [touch locationInView: [touch view]];
    CGPoint converted = [[CCDirector sharedDirector] convertToGL:point];
    [[Gestures sharedGestures] addPoint:converted];
}
-(void) swipeLeftComplete
{
    NSLog(@"Swipe Left Complete Called");
    [self MoveLeft];

}
-(void) swipeRightComplete
{
    [self MoveRight];
}
-(void)MoveLeft
{
    [topLayer[currentTop+1] setPosition:ccp(640/2,480/2)];
    [topLayer[currentTop+1] setVisible:YES];
    [topLayer[currentTop] runAction:[CCMoveTo actionWithDuration:2 position:ccp(-320/2,480/2)]];
    [topLayer[currentTop+1] runAction:[CCMoveTo actionWithDuration:2 position:ccp(320/2,480/2)]];

    [topLayer[currentTop-1] setVisible:NO];
    currentTop+=1;
}
-(void) MoveRight
{
    [topLayer[currentTop-1] setVisible:YES];
    [topLayer[currentTop] setVisible:YES];
    [topLayer[currentTop] runAction:[CCMoveTo actionWithDuration:2 position:ccp(-320/2,480/2)]];
    [topLayer[currentTop-1] runAction:[CCMoveTo actionWithDuration:2 position:ccp(320/2,480/2)]];
    currentTop--;
}

我无法向右/向左平滑地滑动它给出topLayer的重叠。请帮助我平滑图层移动。

任何类型的帮助都会得到赞赏

...谢谢

1 个答案:

答案 0 :(得分:0)

你的问题听起来与我和其他人面临的问题相似,他们面临着拼贴精灵和界限。尝试将其添加到init语句的顶部:

[[CCDirector sharedDirector] setProjection:CCDirectorProjection2D];