2011-03-24 57 views
0

我想爲我的遊戲啓用多點觸控。但我不知道如何實施CCMotionStreak的多點觸控版本。每次我用2個手指觸摸2點,就會出現一條緞帶。我想要的是每個手指一條絲帶。 我會更好,如果我可以在粒子系統中做到這一點,但基本上我面臨同樣的問題。 之前有人做過這個嗎?就像水果忍者一樣。CCMotionStreak或粒子系統的cocos2d多點觸控

回答

2

您需要爲每次觸摸創建一個CCMotionStreak節點。

例如:

-(void)createMotionStreak:(NSInteger)touchHash 
{ 
    CCMotionStreak* streak = [CCMotionStreak streakWithFade:1.7f minSeg:10 image:@"arrow.png" width:32 length:32 color:ccc4(255, 0, 255, 255)]; 
    [self addChild:streak z:5 tag:touchHash]; 
} 

-(void)removeMotionStreak:(NSInteger)touchHash 
{ 
    [self removeChildByTag:touchHash cleanup:YES]; 
} 

-(CCMotionStreak*)getMotionStreak:(NSInteger)touchHash 
{ 
    CCNode* node = [self getChildByTag:touchHash]; 
    if(![node isKindOfClass:[CCMotionStreak class]]) { 
     [self createMotionStreak:touchHash]; 
    } 
    return (CCMotionStreak*)node; 
} 

-(void) addMotionStreakPoint:(CGPoint)point on:(NSInteger)touchHash 
{ 
    CCMotionStreak* streak = [self getMotionStreak:touchHash]; 
    [streak.ribbon addPointAt:point width:32]; 
} 

-(CGPoint)locationFromTouch:(UITouch*)touch 
{ 
    CGPoint touchLocation = [touch locationInView: [touch view]]; 
    return [[CCDirector sharedDirector] convertToGL:touchLocation]; 
} 

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSEnumerator* enumerator = [touches objectEnumerator]; 
    UITouch* oneTouch = nil; 
    while (oneTouch = [enumerator nextObject]) { 
     [self addMotionStreakPoint:[self locationFromTouch:oneTouch] on:oneTouch.hash]; 
    } 
} 

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSEnumerator* enumerator = [touches objectEnumerator]; 
    UITouch* oneTouch = nil; 
    while (oneTouch = [enumerator nextObject]) { 
     [self removeMotionStreak:oneTouch.hash]; 
    } 
}