cocos2d determining where on screen user is touching
I am trying to determine where the user is touching in relation to the character on the screen using cocos2d sprites. When the user clicks to the left of the sprite, I want the sprite to run to the left, and vice versa. My problem is, when the user clicks on one side and moves over to the other side without letting go of the touch (cctouchended doesnt fire), the sprite keeps running, but facing th开发者_运维知识库e wrong direction. Where would I implement the check (and how) to determine if the user's touch has moved to the other side of the character?
current code that I tried:
- (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;
}
}
Thanks.
You need to convert Your touch location like this.
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
Then you get correct touch location on your view in cocos2d.
Can't you just put it in ccTouchesBegan instead? That would solve it, I think...
You have to note your current location , and using this function ccp(location.x, location.y) you can move your sprite in that direction in which you want
精彩评论