0

I was wondering if somebody could help me out and possibly point me in the right direction.

Here's my code:

- (void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:[touch view]];
    point = [[CCDirector sharedDirector] convertToGL:point];
    sprite.position = ccp (sprite.x, point.y);
}

Here's what I'm trying to do.

I want to click and drag the sprite up and down on the Y axis only. I want the sprite to at close to the top of the screen, which is in portrait mode.

I don't want the 'popping' or 'skipping' around every time you touch the screen in a different spot.

Any pointers would be greatly appreciated.

1 Answer 1

0

Use vectors. This codesample uses the targeted touch delegate instead of the standard. Make newPos x property always the same to achieve your behavior.

- (void)panForTranslation:(CGPoint)translation {    
    if (selSprite) {
        CGPoint newPos = ccpAdd(selSprite.position, translation);
        selSprite.position = newPos;
    }
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {       
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

    CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
    oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];

    CGPoint translation = ccpSub(touchLocation, oldTouchLocation);    
    [self panForTranslation:translation];    
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.