2011-02-24 54 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