2010-07-07 127 views
1

我正在嘗試用Cocos2d和Chipmunk(通過SpaceManager)創建一個柔軟的球,使用一系列全部鏈接在一起的Rects,然後將彈簧連接到中央機構。如何將SpaceManager cpShape存儲在類似數組的東西中?

Something like these examples

但爲了做到這一點,我想我需要所有的cpShapes存儲在數組中,我創造了他們,所以我可以再遍歷數組將它們一起與約束鏈接後。

但是,當我嘗試將cpShapes放入數組中時,出現錯誤,告訴我它是「不兼容的指針類型」。所以......我要麼需要使用除數組之外的其他東西(我試過一組,也不管用),或者我需要對形狀做些什麼來使其兼容......但是什麼?或者我需要另一個aproach。

任何想法?

下面的代碼到目前爲止它應該是有關...

- (id) init 
{ 

    if ((self = [super init])) { 

     SpaceManager * spaceMgr = [[SpaceManager alloc] init]; 
     [spaceMgr addWindowContainmentWithFriction:1.0 elasticity:1.0 inset:cpv(5, 5)]; 

      // This is a layer that draws all the chipmunk shapes 
     ChipmunkDrawingLayer *debug = [[ChipmunkDrawingLayer node] initWithSpace:spaceMgr.space]; 
     [self addChild:debug]; 

     int ballPeices = 10; // the number of peices I want my ball to be composed of 
     int ballRadius = 100; 
     float circum = M_PI * (ballRadius * 2); 

     float peiceSize = circum/ballPeices; 
     float angleIncrement = 360/ballPeices; 

     CGPoint origin = ccp(240, 160); 
     float currentAngleIncrement = 0; 

      NSMutableArray *peiceArray = [NSMutableArray arrayWithCapacity:ballPeices]; 

     for (int i = 0; i < ballPeices; i++) { 

      float angleIncrementInRadians = CC_DEGREES_TO_RADIANS(currentAngleIncrement); 

      float xp = origin.x + ballRadius * cos(angleIncrementInRadians); 
      float yp = origin.y + ballRadius * sin(angleIncrementInRadians); 

        // This is wrong, I need to figure out what's going on here. 
      float peiceRotation = atan2(origin.y - yp, origin.x - xp); 

      cpShape *currentPeice = [spaceMgr addRectAt:ccp(xp, yp) mass:1 width:peiceSize height:10 rotation:peiceRotation]; 

      currentAngleIncrement += angleIncrement; 

        [peiceArray addObject:currentPeice]; //!! incompatible pointer type 

     } 



     spaceMgr.constantDt = 0.9/55.0; 
     spaceMgr.gravity = ccp(0,-980); 
     spaceMgr.damping = 1.0; 

    } 

    return self; 

} 
+0

你在哪裏添加彈簧約束?我沒有看到它 – slf 2010-07-07 17:04:59

+0

我還沒有得到那麼多。我想我需要一種方法來創建它們後訪問這些形狀。要做到這一點,我需要讓他們進入數組或其他東西。 – gargantuan 2010-07-07 19:50:12

回答

0

不兼容的指針類型是很容易解釋:)

[NSMutableArray addObject]的定義是這樣的:

- (void)addObject:(id)anObject 

那麼什麼是id呢?偉大的問題!請記住,Objective-C仍然是C核心。按照Objective-C Programming Guide

typedef struct objc_object { 
    Class isa; 
} *id; 

這是偉大的,現在我們知道*id而是id本身是什麼?這就是方法簽名中引用的內容。爲此,我們要看看objc.h

typedef id (*IMP)(id, SEL, ...); 

顯然,cpSpace*不符合這一點,所以如果您嘗試使用該消息把那些進入NSMutableArray你會得到不兼容的指針類型。

+0

所以如果我不能把它們放在一個數組中,我可以把它們放進去嗎?我可以將其他對象放入數組中。 – gargantuan 2010-07-07 19:51:26

+0

你可以把它們放在從NSObject繼承的東西里面,然後在NSMutableArray中存放它。有時這被稱爲「拳擊」 – slf 2010-07-08 13:10:53

+0

此外,你可以隨時保持一個普通的舊C數組 – slf 2010-07-08 13:11:36

相關問題