在cocos2d - iPhone中平滑拖动Sprite

5 iphone cocos2d-iphone

我已经在sprite对象上实现了一个拖动,如下所示.

-(BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch * touch = [touches anyObject];
CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView:touch.view]];    
[diskSprite setPosition:ccp(location.x , location.y )];
return kEventHandled;
}
Run Code Online (Sandbox Code Playgroud)

但是这种拖动并不顺畅.....当我用拇指快速拖动物体离开路径时.

谢谢

baa*_*aal 10

可能有点迟了但我正在寻找类似的东西.我发现这个很棒的教程解释了一切:http: //www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d

- (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);    
    CGPoint newPos = ccpAdd(mySpriteToMove.position, translation);
    mySpriteToMove.position = newPos;
}
Run Code Online (Sandbox Code Playgroud)


nas*_*ash 0

我不太确定“路径留下的对象”是什么意思。我想你的意思是,如果你在屏幕上以弧形或圆形拖动手指,精灵会从一个点“跳跃”到另一个点,而不是精确地跟随你的手指。它是否正确?

如果您希望精灵遵循精确的路径,则必须创建一条路径,然后将精灵设置为遵循该路径。您现在要做的只是将精灵的位置设置为触摸位置,但“拖动”触摸不会为其触摸的每个像素创建事件。
为接收到的触摸创建路径相当容易,并且可以在各处找到代码示例。但是,如果精灵的速度(以每帧像素为单位)太高,即使您使用平滑路径,您也总会看到它“跳跃”。

示例:
您可以在圆形路径上为精灵设置动画。如果您设置动画以在 1 秒内完成路径,您可能会看到流畅的动画。但如果它以高速运行,比如 4 帧中的一个完整的圆,你只会在 4 个位置看到你的精灵,而不是一个平滑的圆。如果你想“纠正”这个问题,你需要研究混合,或者确定可接受运动的最大速度,并在精灵太快时放慢速度。

我希望这能回答你的问题。如果不清楚,请随时编辑您的问题,或在我的答案中添加评论。