Does someone know why i never get the first value of my array? it always starts at the index i+1, when i start the for loop at 0, or like here at 1 : instead of x=44, the console says x=100 :
//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 ?
Would you know why? i cleaned the project also, but it does not change anything.
Thanks
First, you did the following:
This assigns _hillKeyPoints[1] to (44,0). Here, you are still good (you can NSLog here to verify).
However, in the following statement:
you are post-incrementing _nVertices. This means that _hillKeyPoints[_nVertices] is first assigned to (100,75), then the value _nVertices is incremented. The statement above is exactly equivalent to doing this:
Note that _nVertices = 1 here during assignment, so you are overwriting your previous assignment of (44, 0), and hence you get _hillKeyPoints[1] = (100,75) in the end.
If you still want to do it your way, you can pre-increment the index each time:
Hope this helps.