Cocos2d花栗鼠:触摸形状?

时间:2011-02-24 21:41:00

标签: iphone cocos2d-iphone touch shape chipmunk

今天我收到了 cocos2d 的问题!

我使用节点在我的.m文件中设置了一些形状空间管理器

- (CCNode*) createBlockAt:(cpVect)pt

                    width:(int)w
                   height:(int)h
                     mass:(int)mass
{
    cpShape *shape = [smgr addRectAt:pt mass:mass width:w height:h rotation:0];

    cpShapeNode *node = [cpShapeNode nodeWithShape:shape];
    node.color = ccc3(56+rand()%200, 56+rand()%200, 56+rand()%200);

    [self addChild:node];

    return node;
}

- (CCNode*) createCircleAt:(cpVect)pt
                     mass:(int)mass
                     radius:(int)radius
{
    cpShape *shape = [smgr addCircleAt:pt mass:mass radius:radius];
    cpShapeNode *node1 = [cpShapeNode nodeWithShape:shape];
    CCSprite *sprt = [CCSprite spriteWithFile:@"fire.png"];
    node1.color = ccc3(56+rand()%200, 56+rand()%200, 56+rand()%200);

    [self addChild:node1];

    return node1;
}

然后我实际制作形状,设置背景,分配并在我的init方法中启动空间管理器:

- (id) init
{
    [super init];

    CCSprite *background = [CCSprite spriteWithFile:@"BGP.png"];
    background.position = ccp(240,160);
    [self addChild:background]; 

    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];

    //allocate our space manager
    smgr = [[SpaceManagerCocos2d alloc] init];
    smgr.constantDt = 1/55.0; 

    [smgr addWindowContainmentWithFriction:1.0 elasticity:1.0 inset:cpvzero];

    [self createBlockAt:cpv(160,50) width:50 height:100 mass:100];
    [self createBlockAt:cpv(320,50) width:50 height:100 mass:100];
    [self createBlockAt:cpv(240,110) width:210 height:20 mass:100];
    [self createCircleAt:cpv(240,140) mass:25 radius:20];

    [smgr start];

    return self;
}

现在,我想要一个被触摸的形状被删除。最好的方法是什么? 我希望有人可以帮助我:)。

编辑: Plz某人,开始赏金!或者我从来没有得到这个回答:(

-DD

1 个答案:

答案 0 :(得分:1)

这样怎么样?

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint point = [self convertTouchToNodeSpace:touch];
    cpShape *shape = [smgr getShapeAt:point];
    if (shape) {
        [self removeChild:shape->data cleanup:YES];
        [smgr removeAndFreeShape:shape];
    }
    return YES;
}