Touch sensing in SK game does not pick up touchEnded
UPDATE: for every request. I got NSLogged [[event touchesForView:self.view] count]
and touches count
and got the result 2
and 1
when it crashes, so it seems like the event is lost somehow
Here's my two simple methods: (updated)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInNode:self];
if (touchLocation.x < self.size.width / 2.0f) {
NSLog(@"left");
touching = YES;
}
if (touchLocation.x > self.size.width / 2.0f) {
NSLog(@"right");
if (![rocketFlame parent]) {
[self fireRocket];
}
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
for (UITouch *touch in touches) {
NSLog([[event touchesForView:self.view] count]);
NSLog([touches count]);
CGPoint touchLocation = [touch locationInNode:self];
if (touchLocation.x < self.size.width / 2.0f) {
NSLog(@"left off");
touching = NO;
}
if (touchLocation.x > self.size.width / 2.0f) {
NSLog(@"right off");
//do nothing
}
}
}
The problem is I am getting an error where the game does not sense the end touch on the left side of the screen. I'm not sure if this error can also affect the right side of the screen, since this action is only performed once and is not a continuous action, so I wouldn't notice if it didn't feel like a completed touch.
Basically, if I touch at the same time and end my touches at the same time, it works fireRocket
and changes touching
to YES
, but then never changes touching
to NO
, even if both fingers are off.
Here's the log output:
right
left
2
1
right off
My main suspicion is that when there are two touches, it merges into one event. So when the last finger is removed, it calls the callandsEnd callback method.
There are several ways to handle your decision.
The first one is in touchesEnd
, see if it [[event touchesForView:self.view] count]
returns a number greater than 1. After it is greater than two, go through [event touchesForView:self.view]
and make sure it meets your "right" and "left" criteria.
The second option is to have additional booleans that are set to YES
internally touchesBegan
, but only when you have both left and right registered. I think something like "leftTouched" and "rightTouched". And inside the method, touchesEnd
just see if those variables are set to YES
. Don't forget to add the code to reset to NO
at the end of the method touchesEnd
.
source to share