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);
_gameLayer.position = ccpAdd(_gameLayer.position, delta);
}
- (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);
position = ccpAdd(position, _velocity);
_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.