2012-02-12 68 views
2

我一直在處理這個問題很長一段時間。 我想要做的是通過在屏幕左側或右側觸摸來控制CCSprite的右/左移動。精靈運動的MultiTouch控件 - cocos2d

如果在每次觸摸後擡起手指,這樣做不成問題。但我想要的最好用一個例子來描述: 玩家觸摸屏幕的左側,精靈向左移動。現在玩家(雖然還在觸摸左側)觸及右側......精靈現在應該向右移動。現在玩家的左手邊和右手邊都有一個手指,如果他現在從右側擡起手指,精靈應該再次向左移動。

這是我現在有:

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSSet *allTouches = [event allTouches]; 
    UITouch * touch = [[allTouches allObjects] objectAtIndex:0]; 
    CGPoint location = [touch locationInView: [touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 

    if (location.x < 240) { 
     [player walk:kkMoveLeft]; 
    } else if (location.x > 240) { 
     [player walk:kkMoveRight]; 
    } 

    //Swipe Detection Part 1 
    firstTouch = location; 
} 

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSSet *allTouches = [event allTouches]; 
    UITouch * touch = [[allTouches allObjects] objectAtIndex:0]; 
    CGPoint location = [touch locationInView: [touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 

    //Swipe Detection Part 2 
    lastTouch = location; 

    float swipeLength = ccpDistance(firstTouch, lastTouch); 

    if (firstTouch.y < lastTouch.y && swipeLength > 60) { 
     [player jump:kkJumpUp]; 
    } else if (firstTouch.y > lastTouch.y && swipeLength > 60){ 
     [player jump:kkJumpDown]; 
    } 

    [player endWalk]; 

} 

我會很感激,如果有人可以告訴我如何去了解這一點。謝謝 。

更新我的解決方案:

//1. Enable multitouch in the appDelegate 
[glView setMultipleTouchEnabled:YES]; 

//2. Create an Array to keep track of active touches 
touchArray = [[NSMutableArray alloc] init]; 

//3. Touch Methods 
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    for (UITouch *touch in touches) { 
     if (![touchArray containsObject:touch]) { 
      [touchArray addObject:touch]; 
      CGPoint location = [touch locationInView:[touch view]]; 
      location = [[CCDirector sharedDirector] convertToGL:location]; 
      CCLOG(@"start: %f", location.x); 
      if (location.x < 240) { 
       [player walk:kkMoveLeft]; 
      } else if (location.x > 240) { 
       [player walk:kkMoveRight]; 
      } 
     } 
    } 

} 

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

    for (UITouch *touch in touches) { 
     [touchArray removeObject:touch]; 
     CGPoint location = [touch locationInView:[touch view]]; 
     location = [[CCDirector sharedDirector] convertToGL:location]; 
     CCLOG(@"end: %f", location.x); 
    } 

    for (UITouch *touch in touchArray) { 
     CGPoint location = [touch locationInView:[touch view]]; 
     location = [[CCDirector sharedDirector] convertToGL:location]; 
     CCLOG(@"still: %f", location.x); 
     if (location.x < 240) { 
      [player walk:kkMoveLeft]; 
     } else if (location.x > 240) { 
      [player walk:kkMoveRight]; 
     } 
    } 


    if ([touchArray count] == 0) { 
     [player endWalk]; 
    } 

} 

回答

1

如果我理解正確的話你的問題,你應該做的是:

  1. ccTouchesEnded

    ,而不是簡單地調用endWalk,查看任何觸摸仍然存在(這樣做,你可以遍歷allTouches);

  2. 在適當的情況下(即,觸摸仍在激活玩家),請致電startWalk

  3. 如果沒有觸摸,請撥打endWalk

的代碼將類似於你有什麼ccTouchesBegan,只有你遍歷(我不知道什麼allTouches包含索引0 touchesEnded)。

OLD答:

你不是說你是如何現在處理任何接觸。在任何情況下,要走的路是定義ccTouches*方法(與ccTouch*),其中*可以是:BeganMoved,Ended

-(void)tccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch *touch = [[event allTouches] anyObject]; 
CGPoint location = [touch locationInView:[touch view]]; 
location = [[CCDirector sharedDirector] convertToGL:location]; 
    ... 
} 
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    ... 
} 
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    ... 
} 

記住的touchesBegan在檢測到的每個觸摸解僱。因此,如果您想知道當前所有觸摸的狀態,則必須使用allTouches

也看看this post from iphonesdk,我發現這些方法中觸摸的語義有洞察力。

+0

接觸工作是不是我的問題(請參閱更新的問題)。我遇到的問題是玩家在用兩根手指觸摸屏幕後繼續移動,然後擡起並保持播放器移動。 – 2012-02-12 17:31:36

+0

在此期間我自己找到了一個解決方案...但感謝您的努力,因爲您的更新答案實際上是我最終以我接受您的答案;-) – 2012-02-12 23:27:56