繁体   English   中英

CCSprite旋转,例如Cocos2D中的汽车转向

[英]CCSprite rotation like Car Steering in Cocos2D

有什么方法可以使CCSprite旋转为类似于汽车转向系统吗?如果我松开CCSprite的触摸,它将自动以平滑的方式自动返回其原始位置,就像转向一样!Im使用以下代码,

-(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一样返回到其原始位置。在Cocos2D中有什么方法可以这样做吗?

ccTouchesBegan方法中,将ccTouchesBegan的值存储在某个位置,在ccTouchesEnded使用此存储的值对您的sprite运行CCRotateTo操作。

暂无
暂无

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

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