2014-10-02 88 views
0

我試圖在我的應用程序中使用多點觸控....但我必須在手指在屏幕上移動時顯示條紋......但我只看到一條而不是兩條!我如何創建並移動2條紋?cocos2d多點觸控和CCMotionStreak

這裏是我的代碼:

-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    NSSet *allTouches = [event allTouches]; 

    for (int n=0; n < [allTouches count]; n++) 
    { 
     UITouch *touch = [[allTouches allObjects] objectAtIndex:n]; 
     CGPoint touchLoc = [touch locationInNode:self]; 

     streak = [CCMotionStreak streakWithFade:0.3 minSeg:20 width:13 color:[CCColor colorWithUIColor:[UIColor whiteColor]] textureFilename:@"scia.png"]; 
     [streak setPosition:touchLoc]; 
     [self addChild:streak z:30]; 
    } 
} 

-(void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    NSSet *allTouches = [event allTouches]; 

    for (int n=0; n < [allTouches count]; n++) 
    { 
     UITouch *touch = [[allTouches allObjects] objectAtIndex:n]; 
     CGPoint touchLoc = [touch locationInNode:self]; 

      [streak setPosition:touchLoc]; 
      [self detectObjects:touchLoc]; 
    } 
} 
+0

我看到你只有一個'sreak'對象,所以你總是將改變應用到該對象...與在數組中的最後一個接觸。您可能需要一組條紋,每個觸摸一個,並找到將觸摸與適當條紋相關聯的方式,以便您可以恢復touchMoved中的條紋(可能條紋的userObject屬性可能是所涉及的觸摸)。 – YvesLeBorg 2014-10-02 14:35:30

+0

確切的說,我正在做不同的測試,但我沒有找到如何將觸摸與特定的連線相關聯... – swifferina 2014-10-02 15:05:58

+0

我沒有用過自己,但每個CCNode,因此CCMotionStreak都有一個userObject屬性。你可以將它設置爲與條紋相關的觸摸。在觸摸移動,爲每個觸摸通過你的條紋,直到你找到一個streak.userObject == theTouch; ,然後將該條紋的位置移動到觸摸位置。類似的東西。 ymmv :) – YvesLeBorg 2014-10-02 15:08:29

回答

0

多點觸控內CCLayer您可以通過CCStandardTouchDelegate功能使用多點觸控。 建立在cocos2d 如果您正在尋找水果忍者之刃的效果..使用CCBlade代替CCMotionStreak https://github.com/hiepnd/CCBlade

/// .h File 
CFMutableDictionaryRef map; 

// .m File 
void releaseStreak(CFAllocatorRef allocator, const void *value) 
{ 
    [(CCMotionStreak*)value removeFromParentAndCleanup:YES]; 
} 
CFDictionaryValueCallBacks valueCallbacks = { 
    0, 
    NULL, 
    releaseStreak, 
    NULL, 
    NULL 
}; 

-(id) init 
{ 
    if((self=[super init])) { 
    isTouchEnabled_ = 1; 
    [[[CCDirector sharedDirector] openGLView] setMultipleTouchEnabled:YES]; 
    map = CFDictionaryCreateMutable(NULL,0,NULL,&valueCallbacks); 
    } 
} 

- (void) ccTouchesBegan:(NSSet *) touches withEvent:(UIEvent *) event 
{ 
    for (UITouch *touch in touches) { 
     CCMotionStreak *streak = [CCMotionStreak streakWithFade:0.3 minSeg:20 width:13 color:[CCColor colorWithUIColor:[UIColor whiteColor]] textureFilename:@"scia.png"]; 
     CFDictionaryAddValue(map,touch,streak); 
     CGPoint pos = [touch locationInView:touch.view]; 
     pos = [[CCDirector sharedDirector] convertToGL:pos]; 
     [streak setPosition: pos]; 
     [self addChild:streak z:30]; 
    } 
} 

- (void) ccTouchesMoved:(NSSet *) touches withEvent:(UIEvent *) event{ 
{ 
    for (UITouch *touch in touches) { 
      CGPoint pos = [touch locationInView:touch.view]; 
      pos = [[CCDirector sharedDirector] convertToGL:pos]; 
      CCMotionStreak *_streak = (CCMotionStreak *)CFDictionaryGetValue(map, touch); 
      [streak setPosition:pos]; 
      [self detectObjects:pos]; 
    } 
} 
- (void) ccTouchesEnded:(NSSet *) touches withEvent:(UIEvent *) event{ 
    for (UITouch *touch in touches) { 
     CCMotionStreak *_streak = (CCMotionStreak *)CFDictionaryGetValue(map, touch); 
     CFDictionaryRemoveValue(map,touch); 
     //i already added removeFromParent above no need to add release code. 
    } 
} 

讓我知道如果你上面的代碼工作。

+0

我最初的想法只是使用CCBLade,但與cocos2d v3不兼容! – swifferina 2014-10-06 09:54:21

+0

@swifferina:好的我還沒有在cocos2d v3上試過上面的代碼.. 是上面的答案是否工作? – 2014-10-06 09:55:50