Reputation: 13
I try to call a method with a float parameter but the value the method gets is not what I sent.
Here is the code for the call (the actual call is the last part of the last line, [child setFaceDirection: direcao]; ):
direcao = dirRadianos * 180 / M_PI; // direcao = The value I send
NSLog(@"Value Sent: %f", direcao); // Log what I'm sending
for (CCNode *child in super.children) if([child respondsToSelector: @selector(setFaceDirection:)]) [child setFaceDirection: direcao];
Here is the code for the Method:
-(void) setFaceDirection: (float) angle {
NSLog(@"Value Recieved: %f", angle);
[theSprite setRotation: - angle ];
}
Here is the declaration of the float variable direcao
@interface PersControlNode : CCNode <CCTargetedTouchDelegate> {
// ...
float direcao;
// ...
}
// ...
@end
Here is the output from NSLog:
Value Sent: -16.699266
Value Recieved: 0.000000
Value Sent: -16.699301
Value Recieved: 36893488147419103232.000000
Value Sent: -16.699625
Value Recieved: -0.000000
Value Sent: 48.366463
Value Recieved: 2.000000
Value Sent: 48.366451
Value Recieved: -36893488147419103232.000000
Value Sent: 48.366428
Value Recieved: 0.000000
Value Sent: 48.366444
Value Recieved: -0.000000
Value Sent: 14.036244
Value Recieved: -0.000000
Value Sent: 14.036238
Value Recieved: -2.000000
Value Sent: 14.036201
Value Recieved: -36893488147419103232.000000
Value Sent: 14.036191
Value Recieved: -0.000000
Value Sent: 14.036273
Value Recieved: 36893488147419103232.000000
Value Sent: 12.528809
Value Recieved: 0.000000
Value Sent: 12.528766
Value Recieved: 36893488147419103232.000000
Value Sent: 12.528852
Value Recieved: -2.000000
Value Sent: 12.528863
Value Recieved: 0.000000
Value Sent: -101.309929
Value Recieved: -36893488147419103232.000000
Value Sent: -101.309860
Value Recieved: -2.000000
What am I doing wrong? I'm new to Objective C. Thanks to anyone who tries to help.
PS. It works better if I use int instead of float, but even then, there are still some errors.
Upvotes: 1
Views: 1295
Reputation: 12446
I had the same problem. In the HEADER (.h) file the arguments were int. But in the IMPLEMENTATION-file (.m) the arguments were float or double.
@interface ...
{}
- (void) resizeImage:(UIImage *)imageToResize ToFitWidth:(int)max_width andHeight:(int)max_height;
@implementation ...
- (void) resizeImage:(UIImage *)imageToResize ToFitWidth:(float)max_width andHeight:(float)max_height {
NSLog(@"max_width: %f", max_width);
...
The values were always 0.000000000.
SOLUTION could be: So it could be you have not declared the method in the HEADER (.h) or you just forgot to change the type of the arguments in the HEADER (.h), so the implementation of the method uses the type of arguments of the header!
Upvotes: 0
Reputation: 26
I encountered the same problem when passing float values to methods that are only implemented in *.m files but not declared in *.h files.
Example:
if you are using calling the method:
make sure that it is declared in that class's respective interface file. and make sure that you don't have a warning like: "object may not respond to - (void) updatedScreen....."
I declared whatever function that wasn't working in my interface file and it worked perfectly.
Upvotes: 1
Reputation: 9198
Do you have any other methods called setFaceDirection:
that take different types?
Do you have zero compiler warnings?
Upvotes: 1
Reputation: 19143
I believe this has to do with automatic type conversions from the compiler's side, but someone more knowledgeable about C will have to explain it. To fix the problem, change the type of the angle
parameter to double
.
Upvotes: 1