2011-11-02 49 views
0

我正在做一個小遊戲,這裏是怎麼回事一些示例代碼:的NSMutableArray和batchNode問題

-(id) init 
{ 
     self.arrowProjectileArray = [[[NSMutableArray alloc] init] autorelease]; 
     self.batchNode = [CCSpriteBatchNode batchNodeWithTexture:[[CCTextureCache sharedTextureCache] addImage:@"arrow.png"]]; 
     [self addChild:_batchNode z:2]; 
     for (CCSprite *projectile in _arrowProjectileArray) { 
     [_batchNode removeChild:projectile cleanup:YES]; 
     } 
     [_arrowProjectileArray removeAllObjects]; 
     self.nextProjectile = nil; 
     } 
    } 

-(void) callEveryFrame:(ccTime)dt{ 
    for (int i = 0; i < [_arrowProjectileArray count];i++) { 
      CCSprite *cursprite = [_arrowProjectileArray objectAtIndex:i]; 
      if (cursprite.tag == 1) { 
      float x = theSpot.x+10; 
      float y = theSpot.y+10; 
      cursprite.position = ccp(x, y); 
      } 
     } 

    - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
    [_batchNode addChild:_nextProjectile z:1 tag:1]; 
     [_arrowProjectileArray addObject: _nextProjectile]; 
     [self spriteMoveFinished]; 
    } 
    -(void) dealloc 
    { 
     self.arrowProjectileArray = nil; 
     self.nextProjectile = nil; 
     [super dealloc]; 
    } 

,我是包含的代碼,是有關箭頭的投影的唯一代碼。 箭頭射得很好,問題在於我每次拍攝這個愚蠢的東西時,我認爲它會拍攝一個新的箭頭,但是會在該箭頭上放置多個箭頭,並使其看起來像一個肥胖的箭頭像素。我究竟做錯了什麼?我不太熟悉NSMutableArray,但我目前被卡住了。

回答

-1

我做的一點點研究解決我自己的問題,我也擺脫了一批節點。

-1

在init()方法,創建一個新的NSMutableArray實例,並將其分配給self.arrowProjectileArray,那麼你遍歷arrowProjectileArray在使用for循環以下行。如果addChild:方法沒有向arrowProjectileArray添加任何內容,那麼你的代碼有一個邏輯錯誤,因爲你遍歷arrowProjectileArray所做的是遍歷一個空數組,這意味着你在那段代碼中什麼都不做。

你應該仔細檢查你打算做什麼,什麼你的代碼實際上做。

+0

我不認爲你閱讀所有的代碼...箭頭被添加到陣列中,他們拍出來的。這只是它重複使用相同的箭頭和做一些奇怪的事情。看看touchesBegan – Gabe