# 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);


This will rule out your isDragging method, also your code not inside the update is working fine, You can use delta also, but the code where you update velocity by average of current and last can go rouge, if user slides entire screen, then average gives center,and there is no change in position, so delta might fail,

METHOD 2 or use advanced function for same thing as (a+b)/2= a+ (b-a)/2

Not sure whether method 2 will work in your case, but using method 1 is safest choice.

Hope this helps...

• And why did first method is better ? i anyway need to calculate in update method velocity to have lerp moving of layer... – gronzzz Aug 13 '14 at 21:38