2011-03-15 161 views
3

我需要幫助確定用手指旋轉精靈。精靈旋轉得很好,但是在初始觸摸我的手指時,它本身以某種方式旋轉了幾度。用手指在Cocos2d中旋轉精靈

此外,旋轉僅在手指圍繞精靈中心旋轉時才起作用。

我試圖模擬自行車車輪,並有一個齒輪精靈和一個踏板精靈作爲齒輪精靈的孩子。當我觸碰踏板並旋轉它時,我想讓自行車輪旋轉。我還沒有那麼遠。現在我正試圖弄清楚如何擺脫這種裝備的初始轉變。

下面是完整的代碼

#import "BicycleScene.h" 

@implementation BicycleScene 

+(id) scene 
{ 
    // 'scene' is an autorelease object. 
    CCScene *scene = [CCScene node]; 

    // 'layer' is an autorelease object. 
    BicycleScene *layer = [BicycleScene node]; 

    // add layer as a child to scene 
    [scene addChild: layer]; 

    // return the scene 
    return scene; 
} 

-(id) init 
{ 
    if ((self=[super init])) { 

     CGSize winSize = [[CCDirector sharedDirector] winSize]; 

     //place the bicycle sprite 
     bicycleSprite = [CCSprite spriteWithFile:@"bike_gear.png"]; 
     bicycleSprite.position = ccp(bicycleSprite.contentSize.width/2 + 100, winSize.height/2); 
     [self addChild:bicycleSprite z:1]; 

     //place the pedal sprite 
     pedalSprite = [CCSprite spriteWithFile:@"bike_pedal.png"]; 
     [bicycleSprite addChild:pedalSprite z:1]; 

     pedalSprite.position = ccp(150, 15); 

     //enable touch 
     self.isTouchEnabled = YES; 

     [self schedule:@selector(gameLoop:) interval:.1/100]; 

    } 

    return self; 
} 

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

    NSLog(@"Touch begun"); 

} 

-(void)gameLoop:(ccTime) dt{ 

    bicycleSprite.rotation = cocosAngle; 
} 

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

    NSLog(@"Touch moved"); 

    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:[touch view]]; 
    CGPoint previousLocation = [touch previousLocationInView:[touch view]]; 

    CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location]; 
    CGPoint previousTouchingPoint = [[CCDirector sharedDirector] convertToGL:previousLocation]; 

    CGPoint vector = ccpSub(touchingPoint, bicycleSprite.position); 
    CGFloat rotateAngle = -ccpToAngle(vector); 

    previousAngle = cocosAngle; 
    cocosAngle = CC_RADIANS_TO_DEGREES(rotateAngle); 

    //bicycleSprite.rotation = cocosAngle; 
} 

@end 

我稍微感到困惑,如果行:

CGPoint vector = ccpSub(touchingPoint, bicycleSprite.position); 

實際上應該是:

CGPoint vector = ccpSub(touchingPoint, previousTouchingPoint); 

我試過,很好,但它沒沒有工作。

我還上傳了我的完整xcodeproj到4shared的人誰願意到這裏看看:http://www.4shared.com/file/5BaeW4oe/Healthy.html

+0

當您使用previousTouchingPoint而不是bicycleSprite.position時會發生什麼?同樣的行爲?沒有? – lins314159 2011-03-15 07:25:25

+0

當我使用previousTouchingPoint而不是bicycleSprite.position時,齒輪也會跳躍很多,並且齒輪緩慢旋轉時也會出現很多閃爍現象。 – tbag 2011-03-15 15:43:53

+0

我剛剛測試了上面的代碼,它完成了我期望的操作。問題是touchBegan沒有移到正確的角度?或者它在初始移動時跳躍而不是逐漸移動到一個角度? – lins314159 2011-03-16 08:40:13

回答

9
@interface MainScene : CCLayer { 
    CCSprite *dial; 

    CGFloat dialRotation; 

} 

+ (id)scene; 

@end 
--------------------------------- 
@implementation MainScene 

+ (id)scene 
{ 
    CCScene *scene = [CCScene node]; 
    CCLayer *layer = [MainScene node]; 
    [scene addChild:layer]; 

    return scene; 
} 

- (id)init 
{ 
    if((self = [super init])) { 
     CCLOG(@"MainScene init"); 

     CGSize size = [[CCDirector sharedDirector]winSize]; 

     dial = [CCSprite spriteWithFile:@"dial.png"]; 
     dial.position = ccp(size.width/2,dial.contentSize.height/2); 
      [self addChild:dial]; 

     self.isTouchEnabled = YES; 
     [self scheduleUpdate]; 

    } 
    return self; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

- (void)update:(ccTime)delta 
{ 
    dial.rotation = dialRotation; 
} 

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

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

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

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

} 

編輯:我很抱歉,我不能讓這個留在代碼模式。但這是解決方案,應該很容易在代碼中實現。乾杯!

+0

要將文本格式設置爲代碼,請選擇所需文本,然後單擊帶花括號的按鈕。 – lins314159 2011-03-28 22:03:40

+0

不錯 - 謝謝 – Chev 2011-07-06 10:42:06

0

ccTouchesBegan,使用相同的代碼,你在ccTouchesMoved已經得到了確定角度,然後移動使用CCRotateTo到那個角度。您需要一些處理用戶移動他的手指,而CCRotateTo處於活動狀態,可能會停止當前操作並踢開另一個移動到新角度。