开发者

Pass Variables to a Function in Objective C

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!


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜