如何在UIGestureRecogniser Cocos2d中反转Y轴

时间:2013-03-12 18:42:22

标签: cocos2d-iphone uigesturerecognizer

我有以下代码:

- (void)panning:(UIPanGestureRecognizer *)recognizer {

CGPoint position;
//CGPoint velocity;
//velocity = [recognizer velocityInView:[CCDirector sharedDirector].view];

switch( recognizer.state ) {
    case UIGestureRecognizerStatePossible:
        break;

    case UIGestureRecognizerStateBegan:
    {
        position = [recognizer locationInView:[CCDirector sharedDirector].view];

        for (CCSprite *sprite in movableSprites) {
            if (CGRectContainsPoint(sprite.boundingBox, position)) {
                selSprite = sprite;
                break;
            }
        }
        break;
    }
    case UIGestureRecognizerStateChanged:
    {
        position = [recognizer locationInView:[CCDirector sharedDirector].view];
        CGPoint yInvertedPosition = CGPointMake(position.x, -position.y);
        [selSprite setPosition:position];
        break;
    }
    case UIGestureRecognizerStateFailed:
        break;

    case UIGestureRecognizerStateEnded:
        break;

    case UIGestureRecognizerStateCancelled:
        break;
}

}

当我拖动精灵时,它在X轴上移动正常,但它在Y轴上反转。

我该如何转换它?

 CGPoint yInvertedPosition = CGPointMake(position.x, -position.y);

这不起作用

1 个答案:

答案 0 :(得分:0)

您只需将您的位置转换为GL坐标。

case UIGestureRecognizerStateBegan:
{
    position = [recognizer locationInView:[CCDirector sharedDirector].view];
    position = [[CCDirector sharedDirector] convertToGL:position];

    for (CCSprite *sprite in movableSprites) {
        if (CGRectContainsPoint(sprite.boundingBox, position)) {
            selSprite = sprite;
            break;
        }
    }
    break;
}
case UIGestureRecognizerStateChanged:
{
    position = [recognizer locationInView:[CCDirector sharedDirector].view];
    position = [[CCDirector sharedDirector] convertToGL:position];
    [selSprite setPosition:position];
    break;
}