如何用180度cocos2d-iphone旋转精灵?

时间:2013-04-18 13:19:49

标签: iphone ios cocos2d-iphone

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
    {
        CGPoint location = [touch locationInView:[touch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];

        shootVector = ccpSub(location, cannon.position);
        CGFloat shootAngle = ccpToAngle(shootVector);
        cocosAngle = CC_RADIANS_TO_DEGREES(-1 * shootAngle);

        CGFloat rotateDiff = cocosAngle;    
        if (rotateDiff > 180)
            rotateDiff -= 360;
        if (rotateDiff < -180)
            rotateDiff += 360;    

        CGFloat rotateSpeed = 360; 
        rotateDuration = fabs(rotateDiff / rotateSpeed);

        [cannon runAction:[CCSequence actions:
                           [CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle],
                           [CCCallFunc actionWithTarget:nil selector:nil],
                           nil]];

        top.rotation = cannon.rotation;
    }

上面的代码导致触摸旋转移动360度,而我想在180度或更小的程度旋转我的大炮..

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

使用 CCRotateBy 命令代替 CCRotateTo 将导致旋转一定数量而不是转到某个位置。以与CCRotateTo相同的方式调用它,如下所示:

[CCRotateBy actionWithDuration:rotateDuration angle:180]