cocos2d多点触控和CCMotionStreak

时间:2014-10-02 14:18:20

标签: cocos2d-iphone

我试图在我的应用程序中使用多点触控....但是当手指在屏幕上移动时我必须显示条纹...但我只看到一条条纹,而不是两条条纹!如何创建和移动2条条纹?

这是我的代码:

-(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];
    }
 }

1 个答案:

答案 0 :(得分: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.
    }
}

如果上述代码适合您,请告诉我。