2011-04-24 121 views
1

我有兩隻手在屏幕的兩側完全不動,兩隻手指旋轉360度。我想限制拇指旋轉,這意味着我只需要他們能夠像普通拇指一樣旋轉。我是新來的cocos2d,所以任何幫助將不勝感激。以下是我迄今爲止限制精靈旋轉 - 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

在你ccTouchesMoved:withEvent:方法,簡單地檢查,看看是否新的旋轉是將其應用到你的精靈之前給定的閾值內。

最後,在ccTouchesEnded:withEvent:,你可以簡單地設置旋轉值回0:

lthumbRotation = 0; 
rthumbRotation = 0; 
+0

對不起我仍然很新的這一點,你可以解釋通過檢查,看是否新的旋轉是你的意思在將其應用於精靈之前,在給定的閾值內?另外,「lthumbRotation = 0;」工作。出於某種原因,我試圖將位置設置爲0(這是漫長的一天)。非常感謝你。 – Jonathan 2011-04-25 04:20:54