2

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!

4

2 に答える 2

4

ここにあなたにとって大丈夫かもしれないスニペットがあります:

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

//ターゲットの速度を決定しますintminDuration= 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作成した場所ですでに正しく保持されていると思います。あなたがそれについてもっと詳細を与えるならば、私はさらに助けることができます。

于 2011-06-30T18:00:26.507 に答える
3

どうぞ。リテインは不要なので最後に外しました。また、actualDuration の計算は、常に同じであると仮定して、静的変数でメソッドの外で行うことを検討する必要があります。また、セレクターをメソッド引数の 1 つとして指定できるようにしたい場合もありますが、少なくともこれで開始できます。

- (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]];
}
于 2011-06-30T18:05:23.280 に答える