当我尝试从另一个方法内部调用外部方法时出现 NSInvalidArgumentException

发布于 2024-12-08 10:13:18 字数 1328 浏览 0 评论 0原文

我收到控制台消息:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月寒剑心 2024-12-15 10:13:18

您在方法定义和方法调用中都缺少参数标签。

首先,我可以看到你来自哪里:在大多数编程语言中,包括但不限于 C、C++、Java 等,方法的形式为:

float acceleration(float speed, float angle) { }

Objective-C,但是,不同的是,因为你还需要标记参数:

- (float) accelerationFromSpeed:(float)speed andAngle:(float)angle { }

现在,objC 初学者可能会问,既然我们已经命名了参数,为什么还需要给它们加上标签呢?考虑下面对上面第一个方法的 C++ 调用:

float result = obj->acceleration(23, 45);

现在让我问你,23 和 45 指的是什么?您必须返回到方法定义才能知道,例如,23 是速度,45 是角度。一些程序员甚至可能会这样做:

float speed = 23;
float angle = 45;
float result = obj->acceleration(speed, angle);

尽管如此,当我们在 Objective-C 中可以执行以下操作时,为什么我们需要声明变量:

float result = [obj accelerationFromSpeed:23 andAngle:45];

现在,您只需从方法调用本身就可以轻松了解 23 和 45 的含义。无需回顾或记住方法定义头或声明变量。

回到您的代码,您需要将方法定义标头修改为类似以下内容:

-(void) translate:(CGPoint)translateLocation  
     withVelocity:(float)objectVelocity 
andObjectLocation:(CGPoint)objectLocation 
        andSender:(DefaultObject *)sender
{
    CGPoint moveDifference = ccpSub(translateLocation, objectLocation);
    float distanceToMove = ccpLength(moveDifference);
    float moveDuration = distanceToMove / objectVelocity;

    [sender runAction:[CCMoveTo actionWithDuration:moveDuration position:translateLocation]];
}

并使用以下方式调用它(为了清楚起见,此处使用多行,但如果您愿意,您可以随时将其折叠为一行):

[pc      
         translate:touchLocation 
      withVelocity:300.0 
 andObjectLocation:pc.position 
         andSender:pc];

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:

float acceleration(float speed, float angle) { }

Objective-C, however, is different because you also need to label the arguments:

- (float) accelerationFromSpeed:(float)speed andAngle:(float)angle { }

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:

float result = obj->acceleration(23, 45);

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:

float speed = 23;
float angle = 45;
float result = obj->acceleration(speed, angle);

Still, why do we need to declare variables when we can do the following in Objective-C:

float result = [obj accelerationFromSpeed:23 andAngle:45];

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:

-(void) translate:(CGPoint)translateLocation  
     withVelocity:(float)objectVelocity 
andObjectLocation:(CGPoint)objectLocation 
        andSender:(DefaultObject *)sender
{
    CGPoint moveDifference = ccpSub(translateLocation, objectLocation);
    float distanceToMove = ccpLength(moveDifference);
    float moveDuration = distanceToMove / objectVelocity;

    [sender runAction:[CCMoveTo actionWithDuration:moveDuration position:translateLocation]];
}

And call it using (made multi-lined here for clarity but you can always collapse it into one line if you prefer):

[pc      
         translate:touchLocation 
      withVelocity:300.0 
 andObjectLocation:pc.position 
         andSender:pc];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文