2011-04-13 3 views
0

나는 Canabalt와 유사한 것을 짓고 건물과 건물 (모듈) 사이에 구멍과 높이를 더하려고합니다. (간단한 랜드를 추가 할 수Cocos2D - 건물 사이에 구멍과 높이를 더하는 것

-(id) init 
{ 
    // always call "super" init 
    // Apple recommends to re-assign "self" with the "super" return value 
    if((self=[super init])) { 

     moduleSize = 160; 

     screenSize = [[CCDirector sharedDirector] winSize]; 

     CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache]; 
     [frameCache addSpriteFramesWithFile:@"ModulesScene1.plist"]; 

     CCTexture2D* gameArtTexture = [[CCTextureCache sharedTextureCache] addImage:@"ModulesScene1.png"]; 

     // Create the background spritebatch 
     spriteBatch = [CCSpriteBatchNode batchNodeWithTexture:gameArtTexture]; 
     [self addChild:spriteBatch]; 

     numStripes = 1; 

     /* BEGIN MODULES */ 

     NSString* frameName = [NSString stringWithFormat:@"Module0-hd.png"]; 
     CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:frameName]; 
     sprite.anchorPoint = CGPointMake(0, 0.5f); 
     sprite.position = CGPointMake(0, screenSize.height/2); 
     [spriteBatch addChild:sprite z:0 tag:0]; 

     frameName = [NSString stringWithFormat:@"Module0-hd.png"]; 
     sprite = [CCSprite spriteWithSpriteFrameName:frameName]; 
     sprite.anchorPoint = CGPointMake(0, 0.5f); 
     sprite.position = CGPointMake((moduleSize - 1.1f), screenSize.height/2); 
     [spriteBatch addChild:sprite z:1 tag:1]; 

     frameName = [NSString stringWithFormat:@"Module1-hd.png"]; 
     sprite = [CCSprite spriteWithSpriteFrameName:frameName]; 
     sprite.anchorPoint = CGPointMake(0, 0.5f); 
     sprite.position = CGPointMake((moduleSize * 2) - 1.1f, screenSize.height/2); 
     [spriteBatch addChild:sprite z:2 tag:2]; 

     frameName = [NSString stringWithFormat:@"Module2-hd.png"]; 
     sprite = [CCSprite spriteWithSpriteFrameName:frameName]; 
     sprite.anchorPoint = CGPointMake(0, 0.5f); 
     sprite.position = CGPointMake(((moduleSize * 3) - 1.1f), screenSize.height/2); 
     [spriteBatch addChild:sprite z:3 tag:3]; 

     frameName = [NSString stringWithFormat:@"Module0-hd.png"]; 
     sprite = [CCSprite spriteWithSpriteFrameName:frameName]; 
     sprite.anchorPoint = CGPointMake(0, 0.5f); 
     sprite.position = CGPointMake(((moduleSize * 4) - 1.1f), screenSize.height/2); 
     [spriteBatch addChild:sprite z:4 tag:4]; 

     frameName = [NSString stringWithFormat:@"Module1-hd.png"]; 
     sprite = [CCSprite spriteWithSpriteFrameName:frameName]; 
     sprite.anchorPoint = CGPointMake(0, 0.5f); 
     sprite.position = CGPointMake(((moduleSize * 5) - 1.1f), screenSize.height/2); 
     [spriteBatch addChild:sprite z:5 tag:5]; 

     /* END MODULES */ 

     // Get current scrollSpped 
     scrollSpeed = [[GameManager sharedGameManager] scrollSpeed]; 

     speedFactors = [[CCArray alloc] initWithCapacity:numStripes]; 
     [speedFactors addObject:[NSNumber numberWithFloat:2.5f]]; 
     NSAssert([speedFactors count] == numStripes, @"speedFactors count does not match numStripes!"); 

     [self scheduleUpdate]; 

    } 

    return self; 

} 

-(void) update:(ccTime)delta 
{ 

    CCSprite* sprite; 
    scrollSpeed = [[GameManager sharedGameManager] scrollSpeed]; 

    CCARRAY_FOREACH([spriteBatch children], sprite) 
    { 

     NSNumber* factor = [speedFactors objectAtIndex:0]; 

     CGPoint pos = sprite.position; 
     pos.x -= scrollSpeed * [factor floatValue]; 

     if (pos.x < -screenSize.width) 
     { 

      pos.x += ((screenSize.width * 2) - 2); 

      int x = (arc4random() % 3); 

      int xHole = (arc4random() % 10); 

      NSString *randomName = nil; 

      CCSpriteFrame *randomFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:randomName]; 
      [sprite setDisplayFrame:randomFrame]; 

     } 

     sprite.position = pos; 

    } 

} 
+0

무엇이 질문입니까? –

+0

지금 모든 건물은 모두 같은 높이에 있고 서로 붙어 있습니다. 나는 그들이 무작위로 약간 더 높거나 낮추고 구멍이있는 것처럼 그들 사이에 공간을 추가하길 바란다. – Antoni

답변

0

은 건물 사이에 구멍을 추가 : 내가 현재 사용하고 코드를 여기에 http://twitpic.com/4kb5jd

됩니다 : 당신은 어떻게 게임에서 지금 찾고의 스크린 샷을 볼 수 있습니다 INIT 방법에서처럼) 기능 :

...

sprite.position = CGPointMake(((moduleSize * 3) - 1.1f + (rand()%40)), screenSize.height/2); 

...

무작위 간격을 추가합니다 (최대 40 점)