【问题标题】:Rotating a sprite with Touch - Cocos2d使用 Touch 旋转精灵 - Cocos2d
【发布时间】:2011-09-20 05:00:44
【问题描述】:

我知道这个问题已经被问过好几次了,相信我我已经搜索过了。我找到了一个用触摸旋转精灵的答案,但必须有一种更简单的方法。

我所需要的只是让我的精灵随着我的触摸而旋转。最大旋转 0 最小旋转 0。

我知道我需要进行一些检查。

int maxRot = 0;
int minRot = 0;

    if (arrowRotation > maxRot)
    {
    //do or don't do something
    } else if (arrowRotation < minRot)
    {
    //do or don't do something
    }

有人可以引导我以正确的方向通过触摸旋转精灵,最小和最大旋转吗?

这是我认为比较复杂或可以用更简单的方式完成的代码。

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    //acquire the previous touch location
    CGPoint firstLocation = [touch previousLocationInView:[touch view]];
    CGPoint location = [touch locationInView:[touch view]];

    //preform all the same basic rig on both the current touch and previous touch
    CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
    CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];

    CGPoint firstVector = ccpSub(firstTouchingPoint, _arrow.position);
    CGFloat firstRotateAngle = -ccpToAngle(firstVector);
    CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);

    CGPoint vector = ccpSub(touchingPoint, _arrow.position);
    CGFloat rotateAngle = -ccpToAngle(vector);
    CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);

    //keep adding the difference of the two angles to the dial rotation
    arrowRotation += currentTouch - previousTouch;
}

提前致谢!

【问题讨论】:

  • 添加一个您认为太复杂的解决方案的链接 - 这可能是唯一的方法;)
  • 我用代码更新了我的问题!
  • maxRot 和 minRot 都是 = 0 是什么意思。
  • 箭头可以转动的最小和最大度数
  • 您的代码有效吗?看起来不错。如果它运作良好,那么不要担心试图让它“更简单”。如果有问题,那么您可以对其进行调整,直到它达到您想要的为止。

标签: iphone cocos2d-iphone


【解决方案1】:

我认为您无法将旋转简化为更简单的方法。老实说,我不明白您希望最小和最大旋转都为零的方式。它们不能是相同的值。如果您想将旋转限制在最大值和最小值,请将以下内容添加到 ccTouchesMoved: 方法的末尾:

if (arrowRotation >= maxRot) {

    arrowRotation = maxRot; 
}

else if (arrowRotation <= minRot) {

    arrowRotation = minRot;
}

【讨论】:

  • 最小和最大旋转这里只是例子。它们都不会是 0。
【解决方案2】:

使用KTOneFingerRotationGestureRecognizer...

KTOneFingerRotationGestureRecognizer *rotation;

rotation = [[KTOneFingerRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotating:)];
[[self Wheel] addGestureRecognizer:rotation];
[rotation release];

方法

- (void)rotating:(KTOneFingerRotationGestureRecognizer *)recognizer {
    view = [recognizer view];
    if(isRotating == FALSE) {
        [view setTransform:CGAffineTransformRotate([view transform], [recognizer rotation])];
    }
}

【讨论】:

  • 我无法将转换设置为 CCsprite。
猜你喜欢
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多