像Cocos2D中的Car Steering一样进行CCSprite旋转

时间:2012-08-26 03:13:21

标签: ios cocos2d-iphone rotation ccsprite

有没有办法将CCSprite旋转到像汽车转向一样?如果我用CCSprite释放我的触摸,它应该像转向一样以平滑的方式自动返回到原来的位置!我使用下面的代码,

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

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


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

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

        CGPoint vector = ccpSub(touchingPoint, Steering.position);
        CGFloat rotateAngle = -ccpToAngle(vector);
        CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
        dialrotation+=currentTouch-PreviousTouch;
        sprite.rotation=dialrotation;
        }

这会平滑地旋转我的CCSprite,但我不知道如何返回原来的位置,如Car Steering.Is有什么方法可以在Cocos2D中这样做吗?

1 个答案:

答案 0 :(得分:0)

ccTouchesBegan方法中,使用此存储值,在您的精灵的ccTouchesEnded运行CCRotateTo操作中,将sprite.rotation的值存储在某处。