2012-02-15 43 views
0

我正在嘗試使用cocos2d創建我的第一個遊戲。我正在嘗試插入子彈。當我得到這個錯誤。問題是隻有當一名球員不是爲敵方精靈射擊纔會發生。當這個錯誤發生時,不僅玩家與另一個玩家交換位置,而且該子彈在擊中兩個目標後被破壞。添加子彈會產生OpenGL錯誤0x0503 in - [EAGLView swapBuffers]

OpenGL error 0x0503 in -[EAGLView swapBuffers] 

我的武器類有以下子彈實現

if([self.bulletsArray count] <= ([self.numberOfBulletsPerFire intValue]*[self.numberOfBulletsOnScreen intValue])) 
{ 
    for (int i =0; i< [self.numberOfBulletsPerFire intValue]; i++) { 
     BulletClass *bullet = [[Bullet alloc]initWithPosition:position Direction:direction strength:self.weaponLevel spriteArray:spriteArray enemyArray:enemyArray base:base]; 
     [self.bulletsArray addObject:bullet]; 
     [self addChild:bullet]; 
     [bullet release];bullet = nil; 
    } 
} 

在BulletClass我有init方法爲:

(id)initWithPosition:(CGPoint)position 
     Direction:(KDirectionInput)direction 
     strength:(NSNumber *)strength 
     spriteArray:(NSMutableArray *)sprites 
     enemyArray:(NSMutableArray *)enemyArray 
      base:(CCSprite *)base{ 
if ((self = [super init])) { 
    self.base = base; 
    self.strength = strength; 
    self.movementDirection = direction; 
    self.spriteArray = sprites; 
    self.enemyArray = enemyArray; 
    self.velocity = 200/1; 
    self.bullet.position = position; 
    [self addChild:self.bullet z:2]; 
    } 
    return self; 
} 

誰能幫助我在這裏..

回答

0

這裏有幾個問題可能會導致你的問題。

首先,雖然不是一個錯誤,但更多的是性能方面的事情,您不應該在您的條件下放置count s和其他對象方法,如intValue,因爲這會降低程序的運行速度。在等於此數字的循環之前定義一個局部變量,然後在循環中使用該變量,以便軟件在每次循環時都不會重複計算相同的數組或重複提取相同的值。

更重要的是,我沒有看到你正在使用一個精靈幀緩存(精靈批處理)的任何地方,並且如果你使用的精靈數組非常大,在你的數組數很大並且操縱和繪製這些精靈多次正如通常由子彈操作所要求的那樣,您可能會不必要地爲OpenGL增加很多負擔,可能會造成您看到的緩衝區錯誤。

你應該爲你的精靈數組使用一個cocos sprite批處理並給這個另一個嘗試;至少它會大大提高項目在子彈操作上的表現。