# Scrolling effect after touch

i'm making map for a game and need to make scrolling of map, so the main thing what i need is to calculate speed and move layer after player remove finger from screen.

I make it my own, but i think that it must be better decisions:

- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
_isDragging = YES;
}
// ON TOUCH MOVING
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint previous = [[CCDirector sharedDirector] convertToGL:previousLocationOne];
CGPoint current = [[CCDirector sharedDirector] convertToGL:touchLocationOne];

CGPoint delta = ccpSub(current, previous);
}

- (void)update:(CCTime)delta {

CGPoint position = _gameLayer.position;
static float friction = 0.86f; //0.96f;

// AFTER MOVING ENDED
if(!_isDragging) {
_velocity = ccp(_velocity.x * friction, _velocity.y *friction);
_gameLayer.position = position;
} else {
_velocity = ccp((position.x - _lastPos.x)/2, (position.y - _lastPos.y)/2);
_lastPos = position;
}
}


This method is working, but i think that in update method if !_isDragging i need to calculate speed of layer with delta isn't it ? So the question is: If my method is good for "after touch scrolling effect, or i need to use delta ? Sorry for english.

• If it works for you, and you haven't encountered any issues, then this isn't really a question for this site. There are rarely, if ever, best implementations. Do what works for you, and fix it or change it if there is a problem. – Evan Aug 13 '14 at 21:05

Instead of updating your velocity by initialising x and y subcomponents and calculating velocity as average of last position and current position, try using this format

CGPoint velocity = CGPointMake(0, 1); // Move up
CGPoint velocity = CGPointMake(0, -1); // Move down
CGPoint velocity = CGPointMake(1, 0); // Move right
CGPoint velocity = CGPointMake(-1, 0); // Move left


To make it move in a specific direction

and update new position by

position = ccpAdd(MainSprite.position, velocity);


And for scrolling effect you need to play with these

Sprite.position = CGPointMake(screenSize.width/2, screenSize.height/2);