2011-03-14 100 views
4

當調試下面的代碼中,XPOS = 0,yPos = 0,怪異cocos2d的位置座標

CCNode* node = [birdsAlone objectAtIndex:nextInactiveBird]; 
NSAssert([node isKindOfClass:[BirdEntity class]], @"not a bird entity"); 
BirdEntity* bird = (BirdEntity*) node; 
float birdHeight = CGRectGetHeight([bird boundingBox]); 
float xPos = ([[CCDirector sharedDirector] winSize].width - 100) * CCRANDOM_0_1() + 50; 
float yPos = ([[CCDirector sharedDirector] winSize].height + birdHeight/2.0f); 

CGPoint location = ccp(xPos, yPos); 
+0

[[CCDirector sharedDirector] winSize]和birdHeight的值是什麼?在調試器中找不到 – Andrew 2011-03-14 22:36:48

+0

。現在可以。我使用鏗鏘調試和分析,事實證明,birdAlone是零和節點是垃圾...我使用[CCArray arrayWithCapacity:20]而不是[[CCArray alloc] initWithCapacity],輸入太快^ _^ – John 2011-03-15 10:35:04

+0

嘿,那裏,是你在「調試」還是「釋放」模式下調試?你不應該得到birdsAlone ==零...另外,請指定這段代碼放在哪裏的函數。是birdsAlone類的成員?輸入參數? – Goles 2011-03-20 04:34:32

回答

1

當你0,你不要指望他們,兩個常見的原因有:

  • 整數四捨五入。例如,如果你做int x = 3 * .25,x將爲0.我不認爲這是你的問題。

  • 鏈條中的一個對象是零。請記住,在Cocoa中,[無anySelector]是有效的 - 它不會像你所期望的那樣與一個零對象一起崩潰。 [nil intValue](或floatValue等)== 0.所以,如果你有[[myObject someProperty] someOtherProperty],那麼沿途的任何事物都是0,結果將爲零。