2014-10-02 86 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