菜鸟教程小白 发表于 2022-12-12 09:48:26

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


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

<p>在我的 .h`然后 .m</p>

<pre><code>{
CCSprite *right;
CCSprite *left;


NSMutableArray *_left;   
}




-(void)ringcreate:(ccTime)dt
{

CGSize winsize = [ 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 = ;
left.position = ccp(actualX,winsize.height);
;




id move3 = ;


;

}




-(id) init
{
// always call &#34;super&#34; init
// Apple recommends to re-assign &#34;self&#34; with the &#34;super&#39;s&#34; return value
if( (self=) ) {

    self.touchEnabled = YES;

    right = ;
    right.position = ccp(0,0);
    ;


    ;
    ;

}
return self;
}


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




id move = ;

;



}



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

    CCLOG(@&#34;collison hoyse&#34;);



    id move1 = ;
    left.visible = NO;
    ;



    }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我解决了这个问题,我只需要添加数组并使用该方法更新它们,</p>

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

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

            if (CGRectIntersectsRect(crab.boundingBox, ring1.boundingBox)) {                  
                ;                  
            }      
    }

      for (CCSprite *ring1 in ring_to_delete) {
            ;
            ;
      }
    if (ring_to_delete.count &gt;0) {
      ;
    }

      ;


}
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 碰撞检测只发生在屏幕顶部,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/15609689/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/15609689/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 碰撞检测只发生在屏幕顶部