1

我正在尝试使用 Cocos2d 和 Chipmunk(通过 SpaceManager)创建一个柔软的球,使用一堆 rects 全部链接在一起,然后用弹簧连接到一个中央主体。

像这些例子

但是为了做到这一点,我认为我需要在创建它们之后将所有 cpShapes 存储在一个数组中,这样我就可以循环遍历数组以将它们与约束链接在一起。

但是,当我尝试将 cpShapes 放入数组时,我收到一个错误消息,告诉我它是“不兼容的指针类型”。所以......我要么需要使用数组以外的东西(我已经尝试过一个集合,但也没有用)或者我需要对形状做一些事情以使其兼容......但是什么?或者我需要另一种方法。

有任何想法吗?

这是到目前为止的代码应该是相关的......

- (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;

}
4

1 回答 1

0

不兼容的指针类型很容易解释:)

[NSMutableArray addObject]被定义为:

- (void)addObject:(id)anObject

那么什么是an idthen?好问题!请记住,Objective-C 的核心仍然是 C。根据Objective-C 编程指南

typedef struct objc_object {
    Class isa;
} *id;

太好了,现在我们知道了,*idid它本身呢?这就是方法签名中引用的内容。为此,我们必须查看objc.h

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

显然,cpSpace*不适合,因此如果您尝试将它们放入NSMutableArray使用该消息,您将获得不兼容的指针类型。

于 2010-07-07T18:14:30.373 回答