2012-02-12 69 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