2011-02-24 40 views
0

今天我得到了一个cocos2d的问题!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

这个怎么样

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

你好我想这一点,并得到了这让我的游戏崩溃的警告:?!一日一:“控制达到非空功能“(我删除它通过添加返回0;在最后)第二个:'游戏'可能不会回应' - 删除孩子'我应该如何处理第二个警告??谢谢你的帮助!! – DailyDoggy 2011-02-26 09:49:00

+1

对不起,我修复了警告,使用removeChild:cleanup :. – 2011-02-26 12:31:42

+0

OMG IT WORKS !!!!:D:D谢谢你帮我解决了我的问题!!!但是我不是letti嗯,你去那么容易XD我还有一个问题:我有两个矩形在屏幕上形成一个拱门。在这两个之上,我有第三个矩形,在它上面,我有一个球。如果我按下面的两个矩形之一,它们消失,smgr继续工作。但是如果我按上面的矩形它消失,但由于某种原因,球不会掉下来。你能帮我解决这个问题吗? – DailyDoggy 2011-02-26 13:44:45