简体   繁体   English

在cocos2d - iPhone中平滑拖动Sprite

[英]Smoothly drag a Sprite in cocos2d - iPhone

I have implemented a drag on a sprite object as follows.. 我已经在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;
}

but this dragging is not smooth..... when i drag fast with my thumb the object left from the path. 但是这种拖动并不顺畅.....当我用拇指快速拖动物体离开路径时。

Thanks 谢谢

Probably a little bit late but I was searching for a similar thing. 可能有点迟了但我正在寻找类似的东西。 I found this great Tutorial which explained everything: http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d 我发现这个很棒的教程解释了一切: 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;
}

I had this same issue with my game. 我的游戏也有同样的问题。 Dragging operations appeared jerky. 拖动操作显得生涩。 I believe the reason is that touch events aren't generated fast enough to give a smooth appearance. 我相信原因是触摸事件的生成速度不够快,无法提供平滑的外观。

To solve the problem I smoothed the motion out by running an action on the sprite toward the desired location, instead of setting the position immediately. 为了解决这个问题,我通过在精灵上向所需位置运行动作来平滑运动,而不是立即设置位置。

I'm not exactly sure what you mean by "the object left from the path". 我不完全确定你的意思是“路径留下的物体”。 I suppose what you mean is that if you drag your finger over the screen in an arc or circle, that the sprite will "jump" from point to point, instead of follow your finger precisely. 我想你的意思是,如果你用手指在屏幕上以弧形或圆形拖动,那么精灵将从一点到另一点“跳跃”,而不是精确地按照你的手指。 Is this correct? 它是否正确?

If you want your sprite to follow an exact path, you will have to create a path and then set the sprite to follow it. 如果你希望你的精灵遵循一个确切的路径,你将必须创建一个路径,然后设置精灵跟随它。 What you do now is simply set the sprite's position to the touch position, but a "dragged" touch will not create an event for every pixel it touches. 您现在所做的只是将精灵的位置设置为触摸位置,但“拖动”触摸不会为其触摸的每个像素创建事件。
It is fairly easy to create a path for touches received, and code samples can be found here and there. 为接收的触摸创建路径相当容易,并且可以在这里和那里找到代码样本。 However, if the sprite's speed (in pixels per frame) is too high, you will always see it "jump", even if you use a smooth path. 但是,如果精灵的速度(以每帧像素为单位)太高,即使使用平滑路径,也总会看到它“跳跃”。

Example: 例:
You can animate a sprite over a circular path. 您可以在圆形路径上为精灵设置动画。 If you animate this to complete the path in 1 second, you will likely see smooth animation. 如果您设置动画以在1秒内完成路径,您可能会看到平滑的动画。 But if it runs at a high speed, like a full circle in 4 frames, you will just see your sprite at 4 places, not in a smooth circle. 但是如果它以高速运行,就像4帧中的整圆一样,你只会在4个位置看到你的精灵,而不是在光滑的圆圈中。 If you wish to 'correct' that, you will need to look into blending, or determine what the maximum speed is for acceptable motion, and slow your sprite down when it's too fast. 如果你想“纠正”那个,你需要考虑混合,或者确定可接受运动的最大速度是什么,并在速度过快时减慢你的精灵速度。

I hope that answers your question. 我希望能回答你的问题。 If it's not clear, feel free to edit your question, or add a comment to my answer. 如果不清楚,请随时编辑您的问题,或在我的答案中添加评论。

look here, what I suggest you to try in such case: 看看这里,我建议你在这种情况下尝试:

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

        [_sprite stopAllActions];

        id move = [CCEaseIn actionWithAction:[CCMoveTo actionWithDuration:0.1 position:ccp(location.x, _sprite.position.y)]];
        [_sprite runAction:move];
    }
} 

And it really work smoothly. 它确实很顺利。

I enjoyed this easy way. 我喜欢这种简单的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM