ios - 碰撞检测只发生在屏幕顶部

标签 ios objective-c cocos2d-iphone collision-detection removechild

我打算做一个非常简单的游戏,比如一个 Sprite 从顶部掉下来,我必须用另一个 Sprite 接住它,如果我不这样做, Sprite 就会跑出屏幕并移除,那部分没问题,我还添加了碰撞检测,主要问题是这种碰撞只发生在屏幕的上侧而不是屏幕的底部,为什么会发生这种情况请帮忙,(我是新手:S),这是完整的代码

在我的.h`然后.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];



    }
  }

最佳答案

我解决了这个问题,我只需要添加数组并用那个方法更新它们,

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


 }
}

关于ios - 碰撞检测只发生在屏幕顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15609689/

相关文章:

iphone - Cocoa/iPhone 中的多点触摸检测

iphone - MonoTouch 中有 cocos2d 的机会吗?

ios - 如何向 UITableView 添加部分?

ios - 在 iOS 项目中包含 C 库

iphone - 如何检查 UIView 是否是父 View 的 subview

ios - 使用来自 CryptoJS 的 base64 key 在 iOS 上进行 AES 加密

objective-c - 正确的cocos2d场景重启?

ios - Safari : no Inspectable applications

iphone - 如果仅在 Xcode 4.2 中使用 armv6 以支持新旧设备,有什么缺陷吗?

iphone - Objective C - 将科学记数法 "String"转换为 Double