【问题标题】:Pass Variables to a Function in Objective C将变量传递给 Objective C 中的函数
【发布时间】:2011-06-30 17:50:47
【问题描述】:

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

我正在将球移动到 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,位置是整数。

我是这方面的初学者,所以如果您提供解决方案,请告诉我如何清理它(如内存释放)。

非常感谢!

【问题讨论】:

  • Objective-C 有功能;尽管它们是相关的,但它们与方法不同。方法是属于对象的某种函数。语法略有不同 -- 方法:- (void) thisIsAMethodWithAParameter: (int)param { return; } 功能:void thisIsAFunction(int param){ return; }
  • 感谢乔希的澄清

标签: objective-c xcode ipad cocos2d-iphone chipmunk


【解决方案1】:

这里有一个适合你的 sn-p:

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

// 确定目标的速度 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

 }

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

【讨论】:

  • 哈哈,你在开玩笑吧。无论如何,以 4 分钟的优势击败了我!
  • 太好了,非常感谢塞尔吉奥,我有足够的时间在这里运行:)
  • 我试图用这个答案修复格式问题,但因为更改不超过 6 个非空格字符而无法解决
【解决方案2】:

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

- (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]];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多