cocos2d box2d 中的简单碰撞检测..碰撞时没有发生任何事情

发布于 2025-01-04 05:48:05 字数 3379 浏览 1 评论 0原文

我正在尝试弄清楚如何做到这一点,我有一个名为“player”的精灵和一个名为“rock”的精灵 我正在尝试检测两个精灵之间的碰撞......但碰撞时没有任何反应!

这就是我所做的:

-(void)addRock {

rock = [CCSprite spriteWithFile:@"rock.png" 
                                     rect:CGRectMake(0, 0, 27, 40)]; 

// Determine where to spawn the target along the X axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minX = rock.contentSize.width/2;
int maxX = winSize.width - rock.contentSize.width/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;
// Create the target slightly off-screen along the right edge,
// and along a random position along the X axis as calculated above
rock.position = ccp(actualX, 500);
rock.tag = 1;
[rockArray addObject:rock];
[self addChild:rock];

下面的播放器代码:

-(id) init{
if( (self=[super initWithColor:ccc4(255, 255, 255, 255)] )) {
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    player = [CCSprite spriteWithFile:@"Player.png" 
                                           rect:CGRectMake(0, 0, 27, 40)];
    player.position = ccp(winSize.width/2, winSize.height/4+15); //position of where the player is placed
    player.tag = 2;
    [playerArray addObject:player];
    [self addChild:player];

精灵移动完成:

-(void)spriteMoveFinished:(id)sender {


CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];

if (sprite.tag == 1) { // rock
    [rockArray removeObject:sprite];
} else if (sprite.tag == 2) { // players
    [playerArray removeObject:sprite];

init(数组初始化):

rockArray = [[NSMutableArray alloc]init];
    playerArray = [[NSMutableArray alloc]init];
    [self schedule:@selector(update:)];

更新方法

- (void)update:(ccTime)dt {


    NSMutableArray *rocksToDelete = [[NSMutableArray alloc] init];
    for (rock in rockArray) {
        CGRect rockRect = CGRectMake(
                                       rock.position.x - (rock.contentSize.width/2), 
                                       rock.position.y - (rock.contentSize.height/2), 
                                       rock.contentSize.width, 
                                       rock.contentSize.height);


    NSMutableArray *playersToDelete = [[NSMutableArray alloc] init];
    for (player in playerArray) {
        CGRect pRect = CGRectMake(

                                        player.position.x - (player.contentSize.width/2), 
                                        player.position.y -(player.contentSize.height/2), 
                                        player.contentSize.width,
                                        player.contentSize.height);

        if (CGRectIntersectsRect(rockRect, pRect)) {
            [rocksToDelete addObject:rock];
        }

        for (rock in rocksToDelete) {
            [rockArray removeObject:rock];
            [self removeChild:rock cleanup:YES];                                    
        }

        if (rocksToDelete.count > 0) {
            [playersToDelete addObject:player];
        }
        [rocksToDelete release];

        for (player in playersToDelete) {
            [playerArray removeObject:player];
            [self removeChild:player cleanup:YES];
        }
        [playersToDelete release];

我整晚都在尝试解决我的问题。我似乎可以让碰撞检测工作。如果是这样,有人可以给我看一个简短的代码吗?

I'm trying to figure out how to do this, I have a sprite called 'player' and sprites called 'rock'
I'm trying to detect collision between both sprites..... but nothing happens on collision!

heres what i have done:

-(void)addRock {

rock = [CCSprite spriteWithFile:@"rock.png" 
                                     rect:CGRectMake(0, 0, 27, 40)]; 

// Determine where to spawn the target along the X axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minX = rock.contentSize.width/2;
int maxX = winSize.width - rock.contentSize.width/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;
// Create the target slightly off-screen along the right edge,
// and along a random position along the X axis as calculated above
rock.position = ccp(actualX, 500);
rock.tag = 1;
[rockArray addObject:rock];
[self addChild:rock];

player code below:

-(id) init{
if( (self=[super initWithColor:ccc4(255, 255, 255, 255)] )) {
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    player = [CCSprite spriteWithFile:@"Player.png" 
                                           rect:CGRectMake(0, 0, 27, 40)];
    player.position = ccp(winSize.width/2, winSize.height/4+15); //position of where the player is placed
    player.tag = 2;
    [playerArray addObject:player];
    [self addChild:player];

sprite move finished:

-(void)spriteMoveFinished:(id)sender {


CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];

if (sprite.tag == 1) { // rock
    [rockArray removeObject:sprite];
} else if (sprite.tag == 2) { // players
    [playerArray removeObject:sprite];

init (array initialisation):

rockArray = [[NSMutableArray alloc]init];
    playerArray = [[NSMutableArray alloc]init];
    [self schedule:@selector(update:)];

update method

- (void)update:(ccTime)dt {


    NSMutableArray *rocksToDelete = [[NSMutableArray alloc] init];
    for (rock in rockArray) {
        CGRect rockRect = CGRectMake(
                                       rock.position.x - (rock.contentSize.width/2), 
                                       rock.position.y - (rock.contentSize.height/2), 
                                       rock.contentSize.width, 
                                       rock.contentSize.height);


    NSMutableArray *playersToDelete = [[NSMutableArray alloc] init];
    for (player in playerArray) {
        CGRect pRect = CGRectMake(

                                        player.position.x - (player.contentSize.width/2), 
                                        player.position.y -(player.contentSize.height/2), 
                                        player.contentSize.width,
                                        player.contentSize.height);

        if (CGRectIntersectsRect(rockRect, pRect)) {
            [rocksToDelete addObject:rock];
        }

        for (rock in rocksToDelete) {
            [rockArray removeObject:rock];
            [self removeChild:rock cleanup:YES];                                    
        }

        if (rocksToDelete.count > 0) {
            [playersToDelete addObject:player];
        }
        [rocksToDelete release];

        for (player in playersToDelete) {
            [playerArray removeObject:player];
            [self removeChild:player cleanup:YES];
        }
        [playersToDelete release];

I have been trying to sort my problem all night. I can seem to get the collision detection working. If so, can someone show me a brief code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

居里长安 2025-01-11 05:48:05

为了检查两个精灵之间的碰撞,您必须每帧(或您选择的频率)调用一个方法来检查是否发生了碰撞。
将以下代码添加到 init 方法中:

[self scheduleUpdate];

...这将在每个帧上调用以下方法(您必须添加到类实现中):

-(void)update:(ccTime)delta {
   if (CGRectIntersectsRect(player.boundingBox, rock.boundingBox)) {
       //Do Stuff
   }
}

为了使前面的代码正常工作,您必须将 Player 和 Rock sprite 声明为成员变量因此它们可以在整个课堂上使用。

如果场景中有多个岩石精灵,您想要检查碰撞,则必须使用数组。简单示例:

1 在类接口中声明一个数组:

NSMutableArray *rockArray;

2 在 init 方法中为其分配内存

rockArray = [NSMutableArray array];

3 在“addRock”方法中将创建的岩石精灵添加到数组中,然后作为子元素添加到场景

-(void) addRock {
   CCSprite *rock = [CCSprite spriteWithFile:@"rock.png" 
                                     rect:CGRectMake(0, 0, 27, 40)];
   //...

   [rockArray addObject:rock];
   [self addChild:[rockArray lastObject]];
}

中 4 在“update”方法中现在,您可以循环遍历数组并检查岩石和岩石之间的碰撞。玩家

for (CCSprite *_rock in rockArray) {
   if (CGRectIntersectsRect(player.boundingBox, rock.boundingBox)) {
           //Do Stuff e.g remove that rock from the scene
           [self removeChild:_rock cleanup:YES];
       }
}

希望这有帮助;-)如有任何问题,请随时提出

well to check for collision between the two sprites you'll have to call a method every frame (or how ever often you choose) to check if collision has occurred.
Add Following code to the init method:

[self scheduleUpdate];

...this will call following method (you have to add to your class implementation) on every frame:

-(void)update:(ccTime)delta {
   if (CGRectIntersectsRect(player.boundingBox, rock.boundingBox)) {
       //Do Stuff
   }
}

For the previous code to work you'll have to declare the Player and Rock sprite as member variables so they can be used throughout the class.

If you have more than one rock sprite in the scene you want to check collisions with you'll have to use an array. Quick example:

1 Declare an array in you class interface:

NSMutableArray *rockArray;

2 Allocate memory for it in the init method

rockArray = [NSMutableArray array];

3 In the "addRock" method add the created rock sprite to the array and then as a child to the scene

-(void) addRock {
   CCSprite *rock = [CCSprite spriteWithFile:@"rock.png" 
                                     rect:CGRectMake(0, 0, 27, 40)];
   //...

   [rockArray addObject:rock];
   [self addChild:[rockArray lastObject]];
}

4 In the "update" method you now loop through the array and check for collision between rock & player

for (CCSprite *_rock in rockArray) {
   if (CGRectIntersectsRect(player.boundingBox, rock.boundingBox)) {
           //Do Stuff e.g remove that rock from the scene
           [self removeChild:_rock cleanup:YES];
       }
}

Hope this helps ;-) Feel free to ask any questions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文