2011-11-02 26 views
1

我有一個小小的弓箭手遊戲,我正在研究,以前在我的代碼中,我把每個箭頭精靈放到一個CCSprite[7];數組中,在ccTime裏我會更新x/y座標並做一些數學運算,使箭頭移動得很漂亮順利。所以所有的數學/角度/運動都起作用。CCSprite的VS CCSpriteBatchNode和一個NSMutable數組的數組?

後來,在嘗試實現碰撞檢測時,我無法使用一種使我的生活變得如此簡單的數據類型,我認爲它是CGRect,它會得到一個精靈的內容並檢查是否與另一個精靈相交。該錯誤說,我必須使用NSMutable數組的成員或類似的東西,這很好,因爲無論如何這對內存更好,把我的精靈放到一個batchNode和一個NSMutable數組中。但有一個問題。

在我看過的每一個教程中,投射物都會根據一個預定時間的動作序列進行移動。一個動作序列的只是一個例子(沒有在我的代碼)

id action = [Sequence actions: 
      [ScaleTo actionWithDuration:.3 scale:0.7f], 
      [ScaleTo actionWithDuration:.3 scale:1.0f], 
      nil]; 

無論我所看到的,這是精靈怎麼走動,但我不能這樣做,因爲箭的速度取決於被觸摸多久舉行變更,使箭頭看起來逼真的角度和類似的東西。

所以在我的touchesBegan代碼:

ccTouchesBegan:(NSSet *) blah blah { 
    ... 
    ... 
    self.nextProjectile = [[CCSprite spriteWithFile:@"arrow.png"]; 
    _nextProjectile.rotation = vector2 - 90; //this is angle of where the user touched screen 

    //add projectiles to array 
    _nextProjectile.tag = arrowTracker; 

    [_batchNode addChild:_nextProjectile z:1]; 
    [_projectiles addObject:_nextProjectile]; 

    //Release? If I don't have this the arrow fails to move at all... and it was in the tutorial 
    if(_nextProjectile.tag == 1){ 
      [_nextProjectile release]; 
      _nextProjectile = nil; 
     } 
    } 

每次我碰,第一個箭頭不拍出來(這不是問題,我可以解決這個問題很容易),箭頭拍攝出完美,運動與我使用CCSprite陣列時完全相同。唯一的問題是,如果以前的箭頭在飛行中,每次我撥打ccTouchesBegan,它都會停止所有動作並坐在那裏。半空中。所以我的問題是一個邏輯錯誤,顯然我在touchesBegan中做錯了什麼,因爲它終止了以前的箭頭投影!

所以我的問題是:

  1. 我該如何解決這個問題。
  2. 我應該堅持CCsprite [7](精靈數組)嗎?我不能找到圖像的內容,我可以找到箭頭的終點,只是檢查是否與另一個圖像相交,但這需要更多的工作/數學/記憶(我不太確定內存如何工作編程...但我很確定CCSprite陣列需要更多內存

編輯--------------------------- -------------------------------------------------- ----------

這是箭頭的位置被更新。

-(void)callEveryFrame:(ccTime)dt{ 
... 
... 

//move selected arrows 
for(int xe = 0; xe < 7; xe++{ 
float x = _theArrowArray[xe].position.x; 
float y = _theArrowArray[xe].position.y; 
vyArray[xe] += gravity; vyArray is the velocity on the y axis array, I'm just adding gravity 
x += vxArray[xe] *dt; // dt is after (ccTime) in the method definition 
y += vyArray[xe] *dt; 
CGPoint newLocation = CGPointMake(x,y); 
_theArrowArray[xe].position = newlocation; 
//The Code above this moves the arrows inside the CCSprite array, not the batch/nsmutable array. 

//The code below is just a copy and paste with a little change to it, for the batchnode/nsmutable 
float x2 = _nextProjectile.x; // mextProjectile was declared earlier in my code 
float y2 = _nextProjectile.y; 
vyArray[xe] += gravity; vyArray is the velocity on the y axis array, I'm just adding gravity 
x2 += vxArray[xe] *dt*1.2; // This way(dt*1.2), both arrows are being shot out but this one has more gravity to it, so you can tell which arrow is which and see that both are working. 
y2 += vyArray[xe] *dt*1.2; 
CGPoint newLocation2 = CGPointMake(x2,y2); 
_nextProjectile.position = newlocation2; 

} 
+0

您爲_nextProjectile sprite上的標籤分配了哪些值? arrowTracker有什麼價值?是否有可能所有的精靈都被分配了相同的標籤? –

+0

箭頭跟蹤器在每次觸摸時都會遞增 - 開始時,我在舊數組中使用它來跟蹤事物。我想我可以用它代替標籤號碼 – Gabe

回答

2

勿放彈,除非nextProj動物財產保留它。 CCSprite spriteWithFile返回一個自動釋放的對象,由batchNode和射彈陣列保留。奇怪的是,彈丸永遠不會有標籤== 1,所以釋放彈丸的代碼可能會被跳過。

我的猜測是關於#1,射彈將被停止,但它不會被刪除,因爲它仍然被添加到節點層次結構中。看到實際移除拋射物的代碼會很有幫助。

至於你的第二個問題,我不明白你的關注。你有7個投射物。他們是否使用7字節,700字節或7千字節無關緊要。與甚至最小的紋理相比,這種內存量仍然可以忽略不計。

幫你一個忙,並使用像NSMutableArray這樣的常規基礎集合來存儲你的對象。首先,他們將保留添加的對象並在刪除時釋放它們。如果你的代碼有一個導致數組溢出的錯誤,你也會得到錯誤。 C風格的數組可能會更快一些,可能會佔用較少的內存,但它們本質上也是不安全的,需要更加小心的處理。

+0

好吧,我更新了問題 – Gabe

+0

你能幫我解決嗎? – Gabe

+0

好的,如果你的箭頭停下來,陣列中可能會出現一些混淆。您應該逐步完成調試器中的代碼。這在SO上進行調試幾乎是不可能的,而且太本地化了。看到這篇文章的調試幫助:http://www.learn-cocos2d.com/2011/10/xcode-4-debugging-crashcourse/ – LearnCocos2D