1

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;
        }
    }
Guru
  • 21,652
  • 10
  • 63
  • 102
Neil Galiaskarov
  • 5,015
  • 2
  • 27
  • 46
  • cute meaning what? Keep in mind that moving the layer to touch locations will never give you smooth results without interpolating between touches (touch locations can vary greatly) and gradually moving towards the last touch location (by updating position every frame or as a crutch by using CCMoveTo). – CodeSmile Feb 05 '13 at 19:14
  • @LearnCocos2D, thanks, sounds interesting. I want to try to implement this by myself. For practice. – Neil Galiaskarov Feb 06 '13 at 05:17

1 Answers1

0

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

Guru
  • 21,652
  • 10
  • 63
  • 102