我正在編寫一個使用cocos 2d的遊戲,它涉及很多Float類型屬性來確定我的形狀(Square,Triangle)的頂點(x,y)。我有一個形狀類,它只保存頂點(int)和頂點(CGFloat *)的總數。我有一個叫做ShapeHolder的類,它定義了不同類型形狀的值,它具有名爲Shape square; and Shape Triangle
的屬性。在ShapeHolder INIT正在存儲(硬編碼)的頂點信息(X,Y)如下,AM還印刷其值完美地工作Objective C浮動值在內部轉換 - 不知道爲什麼
CGPoint _square[] = {
CGPointMake(100.0f, 100.0f) ,
CGPointMake(200.0f, 100.0f) ,
CGPointMake(200.0f, 200.0f) ,
CGPointMake(100.0f, 200.0f)
};.
square = [[[Shape alloc] initWithVerticesLength: 4] retain];
square.vertices = _square;
for (int i = 0; i< square.count; i++) {
NSLog(@"Init Square Vertices X Y = %f , %f" , square.vertices[i].x ,square.vertices[i].y);
}
triangle = [[[Shape alloc] initWithVerticesLength: 3] retain];
triangle.vertices = _triangle;
問題時遇到是上而檢索相同的定義後,立即我在OpenGL繪圖類值來呈現不同的形狀,這裏是代碼
allShapes = [[ShapeHolder alloc] init];
for (int i = 0; i< allShapes.square.count; i++) {
NSLog(@"Main Square Vertices X Y = %f , %f" , [[allShapes square]vertices][i].x ,[[allShapes square] vertices][i].y);
}
值以某種方式轉化,不知道爲什麼,請澄清。我沒有得到任何運行時錯誤或編譯這是真正困擾我的知識
Init Square Vertices X Y = 100.000000 , 100.000000
Init Square Vertices X Y = 200.000000 , 100.000000
Init Square Vertices X Y = 200.000000 , 200.000000
Init Square Vertices X Y = 100.000000 , 200.000000
Main Square Vertices X Y = 100.000000 , 100.000000
Main Square Vertices X Y = -1.998910 , 0.000000
Main Square Vertices X Y = 0.000000 , 0.000000
Main Square Vertices X Y = -1.998912 , 0.000000
看起來像是你正在做一個數組指針,即_square的淺拷貝。您需要將每個頂點深度複製到對象中的數組中。 – 2011-05-28 21:34:18
你節省了我的時間,非常感謝。還有一個問題我可以知道,在班級內的第一個打印值是如何正確打印的?我也正在閱讀淺拷貝與深拷貝儀式。非常感謝。 – Anandh 2011-05-28 21:49:58
很難說,不看代碼。取決於_square如何產生。 – 2011-05-28 21:58:04