2

我打算做一個非常簡單的遊戲,如從頂部下降的精靈,我必須用另一個精靈抓住它,如果我不精靈將會出現在屏幕之外並移除,那部分還行,我還加了碰撞檢測,主要問題是這次碰撞只發生在屏幕上方而不是屏幕底部碰撞,爲什麼會發生這種情況請幫忙,(我是新手:S),,這裏是完整的代碼碰撞檢測只發生在屏幕上方

在我.h`then .M

{ 
CCSprite *right; 
CCSprite *left; 


NSMutableArray *_left;  
} 




-(void)ringcreate:(ccTime)dt 
{ 

CGSize winsize = [[CCDirector sharedDirector] winSize]; 

int minX = left.contentSize.width/2; 
int maxX = winsize.width - left.contentSize.width/2; 
int rangeX = maxX - minX; 
int actualX = (arc4random() % rangeX) + minX; 



left = [CCSprite spriteWithFile:@"2.png"]; 
left.position = ccp(actualX,winsize.height); 
[self addChild:left]; 




id move3 = [CCMoveTo actionWithDuration:5 position:ccp(winsize.width/2,0)]; 


[left runAction:move3]; 

} 




-(id) init 
{ 
// always call "super" init 
// Apple recommends to re-assign "self" with the "super's" return value 
if((self=[super init])) { 

    self.touchEnabled = YES; 

    right = [CCSprite spriteWithFile:@"1.png"]; 
    right.position = ccp(0,0); 
    [self addChild:right]; 


    [self schedule:@selector(ringcreate:) interval:2]; 
    [self schedule:@selector(update:)]; 

} 
return self; 
    } 


    -(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
UITouch *touch =[touches anyObject]; 
CGPoint location =[touch locationInView:[touch view]]; 
location = [[CCDirector sharedDirector] convertToGL:location]; 




id move = [CCMoveTo actionWithDuration:1 position:ccp(location.x,location.y)]; 

[right runAction:move]; 



} 



-(void) update:(ccTime)dt 
{ 
    if (CGRectIntersectsRect(right.boundingBox, left.boundingBox)) { 

    CCLOG(@"collison hoyse"); 



    id move1 = [CCScaleTo actionWithDuration:0.4 scale:0.3]; 
    left.visible = NO; 
    [left runAction:move1]; 



    } 
    } 

回答

1

我解決了這個問題,我只需要添加陣列和更新他們打贏這種方法,

-(void) update:(ccTime)dt 
{ 
NSMutableArray *crabToUpdate = [[NSMutableArray alloc] init]; 
for (CCSprite *crab in crabarray) { 

    NSMutableArray *ring_to_delete = [[NSMutableArray alloc] init]; 
    for (ring1 in ringarray) { 

      if (CGRectIntersectsRect(crab.boundingBox, ring1.boundingBox)) {     
       [ring_to_delete addObject:ring1];      
      }  
    } 

     for (CCSprite *ring1 in ring_to_delete) { 
      [ringarray removeObject:ring1]; 
      [self removeChild:ring1 cleanup:YES]; 
     } 
    if (ring_to_delete.count >0) { 
     [crabToUpdate addObject:crab]; 
    } 

     [ring_to_delete release]; 


} 
} 
+1

非常感謝。你讓我今天一整天都感覺很好。 – Reza 2015-05-07 08:32:42

+0

:),歡迎你 – 2015-06-22 03:44:13