用鼠标和Cocos2d拖动精灵

时间:2013-01-09 13:29:36

标签: objective-c cocos2d-iphone ccsprite

我写了一些代码,我可以选择精灵,我可以拖动它,但是一旦我拖动它,它会离开鼠标光标一些像素,之后我可以控制它,但它是一些像素的距离从我的鼠标cursos ..我的CGPoint转换似乎有问题,或者我不知道,这是我的代码

- (void)selectSpriteForTouch:(CGPoint)touchLocation {
    CCSprite * newSprite = nil;
    for (CCSprite *sprite in movableSprites) {
        if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {
            newSprite = sprite;
            NSLog(@"palieciau");
            break;
        }
    }
    if (newSprite != selSprite) {
        [selSprite stopAllActions];
        [selSprite runAction:[CCRotateTo actionWithDuration:0.1 angle:0]];
        CCRotateTo * rotLeft = [CCRotateBy actionWithDuration:0.1 angle:-4.0];
        CCRotateTo * rotCenter = [CCRotateBy actionWithDuration:0.1 angle:0.0];
        CCRotateTo * rotRight = [CCRotateBy actionWithDuration:0.1 angle:4.0];
        CCSequence * rotSeq = [CCSequence actions:rotLeft, rotCenter, rotRight, rotCenter, nil];
        [newSprite runAction:[CCRepeatForever actionWithAction:rotSeq]];
        selSprite = newSprite;
    }
}

- (BOOL)ccMouseDown:(NSEvent*)event {
    CCSprite * newSprite = nil;
    CGPoint clickLocation = [[CCDirector sharedDirector] convertEventToGL:event];
    for (CCSprite *sprite in movableSprites) {
        if (CGRectContainsPoint(sprite.boundingBox, clickLocation)) {
            newSprite = sprite;
            break;
        }
    }
    if (newSprite != selSprite) {
        [selSprite stopAllActions];
        [selSprite runAction:[CCRotateTo actionWithDuration:0.1 angle:0]];
        CCRotateTo * rotLeft = [CCRotateBy actionWithDuration:0.1 angle:-4.0];
        CCRotateTo * rotCenter = [CCRotateBy actionWithDuration:0.1 angle:0.0];
        CCRotateTo * rotRight = [CCRotateBy actionWithDuration:0.1 angle:4.0];
        CCSequence * rotSeq = [CCSequence actions:rotLeft, rotCenter, rotRight, rotCenter, nil];
        [newSprite runAction:[CCRepeatForever actionWithAction:rotSeq]];
        selSprite = newSprite;
    }
}


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

-(BOOL)ccMouseDragged:(NSEvent *)event {
    CGPoint point = [[CCDirector sharedDirector] convertEventToGL:event];
    CGPoint mouseLocation = [self convertToNodeSpace:point];
    CGPoint translation = ccpSub(point, oldMouseLocation_);
    [self panForTranslation:translation];
    oldMouseLocation_ = point;
}

2 个答案:

答案 0 :(得分:0)

你应该考虑的一些事情:

  1. 在开始拖动时,您应该将oldMouseLocation设置为启动拖动的点
  2. 确保将精灵从中心拖出。例如,如果您正在移动容器,则可以从左下角拖动它们
  3. 在您的ccMouseDown中,您没有使用convertToNodeSpace函数
  4. 您可以考虑在鼠标位置移动拖动的精灵(selSprite),而不是使用[oldMouseLocation - mouseLocation]距离进行翻译。 (可选)您可以应用在[开始拖动点 - 拖动精灵位置]距离
  5. 上计算的偏移量

答案 1 :(得分:0)

我得到了它,谢谢。

- (BOOL)ccMouseDragged:(NSEvent *)event 
{
    CGPoint point = [[CCDirector sharedDirector] convertEventToGL:event];
    CGPoint mouseLocation = [self convertToNodeSpace:point];
    CGPoint translation = (mouseLocation);
    [self panForTranslation:translation];
    return YES;
}

-(void)panForTranslation:(CGPoint)translation 
{
    if (first) 
    {
        NSLog(@"%f, %f",translation.x, translation.y);
        deb1.position = ccp( translation.x, translation.y );
    }
    if (second) 
    {
        NSLog(@"%f, %f",translation.x, translation.y);
        deb2.position = ccp( translation.x, translation.y );
    }
    if (third) 
    {
        NSLog(@"%f, %f",translation.x, translation.y);
        deb3.position = ccp( translation.x, translation.y );
    }
    else 
    {
        NSLog(@"not in sprite's rect");
    }

}