2012-07-02 48 views
0

有人知道爲什麼我永遠不會獲得我的數組的第一個值嗎?它總是從I + 1的指數,當我開始在for循環爲0,或1喜歡這裏:不是X = 44,控制檯說,X = 100:CGPoint數組中的索引從i + 1開始?

//at the top 
#define kMaxHillKeyPoints 5 

//in the .h: 
CGPoint _hillKeyPoints[kMaxHillKeyPoints]; 

- (void)generatePath { 

    int _nVertices = 1; 

    _hillKeyPoints[_nVertices] = CGPointMake(44, 0); 
    _hillKeyPoints[_nVertices++] = CGPointMake(100, 75); 
    _hillKeyPoints[_nVertices++] = CGPointMake(50, 150); 
    _hillKeyPoints[_nVertices++] = CGPointMake(150, 225); 

    for(int i = 1; i < 4; i++) { 
     CCLOG(@" _hillKeyPoints[1].x : %f", _hillKeyPoints[1].x); 
     CCLOG(@"%i", i); 
    } 
} 

//output : 
_hillKeyPoints[1].x : 100.000000 //why not x = 44 ? 

你知道爲什麼嗎?我也清理了這個項目,但它並沒有改變任何東西。

感謝

回答

2

首先,你做了以下內容:

int _nVertices = 1; 
_hillKeyPoints[_nVertices] = CGPointMake(44, 0); //_nVertices = 1 

這就賦予_hillKeyPoints [1] - (44,0)。在這裏,你仍然很好(你可以在這裏NSLog驗證)。

然而,在下面的語句:

_hillKeyPoints[_nVertices++] = CGPointMake(100, 75); 

後遞增 _nVertices。這意味着_hillKeyPoints [_nVertices]首先分配給(100,75),,然後值_nVertices增加。上面的說法是完全等同於這樣做:

_hillKeyPoints[_nVertices] = CGPointMake(100, 75); 
_nVertices = _nVertices + 1; 

注意_nVertices = 1這裏任職期間,所以你要覆蓋以前的分配(44,0),因此你會得到_hillKeyPoints [1] =( 100,75)。

int _nVertices = 1; 
_hillKeyPoints[_nVertices] = CGPointMake(44, 0); //_nVertices = 1 
_hillKeyPoints[++_nVertices] = CGPointMake(100, 75); //_nVertices = 2 
_hillKeyPoints[++_nVertices] = CGPointMake(50, 150); //_nVertices = 3 
_hillKeyPoints[++_nVertices] = CGPointMake(150, 225); //_nVertices = 4 

希望這有助於:

如果你還想做你的方式,你可以在每次預先遞增索引。

+0

謝謝kentoh,它現在可以工作!謝謝你的幫助! – Paul

+0

歡迎您:) –

相關問題