2012-02-22 9 views
3

次のコード行をどのように減らすことができますか?私はまだobjective-c/cocos2Dでかなり新しいです。私の次のコードは電車のようなものです。 PHPならば、簡単にループを作成してこれらすべてを得ることができますが、私はobj-cにまだ慣れていないだけです。Objective-C/Cocos2Dコードのリピート回数を減らす

dinosaur1_c = [CCSprite spriteWithSpriteFrameName:@"dinosaur1-c.png"]; 
    dinosaur2_c = [CCSprite spriteWithSpriteFrameName:@"dinosaur2-c.png"]; 
    dinosaur3_c = [CCSprite spriteWithSpriteFrameName:@"dinosaur3-c.png"]; 
    dinosaur4_c = [CCSprite spriteWithSpriteFrameName:@"dinosaur4-c.png"]; 
    dinosaur5_c = [CCSprite spriteWithSpriteFrameName:@"dinosaur5-c.png"]; 
    dinosaur6_c = [CCSprite spriteWithSpriteFrameName:@"dinosaur6-c.png"]; 
    dinosaur7_c = [CCSprite spriteWithSpriteFrameName:@"dinosaur7-c.png"]; 
    dinosaur8_c = [CCSprite spriteWithSpriteFrameName:@"dinosaur8-c.png"]; 
    dinosaur9_c = [CCSprite spriteWithSpriteFrameName:@"dinosaur9-c.png"]; 
    dinosaur10_c = [CCSprite spriteWithSpriteFrameName:@"dinosaur10-c.png"]; 
    dinosaur11_c = [CCSprite spriteWithSpriteFrameName:@"dinosaur11-c.png"]; 

    [sceneSpriteBatchNode addChild:dinosaur1_c]; 
    [sceneSpriteBatchNode addChild:dinosaur2_c]; 
    [sceneSpriteBatchNode addChild:dinosaur3_c]; 
    [sceneSpriteBatchNode addChild:dinosaur4_c]; 
    [sceneSpriteBatchNode addChild:dinosaur5_c]; 
    [sceneSpriteBatchNode addChild:dinosaur6_c]; 
    [sceneSpriteBatchNode addChild:dinosaur7_c]; 
    [sceneSpriteBatchNode addChild:dinosaur8_c]; 
    [sceneSpriteBatchNode addChild:dinosaur9_c]; 
    [sceneSpriteBatchNode addChild:dinosaur10_c]; 
    [sceneSpriteBatchNode addChild:dinosaur11_c]; 

入力が非常に高く評価されています。

+1

は、彼らが一時、あなたのインスタンス変数をdinosaur1_c'、またはある 'ていますか? – dasblinkenlight

+0

これらはインスタンス変数です – rizzlerazzle

答えて

4

私はこのように、NSMutableArrayでこれらのオブジェクトを管理するお勧めします。

NSMutableArray *sprites = [[NSMutableArray alloc] init]; 
for (int i = 1; i <= 11; i++) { 
    id dino = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"dinosaur%d-c.png",i]]; 
    [sprites addObject:dino]; 
    [sceneSpriteBatchNode addChild:dino]; 
} 

// Since I don't know what your addChild: method does, the 'sprites' array exists to let you access the objects later, outside of the 'for' loop if desired... 
// So where you would've used dinosaur4_c before, you would instead use [sprites objectAtIndex:4] 
// This also demonstrates how to cast the return value from -objectAtIndex: to a CCSprite * 
CCSprite *certainDino = (CCSprite *)[sprites objectAtIndex:4]; 

// Then, when done working with the sprites 
[sprites release]; 
+0

C-Array(またはCCArray)だけを使用するのはなぜですか?リンクされたリストよりも高速です。 –

+0

「スプライト」に何も追加しないでください –

+0

@JimRhodesありがとうございます。一定! – bneely

関連する問題