我有一個小小的弓箭手遊戲,我正在研究,以前在我的代碼中,我把每個箭頭精靈放到一個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中做錯了什麼,因爲它終止了以前的箭頭投影!
所以我的問題是:
- 我該如何解決這個問題。
- 我應該堅持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;
}
您爲_nextProjectile sprite上的標籤分配了哪些值? arrowTracker有什麼價值?是否有可能所有的精靈都被分配了相同的標籤? –
箭頭跟蹤器在每次觸摸時都會遞增 - 開始時,我在舊數組中使用它來跟蹤事物。我想我可以用它代替標籤號碼 – Gabe