将变量传递给 Objective C 中的函数
首先,让我解释一下,我已经用谷歌搜索过这个问题,但似乎找不到明确的答案;但我相信这是因为我使用了不正确的术语。
我正在将一个球移动到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一个适合您的代码片段:
// 确定目标的速度
int minDuration = 2.0;
int 最大持续时间 = 4.0;
int rangeDuration = maxDuration - minDuration;
int实际持续时间 = (arc4random() % rangeDuration) + minDuration;
您不需要在此函数中保留
ball
。无论如何,由于您没有指定ball
的创建方式,我假设它已经在您创建它的位置正确保留。如果您提供更多详细信息,我可以提供进一步帮助。Here you have a snippet that could be ok for you:
// Determine speed of the target
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
You don't need to retain
ball
in this function. Anyway, since you do not specify howball
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.干得好。我最后删除了保留,因为不需要它。另外,您应该考虑在方法外部的静态变量中进行实际持续时间计算,假设它始终相同。并且,您可能希望能够将选择器指定为方法参数之一,但这至少可以帮助您入门。
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.