cocos2d-iphoneccscrolllayer

how to scroll CCLayer smoothly?


I want to scroll my map by touching. Could you give me basic concepts how to reach it? Here is my ugly simple code. Just modifying layer's position by each touch moving. It works, but it's not cute as could be.

.h
CGPoint *touchBegin;
CGPoint *touchmove;

.m


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


    CGPoint touchLocation = [touch locationInView:[touch view]];
    touchLocation = [[CCDirector sharedDirector]
                     convertToGL:touchLocation];
    touchBegin=new CGPoint(touchLocation);
}



  -(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
        CGPoint touchLocation = [touch locationInView:[touch view]];
        touchLocation =
        [[CCDirector sharedDirector] convertToGL:touchLocation];
        touchmove = new CGPoint(touchLocation);
        [self setPosition:ccp(self.position.x/100-(touchBegin->x-touchmove->x),self.position.y/100.0f)];
        delete touchmove;
            touchmove=NULL;
}


-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
        if (touchBegin)
        {
            delete touchBegin;
            touchBegin=NULL;
        }

        if (touchmove)
        {
            delete touchmove;
            touchmove=NULL;
        }
    }

Solution

  • Try CCScrollLayer Extension. Here is link

    « First add both files to your project

    « In your scene import CCScrollLayer.h

    « In your scene's init method construct each layer and pass it to the CCScrollLayer class

    For more details refer CCScrollLayerTestLayer.m in cocos2d-iphone-extensions