限制精灵旋转 - Cocos2d

时间:2011-04-24 17:10:40

标签: iphone cocos2d-iphone rotation

屏幕两侧有两只手完全不动,两只手拇指旋转360度。我想限制拇指旋转,这意味着我只需要它们就像普通拇指一样旋转。我是cocos2d的新手,所以任何帮助都将不胜感激。这是我到目前为止所拥有的

    #import "cocos2d.h"


    @interface GameScene : CCLayer {

        CGFloat lthumbRotation, rthumbRotation;
        CCSprite *lthumb, *rthumb;
    }

    +(CCScene *) scene;


    @end
------------------------------------
#import "GameScene.h"

@implementation GameScene

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    GameScene *layer = [GameScene node];

    [scene addChild: layer];
    return scene;
}

-(id) init
{
    if ((self = [super init]))
    {
        lthumb = [CCSprite spriteWithFile:@"lthumb.png" rect:CGRectMake(0,0, 145, 59)];
        lthumb.position = ccp(100, 140);
        lthumb.anchorPoint = ccp(0.3, 0.8);

        [self addChild:lthumb z:0];


        rthumb = [CCSprite spriteWithFile:@"rthumb.png" rect:CGRectMake(0,0, 145, 59)];
        rthumb.position = ccp(380, 140);
        rthumb.anchorPoint = ccp(0.7, 0.8);

        [self addChild:rthumb z:0];

        [self scheduleUpdate];

    }
    return self;
}
-(void)update:(ccTime)delta
{
    lthumb.rotation = lthumbRotation;
    rthumb.rotation = rthumbRotation;
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

}

- (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, rthumb.position);
    CGFloat firstRotateAngle = -ccpToAngle(firstVector);
    CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);

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

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

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

}

- (void) dealloc
{
    CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
    [super dealloc];
}

@end

这使得两个拇指同时以向上/向下的角度移动,其中锚点位于拇指的底部(像关节一样)。

一旦触摸结束,我还需要拇指旋转才能重置为0.

提前致谢

1 个答案:

答案 0 :(得分:1)

ccTouchesMoved:withEvent:方法中,只需检查新轮换是否在给定的阈值内,然后再将其应用于精灵。

最后,在ccTouchesEnded:withEvent:中,您只需将旋转值设置回0:

lthumbRotation = 0;
rthumbRotation = 0;
相关问题