I'm pretty new to iphone app development, and so I'm starting off using the Cocos2d framework instead of entirely objective c from scratch, because it has lowered the learning curve some and I'm finding it much easier. I'm attempting to do some of the tutorials off the Cocos2d website, but, being human rather than a sheep, have decided to try some stuff that wasn't in the tutorial.
So, here's the relevant code from the tutorial:
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [self convertTouchToNodeSpace: touch];
[cocosGuy stopAllActions];
[cocosGuy runAction: [CCMoveTo actionWithDuration:1 position:ccp( location.x + 32, location.y + 32)]];
This works as expected, the cocos guy moves to where I touch my ipod with a one second delay and a 32 pixel offset. So now, I wanted to get some textual feedback as well about where I was tapping the screen. I tried to use NSLog to display some information about the location
of the touch:
NSLog([NSString stringWithFormat:@"x=%d y=%d", location.x, location.y]);
Xcode kindly informs me: Format not a string literal and no format arguments
. Unless I'm entirely mistaken, @"text"
is a string literal in objective C and the two location variables are the format arguments.
When run, the console displays something like:
2011-06-20 19:54:22.765 Lesson1[315:707] x=0 y=1078640640
2011-06-20 19:54:23.887 Lesson1[315:707] x=0 y=1080631296
2011-06-20 19:54:24.576 Lesson1[315:707] x=0 y=1080242176
when I touch the ipod screen at three random places. x never seems to change and y is always a massive integer.
What is wrong with my code, and being new to the world of iphone development, objective c, and cocos2d, how can I go about trying to better debug this myself in the future?
Please note that this is literally my second day of experimenting with all of this, so forgive me if this is an incredibly obvious or stupid question.
The x, y
members of CGPoint
are float
s, not int
s, hence you should use %f
instead of %d
.
Also, NSLog()
is able to do formatting itself. Try:
NSLog(@"x=%f y=%f", location.x, location.y);
instead. Note that in the code above the first parameter to NSLog()
:
@"x=%f y=%f"
is a string literal, whilst in your code the first parameter is not literal:
[NSString stringWithFormat:@"x=%d y=%d", location.x, location.y]