开发者

Rotating Sprite with Touch - Cocos2d

Does anyone have a better way to rotate a sprite with one finger? My problem is that I can not get the sprite to stop rotating after it has been fully rotated twice and I flip my screen 180 degree periodcally (self.rotation = 180;) then flip it back (self.rotation = 0). But, when I flip it to 180 degrees the sprite will not rotate properly.

Any one have any better ideas than this?

CGFloat gRotation;

- (void)update:(ccTime)delta
{
    g.rotation = gRotation;
}
    - (voi开发者_如何学JAVAd)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInView:[touch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];

        if (CGRectContainsPoint(g.boundingBox, location))
        {
            CGPoint firstLocation = [touch previousLocationInView:[touch view]];
            CGPoint location = [touch locationInView:[touch view]];

            CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
            CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];

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

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

            gRotation += currentTouch - previousTouch;
        }
    }

Thanks

EDIT:

I went in to GameConfig.h and changed #define GAME_AUTOROTATION kGameAutorotationUIViewController to #define GAME_AUTOROTATION kGameAutorotationNone

Then, went in to AppDelegate.m and changed #if GAME_AUTOROTATION == kGameAutorotationUIViewController to #if GAME_AUTOROTATION == kGameAutorotationNone

That fixed the sprite's rotation when I flipped the screen, but I am still having problems stopping the sprite's rotation after two full rotations.


add a new line at the end:

gRotation += currentTouch - previousTouch;
gRotation = fmod(gRotation,360.0); // <<< fix the angle

this maybe fix your problem, because the angle will stay in the 360 range

Other way to rotate with one finger is the UIPanGestureRecognizer (but you still need to keep the angle in the 360 range):

UIPanGestureRecognizer *gestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:layer action:@selector(handlePanFrom:)] autorelease];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:gestureRecognizer];

...

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer 
{
  CGPoint translation = [recognizer translationInView:recognizer.view];
  ...
}

Take a look at this tutorial for more details (its on drag, but show how to do the pan gesture):

http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜