将变量传递给 Objective C 中的函数

发布于 2024-11-17 20:16:32 字数 1062 浏览 3 评论 0原文

首先,让我解释一下,我已经用谷歌搜索过这个问题,但似乎找不到明确的答案;但我相信这是因为我使用了不正确的术语。

我正在将一个球移动到 cocos2d/chipmunk ipad 应用程序中的某个位置,如下所示:

// Determine speed of the target
        int minDuration = 2.0;
        int maxDuration = 4.0;
        int rangeDuration = maxDuration - minDuration;
        int actualDuration = (arc4random() % rangeDuration) + minDuration;

        NSLog([NSString stringWithFormat:@"%d",actualDuration]);

        // Create the actions
        id actionMove = [CCMoveTo actionWithDuration:0.2 
                                            position:ccp(location.x, location.y)];
        id actionMoveDone = [CCCallFuncN actionWithTarget:self 
                                                 selector:@selector(spriteMoveFinished:)];
        [ball runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

        [ball retain]; 

我想将这段代码放入一个函数中(也许在 Obj-C 中称为“方法”,对吧?)并传递名称精灵(在本例中为“球”)、x 坐标 (location.x) 和 y 坐标 (location.y)。球是一个 CCSprite,位置是整数。

我是这方面的初学者,所以如果您提供解决方案,请让我知道如何在解决方案之后进行清理(例如内存释放)。

太感谢了!

Firstly, let me explain that I have googled this, and I can't seem to find a clear answer to this; but I believe this is because I am using incorrect terminology.

I am moving a ball to a location in a cocos2d/chipmunk ipad app like this:

// Determine speed of the target
        int minDuration = 2.0;
        int maxDuration = 4.0;
        int rangeDuration = maxDuration - minDuration;
        int actualDuration = (arc4random() % rangeDuration) + minDuration;

        NSLog([NSString stringWithFormat:@"%d",actualDuration]);

        // Create the actions
        id actionMove = [CCMoveTo actionWithDuration:0.2 
                                            position:ccp(location.x, location.y)];
        id actionMoveDone = [CCCallFuncN actionWithTarget:self 
                                                 selector:@selector(spriteMoveFinished:)];
        [ball runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

        [ball retain]; 

I want to put this piece of code into a function (perhaps called a "method" in Obj-C, right?) and pass in the name of the sprite (in this case it's "ball"), the x coordinate (location.x) and the y coordinate (location.y). The ball is a CCSprite and the location's are integers.

I am a beginner at this so if you provide a solution please let me know how to clean up after it (like memory deallocation).

Thank you so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

执手闯天涯 2024-11-24 20:16:32

这里有一个适合您的代码片段:

  • (void)moveBall:(CCNode*)ball toLocation:(CGPoint)location {

// 确定目标的速度
int minDuration = 2.0;
int 最大持续时间 = 4.0;
int rangeDuration = maxDuration - minDuration;
int实际持续时间 = (arc4random() % rangeDuration) + minDuration;

    NSLog([NSString stringWithFormat:@"%d",actualDuration]);

    // Create the actions
    id actionMove = [CCMoveTo actionWithDuration:0.2 
                                        position:ccp(location.x, location.y)];
    id actionMoveDone = [CCCallFuncN actionWithTarget:self 
                                             selector:@selector(spriteMoveFinished:)];
    [ball runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

    //        [ball retain];  //-- this makes no sense here

 }

您不需要在此函数中保留 ball 。无论如何,由于您没有指定 ball 的创建方式,我假设它已经在您创建它的位置正确保留。如果您提供更多详细信息,我可以提供进一步帮助。

Here you have a snippet that could be ok for you:

  • (void)moveBall:(CCNode*)ball toLocation:(CGPoint)location {

// Determine speed of the target
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;

    NSLog([NSString stringWithFormat:@"%d",actualDuration]);

    // Create the actions
    id actionMove = [CCMoveTo actionWithDuration:0.2 
                                        position:ccp(location.x, location.y)];
    id actionMoveDone = [CCCallFuncN actionWithTarget:self 
                                             selector:@selector(spriteMoveFinished:)];
    [ball runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

    //        [ball retain];  //-- this makes no sense here

 }

You don't need to retain ball in this function. Anyway, since you do not specify how ball is created, I assume that it is already correctly retained where you create it. If you give more details about it, I can help further.

阳光的暖冬 2024-11-24 20:16:32

干得好。我最后删除了保留,因为不需要它。另外,您应该考虑在方法外部的静态变量中进行实际持续时间计算,假设它始终相同。并且,您可能希望能够将选择器指定为方法参数之一,但这至少可以帮助您入门。

- (void) moveBall: (CCSprite *) ball toLocationX: float andY: float {
    int minDuration = 2.0;
    int maxDuration = 4.0;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = (arc4random() % rangeDuration) + minDuration;

    NSLog([NSString stringWithFormat:@"%d",actualDuration]);
    // Create the actions 
    id actionMove = [CCMoveTo actionWithDuration:0.2 position:ccp(x, y)];
    id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector               (spriteMoveFinished:)];

    [ball runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
}

Here you go. I removed the retain at the end because its not needed. Also, you should consider making the actualDuration calculation outside the method, in a static variable, assuming it will always be the same. AND, you might want to be able to specify the selector as one of the method arguments, but at least this will get you started.

- (void) moveBall: (CCSprite *) ball toLocationX: float andY: float {
    int minDuration = 2.0;
    int maxDuration = 4.0;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = (arc4random() % rangeDuration) + minDuration;

    NSLog([NSString stringWithFormat:@"%d",actualDuration]);
    // Create the actions 
    id actionMove = [CCMoveTo actionWithDuration:0.2 position:ccp(x, y)];
    id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector               (spriteMoveFinished:)];

    [ball runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文