I'm making a simple game using Cocos2D. The user can touch and trace a path for small moving objects. The objects then follow the path. This is similar to the Flight Control game mechanic.
My code works well almost all the time. However sometimes I attempt to touch and drag an object but the touch callbacks don't fire. Here's how my code works in general:
- small moving objects inherit
CCLayer
- I'm using the touch callbacks that have a single touch parameter (
ccTouchMoved:withEvent:
etc) - all callbacks like
ccTouchBegan:withEvent:
are on the objects (instead of the main game layer)
How can I improve the touch handling such that the error noted above is avoided?
Here's my thoughts so far: Perhaps the touch callbacks are not firing because the objects (which are always moving) move slightly just around the time the user attempts to touch the object. This could be fixed by handling the touches on the main game layer using callbacks that have collections of touches as parameters. Since the total object count is relatively small (say less than 50) I can just compare each touch location with each object.
I also tried increasing the touchable area for the objects (note the 1.5 multiplication). This seemed to help but did not eliminate the problem.
- (BOOL) ccTouchBegan:(UITouch *) touch withEvent:(UIEvent *) event {
CGSize size = self.contentSize;
CGRect rect = CGRectMake(-size.width / 2.0,
-size.height / 2.0,
size.width * 1.5,
size.height * 1.5);
return CGRectContainsPoint(rect, [self convertTouchToNodeSpace:touch]);
}
Another thing to try would be increasing the speed of the objects and see if the callbacks don't fire more often. At the moment the objects move slowly.
Any help is much appreciated!
IIRC, node's origin is in its bottom left corner. Try