• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:48:26 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

在我的 .h`然后 .m

{
CCSprite *right;
CCSprite *left;


NSMutableArray *_left;    
}




-(void)ringcreateccTime)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 scheduleselector(ringcreate  interval:2];
    [self scheduleselector(update];

}
return self;
  }


  -(void) ccTouchesBeganNSSet *)touches withEventUIEvent *)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) updateccTime)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];



    }
  }



Best Answer-推荐答案


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

-(void) updateccTime)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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap