Cocos2D中。使用ccTouchMoved在屏幕上拖动CCSprite会使运动变得不稳定

时间:2011-09-19 04:00:35

标签: cocos2d-iphone

我的ccTouchMoved方法存在问题。我有3个ccSprites,我想用手指在屏幕上移动。问题是,如果我将鼠标指针快速移动,那么当拖动图像时,运动是不稳定的,有时甚至会失去ccSprites的“抓地力”。

我错过了什么?

我使用的三种触摸方法有以下任务:

ccTouchBegan:扩大正在触及的ccSprite

ccTouchMoved:移动在屏幕上触摸的ccSprite

ccTouchEnded:将ccSprite缩小到原始大小并停止移动(有没有更好的方法将ccSprite缩小到原始大小?)

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{

CCLOG(@"ccTouchBeganRuby");

CGPoint location = [self convertTouchToNodeSpace: touch];

if(CGRectContainsPoint([animatingRuby1 boundingBox], location)){
    CCLOG(@"ccTouchEndedRubyScaleUp1");
    id ScaleAction1 = [CCScaleBy actionWithDuration:1 scale:1.2f ];
    [animatingRuby1 runAction:ScaleAction1];
}
if(CGRectContainsPoint([animatingRuby2 boundingBox], location)){
    CCLOG(@"ccTouchEndedRubyScaleUp2");
    id ScaleAction2 = [CCScaleBy actionWithDuration:1 scale:1.2f ];
    [animatingRuby2 runAction:ScaleAction2];
}
if(CGRectContainsPoint([animatingRuby3 boundingBox], location)){
    CCLOG(@"ccTouchEndedRubyScaleUp3");
    id ScaleAction3 = [CCScaleBy actionWithDuration:1 scale:1.2f ];
    [animatingRuby3 runAction:ScaleAction3];
}

return YES;

}

- (void)ccTouchMoved:(UITouch *)touches withEvent:(UIEvent *) event{

CCLOG(@"ccTouchMovedRuby");

if (CGRectContainsPoint([animatingRuby1 boundingBox], location)) {
    CCLOG (@"moveRubies1");
    if((CGRectContainsPoint([animatingRuby2 boundingBox], location))|| (CGRectContainsPoint([animatingRuby1 boundingBox], location))) {
        return;
    }
    animatingRuby1.position = location;

}
if (CGRectContainsPoint([animatingRuby2 boundingBox], location)) {
    CCLOG (@"moveRubies2");
    if((CGRectContainsPoint([animatingRuby1 boundingBox], location))|| (CGRectContainsPoint([animatingRuby3 boundingBox], location))) {
        return;
    }
    animatingRuby2.position = location;
}
if (CGRectContainsPoint([animatingRuby3 boundingBox], location)) {
    CCLOG (@"moveRubies3");
    if((CGRectContainsPoint([animatingRuby1 boundingBox], location))|| (CGRectContainsPoint([animatingRuby2 boundingBox], location))) {
        return;
    }
    animatingRuby3.position = location;

}
[self collidableWithEachOther:location];

}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *) event{
CGSize screenSize = [[CCDirector sharedDirector] winSize]; 
CCLOG(@"ccTouchEndedRuby");
CGPoint location = [self convertTouchToNodeSpace: touch];

if(CGRectContainsPoint([animatingRuby1 boundingBox], location)){
    CCLOG(@"ccTouchEndedRubyScaleDown1");
    [animatingRuby1 setScaleX:screenSize.width/14850.0f]; 
    [animatingRuby1 setScaleY:screenSize.height/9552.0f];

}
if(CGRectContainsPoint([animatingRuby2 boundingBox], location)){
    CCLOG(@"ccTouchEndedRubyScaleDown2");
    [animatingRuby2 setScaleX:screenSize.width/14850.0f]; 
    [animatingRuby2 setScaleY:screenSize.height/9552.0f];
}
if(CGRectContainsPoint([animatingRuby3 boundingBox], location)){
    CCLOG(@"ccTouchEndedRubyScaleDown3");
    [animatingRuby3 setScaleX:screenSize.width/14850.0f]; 
    [animatingRuby3 setScaleY:screenSize.height/9552.0f];
}

}

1 个答案:

答案 0 :(得分:2)

将精灵中的矢量移到触摸位置并将其设置为精灵的方向,同时限制其速度。这种方式变化不会那么快速和剧烈,运动看起来会更顺畅。不要求触摸位置始终位于精灵上方,只需跟踪是否正在进行拖动操作,以及拖动哪个精灵。