开发者

Rotating a sprite with Touch - Cocos2d

I know this question has been asked several times, trust me I have searched. I have found one answer to rotating a sprite with touch, but there has to be a simpler way.

All I need is for my sprite to rotate with my touch. Max rotation 0 Minimum rotation 0.

I know that I'll need a few checks.

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
    }

Can someone lead me in the right direction to rotating a sprite with touch, with a minimun and maximum rotation?

Here is the code that I think is to complicated or can be accomplished in a simpler way.

-(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(vec开发者_开发问答tor);
    CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);

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

Thanks in advance!


I dont think you can simplify the rotation into anything simpler. I honestly didnt get how you want to both the minimum and the maximum rotations to be zero. They cant be same value. If you want to limit the rotation to a maximum and minimum add the following to the end of your ccTouchesMoved: method:

if (arrowRotation >= maxRot) {

    arrowRotation = maxRot; 
}

else if (arrowRotation <= minRot) {

    arrowRotation = minRot;
}


use KTOneFingerRotationGestureRecognizer...

KTOneFingerRotationGestureRecognizer *rotation;

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

Method

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜