1

我正在尝试使用 cocos2d sprites 确定用户相对于屏幕上角色的触摸位置。当用户单击精灵左侧时,我希望精灵向左运行,反之亦然。我的问题是,当用户单击一侧并移动到另一侧而不松开触摸时(cctouchended 不会触发),精灵会继续运行,但朝向错误的方向。我将在哪里执行检查(以及如何)以确定用户的触摸是否已移动到角色的另一侧?

我尝试的当前代码:

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView: [touch view]];  
    CGPoint moveDifference = ccpSub(touchLocation, _character.position);
    if (moveDifference.x < 0) {
      _character.flipX = YES;
    } else {
      _character.flipX = NO;
    }   
}

谢谢。

4

3 回答 3

0

您必须记下您当前的位置,并使用此功能 ccp(location.x, location.y) 您可以将精灵移动到您想要的方向

于 2011-01-25T18:03:15.037 回答
0

您需要像这样转换您的触摸位置。

CGPoint location = [touch locationInView: [touch view]];    
location = [[CCDirector sharedDirector] convertToGL: location];

然后您在 cocos2d 中的视图上获得正确的触摸位置。

于 2011-07-21T06:41:42.163 回答
0

你不能把它放在 ccTouchesBegan 中吗?这会解决它,我想...

于 2010-12-13T12:13:00.123 回答