control max speed in sprite kit

Viewed 3550

I'm trying to control the max speed of my character in my game. When I move him I use this:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


UITouch *touch = [touches anyObject];

CGPoint positionInScene = [touch locationInNode:self];
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
CGPoint posicionHero = [self childNodeWithName:@"hero"].position;
SKSpriteNode *touchHero = (SKSpriteNode *)[self nodeAtPoint:posicionHero];
if((touchedNode !=touchHero) //jump forward
   && (positionInScene.x > [self childNodeWithName:@"Hero"].position.x)
   && (positionInScene.y > [self childNodeWithName:@"Hero"].position.y)
   )
{
    [[self childNodeWithName:@"Hero"].physicsBody applyImpulse:CGVectorMake(5,0)];
    [[self childNodeWithName:@"Hero"].physicsBody applyImpulse:CGVectorMake(0, 10)];
    NSLog(@"jump forward done");
}

But the problem is the speed is not limited, and when i do this two or three times the character goes very fast. I tried with a lot of properties (velocity, angular speed, etc) and i didn't found anything satisfactory. Does anybody know how to set a speed limit or any "trick" to control the max speed of a character?

3 Answers
Related