cocos2d确定屏幕上用户正在触摸的位置

问题描述:

我正在尝试确定用户触摸的位置与使用cocos2d sprite的屏幕上的字符有关。当用户点击精灵的左侧时,我想让精灵向左运行,反之亦然。 我的问题是,当用户点击一边并移动到另一边而没有放下触摸(cctouchended不起作用)时,精灵将继续运行,但面向错误的方向。我将在哪里执行检查(以及如何)以确定用户的触摸是否已经移动到角色的另一侧?cocos2d确定屏幕上用户正在触摸的位置

当前的代码,我想:

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

感谢。

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

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

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

难道你不能只是把它放在ccTouchesBegan?这将解决它,我认为...

你必须注意你的当前位置,并使用此函数ccp(location.x,location.y)你可以移动你想要的方向你的精灵