I've begun my first real attempt at a Cocos2D app (ex. not just screwing around) and I'm having a problem with detecting touch location. I used the Cocos2D-iphone template in Xcode to start this project and used the samples in the Cocos2D SVN to add touch detection. For some reason I get a strange result, though, on both the iPhone simulator and device.
After detecting the problem I stripped the application a bit to try to find the problem, but I can't figure this out. Anyone have an idea?
AppDelegate.m
- (void) applicationDidFinishLaunching:(UIApplication*)application {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window setUserInteractionEnabled:YES];
[window setMultipleTouchEnabled:YES];
if ( ![CCDirector setDirectorType:CCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:CCDirectorTypeMainLoop];
// Obtain the shared director in order to...
CCDirector *director = [CCDirector sharedDirector];
// Turn on display FPS
[director setDisplayFPS:YES];
[director attachInView:window];
[window makeKeyAndVisible];
// Default texture format for PNG/BMP/TIFF/JPEG/GIF images
// It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
// You can change anytime.
[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
[[CCDirector sharedDirector] runWithScene:[GameScene scene]];
}
GameScene.m (subclassing CCLayer)
- (void)onEnter {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[super onEnter];
}
- (void)onExit {
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint loc = [self convertTouchToNodeSpaceAR:touch];
NSLog(@"began: (%d, %d)", loc.x, loc.y);
return YES;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint loc = [self convertTouchToNodeSpaceAR:touch];
NSLog(@"moved: (%d, %d)", loc.x, loc.y);
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint loc = [self convertTouchToNodeSpaceAR:touch];
NSLog(@"ended: (%d, %d)", loc.x, loc.y);
}
And the very odd output after some random clicks all around the screen:
[21817:207] began: (0, -1072693248)
[21817:207] ended: (0, -1072693248)
[21817:207] began: (0, 1077936128)
[21817:207] moved: (0, 1077936128)
[21817:207] moved: (0, 1077936128)
[21817:207] moved: (0, 1077936128)
[21817:207] moved: (0, 1077936128)
[21817:207] moved: (0, 1077936128)
[21817:207] moved: (0, 1077870592)
[21817:207] ended: (0, 1077870592)
[21817:207] began: (0, 1076232192)
[21817:207] moved: (0, 1076953088)
[21817:207] moved: (0, 1077673984)
[21817:207] moved: (0, 1078329344)
[21817:207] moved: (0, 1078820864)
[21817:207] ended: (0, 1078820864)
[21817:207] began: (0, -1068761088)
[21817:207] ended: (0, -1068761088)
[21817:207] began: (0, -1068449792)
[21817:207] moved: (0, -1068449792)
[21817:207] moved: (0, -1068449792)
[21817:207] moved: (0, -1068449792)
[21817:207] moved: (0, -1068482560)
[21817:207] ended: (0, -1068482560)
[21817:207] began: (0, 1076887552)
[21817:207] moved: (0, 1076756480)
[21817:207] moved: (0, 1076494336)
[21817:207] moved: (0, 1076101120)
[21817:207] ended: (0, 1076101120)
[21817:207] began: (0, 1077477376)
[21817:207] moved: (0, 1077346304)
[21817:207] moved: (0, 1077215232)
[21817:207] moved: (0, 1077149696)
[21817:207] moved: (0, 1077346304)
[21817:207] moved: (0, 1077936128)
[21817:207] ended: (0, 1077936128)
[21817:207] began: (0, -1072431104)
[21817:207] moved: (0, -1071513600)
[21817:207] moved: (0, -1071251456)
[21817:207] moved: (0, -1071120384)
[21817:207] moved: (0, -1070858240)
[21817:207] ended: (0, -1070858240)