2013-03-31 76 views
0

因此,對於我的遊戲,我將多個精靈添加到數組中,我添加的精靈越多,我的遊戲開始運行得越快?我想我的問題是,是否添加像這樣的精靈,然後在for循環中調用它們的屬性來創建問題?或者是其他地方的問題。我發現添加更多的「地面」精靈會增加跳躍高度,移動速度和滾動速度。我應該改用拼貼來解決這個問題嗎?數組中的精靈

CCSprite *ground = [CCSprite spriteWithFile:@"testlevel 5.png"]; 
      [ground setPosition:ccp(520, 10)]; 
      [ground setScale:1.0]; 
      //[ground setScaleX:(2 * ground.contentSize.width)]; 
      [self addChild:ground z:0]; 
      [boxes addObject:ground]; 

      ground = [CCSprite spriteWithFile:@"testlevel 4.png"]; 
      [ground setPosition:ccp(ground.contentSize.width/2 +400, -50)]; 
      [ground setScale:1.0]; 
      //[ground setScaleX:(2 * ground.contentSize.width)]; 
      [self addChild:ground z:0]; 
      [boxes addObject:ground]; 

      ground = [CCSprite spriteWithFile:@"testlevel 3.png"]; 
      [ground setPosition:ccp(ground.contentSize.width/2 +280, -10)]; 
      [ground setScale:1.0]; 
      //[ground setScaleX:(2 * ground.contentSize.width)]; 
      [self addChild:ground z:0]; 
      [boxes addObject:ground]; 

上面這部分是我的圖片添加到陣列中,當我要檢查的東西像我的性格精靈與地面之間的交叉點我用一個for循環是這樣的:

gravity -= 2 ; 


for(CCSprite *ground in boxes){ 

    if(CGRectIntersectsRect(_character.boundingBox, ground.boundingBox) && !jump && (_character.position.y > ground.position.y + ground.contentSize.height/2 -20) && _character.position.x > ground.position.x - ground.contentSize.width/2 && facingLeft) 
    { 
     _character.position = ccp(_character.position.x, ground.position.y + ground.contentSize.height/2 + _character.contentSize.height/2-.001); 
     falling = NO; 
     onGround = YES; 
     gravity = 0; 
     if(!_moving){ 
      [_character setDisplayFrame: 
      [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"1-1.png"] ]; 
     } 
    } 

    else if(CGRectIntersectsRect(_character.boundingBox, ground.boundingBox) && !jump && (_character.position.y > ground.position.y + ground.contentSize.height/2-20) && _character.position.x < ground.position.x + ground.contentSize.width/2 && !facingLeft) 
    { 
     _character.position = ccp(_character.position.x, ground.position.y + ground.contentSize.height/2 + _character.contentSize.height/2-.001); 
     falling = NO; 
     onGround = YES; 
     gravity = 0; 
     if(!_moving){ 
      [_character setDisplayFrame: 
      [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"1-1.png"] ]; 
     } 
    } 

    else if(CGRectIntersectsRect(_character.boundingBox, ground.boundingBox) && (targetX <= 0) && (_character.position.y +_character.contentSize.height < ground.position.y + ground.contentSize.height) && (_character.position.x > ground.position.x) && (gp.isControlling && (gp.controlQuadrant == 2 || gp.controlQuadrant == 3))) 
    { 
     backgroundX = 0; 
     targetX = 0; 

    } 
    else if(CGRectIntersectsRect(_character.boundingBox, ground.boundingBox) && (_character.position.y +_character.contentSize.height < ground.position.y + ground.contentSize.height) && (_character.position.x < ground.position.x) && (gp.isControlling && (gp.controlQuadrant == 0 || gp.controlQuadrant == 1))) 
    { 

     backgroundX = 0; 
     targetX = 0; 
    } 

    else 
    { 
     normalForce = 0; 
     jump = NO; 
     falling = YES; 
     doubleJump = 0; 
    } 


    // totalForce = normalForce + gravity; 
    // [_character setPosition:ccp(_character.position.x, _character.position.y+totalForce)]; 
     } 
totalForce = normalForce + gravity; 
if(gravity == 0) { 
    falling = NO; 
    NSLog(@"Gravity = 0"); 
} 

[_character setPosition:ccp(_character.position.x + targetX, _character.position.y + totalForce)]; 

}

跳躍由一個簡單的按鈕控制,該按鈕將重力加到10左右。我有我的運行節拍功能重力看起來像這樣一個計時器:

gravity -=1; 

,我該值添加到我的角色位置,重力被設置爲0時,在盒子上面。玩家向左/向右是由一個操縱桿控制的,該操縱桿僅通過打勾功能更新速度。如果需要更多信息,請讓我知道,但同樣,將更多名爲ground的精靈添加到陣列中會改變速度,原因不明。任何幫助將非常感激。

下面是在代碼中跳

-(void)jumpTapped 
{ 
    NSLog(@"PRESSED"); 
    if(!falling && onGround){ 
     doubleJump += 1; 
     gravity += 10; 
     jump = YES; 
     onGround = NO; 
     NSLog(@"JUMPING"); 
    } 
} 
+0

您能否寫出完整的for (CCSprite *地面在框中)代碼?我有一種感覺,它與你在那裏的代碼有關 –

+0

這應該是一切 – user1569940

+0

所以從我可以告訴簡單地查看代碼後,我猜測應該只有一個if語句返回真的,這是正確的嗎? –

回答

0

虛空功能

} else { 
    normalForce = 0; 
    jump = NO; 
    falling = YES; 
    doubleJump = 0; 
} 

設置跳轉到沒有那麼如果第一個精靈它在循環貫穿和不符合任何那些if語句,它會默認爲這個。然後下一次運行時跳轉將被設置爲NO,這會從您按下按鈕時拋出初始值,因此,讓玩家跳轉的代碼將永遠不會運行

+0

跳等於no只是檢查天氣與否按鈕被按下,我能夠通過添加我的if語句中的引力<0子句。非常感謝!這讓我非常頭疼,但現在已經修好了!儘管爲了節省輸入碰撞對象座標的時間,我很可能會盡快切換到拼貼。 – user1569940

+0

很高興看到你的if語句也基於跳轉變量的碰撞檢測。即在您的if語句中,它表示&&!跳&&表示如果跳轉設置爲NO,則跳過if語句 –