Cocos2d Chipmunk:触摸形状?

发布于 2024-10-19 03:24:35 字数 1932 浏览 1 评论 0 原文

今天我有一个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;
}

现在,我希望在触摸某个形状时将其删除。最好的方法是什么??? 我希望有人可以帮助我:)

编辑: 请有人开始赏金!或者我永远不会得到这个答案:(

-DD

Today i got a cocos2d question!

I got a few shapes and the space-manager set up in my .m file by using nodes:

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

then i actually make the shapes, set up a background, alloc and start the space-manager in my init method:

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

Now, i want a shape to be removed if it's touched. What is the best way to do that???
I hope that somebody out there can help me :)

Edit:
Plz somebody, start a bounty! Or i never get this answered :(

-DD

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-10-26 03:24:35

像这样怎么样?

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

How about like this?

- (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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文