我正在尝试使用 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;
}
}
谢谢。