2010-07-19 80 views
1

我尝试使用float参数调用方法,但方法获取的值不是我发送的。将参数传递给方法的问题

下面是呼叫(实际呼叫是最后一行的最后一部分,[孩子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]; 

下面是该方法的代码:

-(void) setFaceDirection: (float) angle { 
    NSLog(@"Value Recieved: %f", angle); 
    [theSprite setRotation: - angle ]; 
} 

这里是浮点变量direcao

@interface PersControlNode : CCNode <CCTargetedTouchDelegate> { 
      // ... 
     float direcao; 
      // ... 

} 
// ... 
@end 

这里的声明是从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 

我在做什么错?我是Objective C的新手。感谢任何想尝试帮助的人。

PS。如果我使用int而不是float,它会更好,但即使如此,仍然存在一些错误。

+1

它工作得更好,如果该参数是int类型的......但即使是这样,它不是完美的 – Jose 2010-07-19 15:15:53

回答

1

将float值传递给仅在* .m文件中实现但未在* .h文件中声明的方法时遇到同样的问题。

实施例:

如果使用的是调用方法:

  • (无效)updateScreen:(浮点)值; { ... }

确保它在该类的各个接口文件中声明。并确保你没有像这样的警告: “对象可能无法响应 - (void)updatedScreen .....”

我声明了任何不起作用的接口文件中的函数,完美地工作。

1

我认为这与编译器方面的自动类型转换有关,但是对C有更多了解的人必须解释它。要解决该问题,请将angle参数的类型更改为double

+0

谢谢...我会尝试, – Jose 2010-07-19 21:19:30

1

您是否有其他方法称为setFaceDirection:采取不同类型?

你有没有编译器警告?

+0

嗨..我有其他的方法称为setFaceDirection但他们也采取浮动 唯一警告我曾经得到的是“CCNode可能无法响应setFaceDirection”,女巫是真实的,这就是为什么我检查它是否首先响应。 但现在我将所有转换为int,警告消失... – Jose 2010-07-19 21:18:14

+0

只是澄清:我有一些类与setFaceDirection:(浮动)角度{..}。他们都是漂浮的。然后,由于这个问题,我把所有东西都改为int,并且(有点)工作。但我必须能够通过浮动参数。有趣的事情是,当永恒的时候,警告就消失了。直到现在我还没有注意到。谢啦。 – Jose 2010-07-19 21:26:17

0

我有同样的问题。 在HEADER(.h)文件中,参数是int。 但是在IMPLEMENTATION-file(.m)中,参数是float或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); 
    ... 

该值始终为0.000000000。

解决方案可能是: 因此,您可能未在HEADER(.h)中声明该方法,或者您只是忘记更改HEADER(.h)中的参数类型,因此实施方法使用标题的参数类型!