当我尝试从另一个方法内部调用外部方法时出现 NSInvalidArgumentException
我收到控制台消息:
2011-10-05 17:21:15.112 Fairstead[4986:207] -[CCSprite translate::::]: unrecognized selector sent to instance 0x546fdb0
2011-10-05 17:21:15.116 Fairstead[4986:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCSprite translate::::]: unrecognized selector sent to instance 0x546fdb0'
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector]convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
[pc translate:300.0 :touchLocation :pc.position :pc]; //crash on this line
}
这是翻译方法:
-(void) translate:(float) objectVelocity: (CGPoint) translateLocation: (CGPoint) objectLocation:(DefaultObject *) sender
{
CGPoint moveDifference = ccpSub(translateLocation, objectLocation);
float distanceToMove = ccpLength(moveDifference);
float moveDuration = distanceToMove / objectVelocity;
[sender runAction:[CCMoveTo actionWithDuration:moveDuration position:translateLocation]];
}
触摸结束方法与翻译位于不同的类文件中。
翻译方法位于 DefaultObject 类文件中,该文件是合成的 CCSprite pc 的子类
,并且 @property 是非原子的,保留
我缺少什么?
I get the console messages:
2011-10-05 17:21:15.112 Fairstead[4986:207] -[CCSprite translate::::]: unrecognized selector sent to instance 0x546fdb0
2011-10-05 17:21:15.116 Fairstead[4986:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCSprite translate::::]: unrecognized selector sent to instance 0x546fdb0'
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector]convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
[pc translate:300.0 :touchLocation :pc.position :pc]; //crash on this line
}
here is the translate method:
-(void) translate:(float) objectVelocity: (CGPoint) translateLocation: (CGPoint) objectLocation:(DefaultObject *) sender
{
CGPoint moveDifference = ccpSub(translateLocation, objectLocation);
float distanceToMove = ccpLength(moveDifference);
float moveDuration = distanceToMove / objectVelocity;
[sender runAction:[CCMoveTo actionWithDuration:moveDuration position:translateLocation]];
}
The touches ended method is in a different class file than translate.
The translate method is in a DefaultObject class file that is a sub-class of CCSprite
pc is synthesized and the @property is nonatomic, retain
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在方法定义和方法调用中都缺少参数标签。
首先,我可以看到你来自哪里:在大多数编程语言中,包括但不限于 C、C++、Java 等,方法的形式为:
Objective-C,但是,不同的是,因为你还需要标记参数:
现在,objC 初学者可能会问,既然我们已经命名了参数,为什么还需要给它们加上标签呢?考虑下面对上面第一个方法的 C++ 调用:
现在让我问你,23 和 45 指的是什么?您必须返回到方法定义才能知道,例如,23 是速度,45 是角度。一些程序员甚至可能会这样做:
尽管如此,当我们在 Objective-C 中可以执行以下操作时,为什么我们需要声明变量:
现在,您只需从方法调用本身就可以轻松了解 23 和 45 的含义。无需回顾或记住方法定义头或声明变量。
回到您的代码,您需要将方法定义标头修改为类似以下内容:
并使用以下方式调用它(为了清楚起见,此处使用多行,但如果您愿意,您可以随时将其折叠为一行):
You are missing the argument labels, both in method definition and method call.
First of all, I can see where you are coming from: in most programming languages including but not limited to C, C++, Java etc a method has the form of:
Objective-C, however, is different because you also need to label the arguments:
Now, objC beginners might ask why do we need to label the arguments when we already name them? Consider the following C++ call to our first method above:
Now let me ask you, what are 23 and 45 referring to? You would have to go back to the method definition to know that, for example, 23 is speed and 45 is angle. Some programmers might even do the following:
Still, why do we need to declare variables when we can do the following in Objective-C:
You can now easily know what 23 and 45 mean just from the method call itself. No need to refer back or memorize the method definition header or declare variables.
Back to your code, you need to modify your method definition header to something like:
And call it using (made multi-lined here for clarity but you can always collapse it into one line if you prefer):