Cocos2d游戏中的碰撞检测? [英] Collision Detection in Cocos2d game?

查看:126
本文介绍了Cocos2d游戏中的碰撞检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以下列方式检测两个精灵冲突:但是当我尝试运行游戏时不会发生碰撞....我可能做错了吗?

   - (void)update:(ccTime)dt {


$ b b CGRect projectileRect = CGRectMake(projectile.position.x - (projectile.contentSize.width / 2),
projectile.position.y - (projectile.contentSize.height / 2),
projectile.contentSize。 width,
projectile.contentSize.height);

// CGRectMake(0,220,320,50);
CGRect targetRects = CGRectMake(_monkey.position.x - (_monkey.contentSize.width / 2),
_monkey.position.y - (_monkey.contentSize.height / 2),
_monkey .contentSize.width,
_monkey.contentSize.height);

if(CGRectIntersectsRect(projectileRect,targetRects)){
NSLog(@ha ha Collision detected);
}

}



projectile sprite是从屏幕顶部到底部的动画,猴子 sprite是从左到右在底部通过猴子的动画,但日志不会被调用

   - (void)update:(ccTime)dt {

CGRect projectileRect = [弹丸边界框];
CGRect targetRects = [_monkey boundingBox];

if(CGRectIntersectsRect(projectileRect,targetRects))
{
NSLog(@ha ha Collision detected);
}

CGRect projectileRects = CGRectMake(projectile.position.x - (projectile.contentSize.width / 2),
projectile.position.y - (projectile.contentSize.height / 2),
projectile.contentSize.width,
projectile.contentSize.height);
CGRect targetRect = CGRectMake(_monkey.position.x - (_monkey.contentSize.width / 2),
_monkey.position.y - (_monkey.contentSize.height / 2),
_monkey .contentSize.width,
_monkey.contentSize.height);
if(CGRectIntersectsRect(projectileRects,targetRect)){
NSLog(@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ );
}

}



- (void)spriteMoveFinished:(id)sender {

  // NSLog(@spriteMoveFinished); 
CCSprite * sprite =(CCSprite *)sender;
[self removeChild:sprite cleanup:YES];

if(sprite.tag == 1){
[_targets removeObject:sprite];



} else if(sprite.tag == 2){
[_projectiles removeObject:sprite];
}

}



- (void)addTarget {

  projectile = [CCSprite spriteWithFile:@egg.pngrect:CGRectMake(0, 10,10)]; 
projectile.position = ccp(_bear.position.x,_bear.position.y-20);
projectile.tag = 2;
[self addChild:projectile];

CGPoint realDest = ccp(_bear.position.x,_bear.position.y - 380);

int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration =(arc4random()%rangeDuration)+ minDuration;

//将炮弹移动到实际端点
[projectable runAction:[CCSequence actions:
[CCMoveTo actionWithDuration:actualDuration position:realDest],
[CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished :)],
nil]];

//添加到projectiles数组
projectile.tag = 2;
[_projectiles addObject:projectile];

}

   - (void)registerWithTouchDispatcher 
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; (UIEvent *)触摸withEvent:(UIEvent *)触摸事件触发事件(UIEvent *)触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件触发事件)event {

  CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; 


if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation))
{
if(![_ walkMonkey isDone]){
[_monkey runAction:_walkMonkey];
}


}
else {

}

return YES;

}



- (void)ccTouchEnded :(UITouch *)touch withEvent:(UIEvent *)event {

  CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; 

if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation))
{

[_monkey stopAction:_walkMonkey];

}

}

   - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {

CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];

CGPoint translation = ccpSub(touchLocation,oldTouchLocation);

if(translation.x> 3){

_monkey.flipX = YES;
}
else if(translation.x< -3){
_monkey.flipX = NO;

}

if(CGRectContainsPoint(CGRectMake(40,0,240,50),touchLocation))
{

CGPoint newPos = ccpAdd (translation,_monkey.position);
if(newPos.x> = 320 || newPos.x< = 20)
{
NSLog(@monkey not walking);
}
else {
newPos.y = 100;
_monkey.position = newPos;
}

}

}


解决方案

您应该使用内置的功能:

  CGRect projectileRect = boundingBox]; 
CGRect targetRects = [_monkey boundingBox];

if(CGRectIntersectsRect(projectileRect,targetRects))
{
NSLog(@ha ha Collision detected);
}

boundingBox方法需要考虑更多的事情,例如,



另请注意,在变量前面加上下划线在Objective-C中被认为是不好的做法。 Apple保留其内部库的带有前导下划线的变量名。如果你真的需要分类实例变量,一个常见的方法是用下划线后缀。


i am trying to detect Collision of two sprites in the following way...but when i try to run the game no collision takes place.... what am i possibly doing wrong??

- (void)update:(ccTime)dt {



    CGRect projectileRect = CGRectMake(projectile.position.x - (projectile.contentSize.width/2), 
                                       projectile.position.y - (projectile.contentSize.height/2), 
                                       projectile.contentSize.width, 
                                       projectile.contentSize.height);

    //CGRectMake(0,220,320,50);
    CGRect targetRects =  CGRectMake(_monkey.position.x - (_monkey.contentSize.width/2), 
                                 _monkey.position.y - (_monkey.contentSize.height/2), 
                                 _monkey.contentSize.width, 
                                 _monkey.contentSize.height);

        if (CGRectIntersectsRect(projectileRect, targetRects)) {
                    NSLog(@"ha ha Collision detected"); 
        }                       

}

projectile sprite is animating from top of screen to the bottom and monkey sprite is animating from left to right at the bottom the projectile goes through the monkey but the log does not get called???

- (void)update:(ccTime)dt {

CGRect projectileRect = [projectile boundingBox];
CGRect targetRects = [_monkey boundingBox];

if (CGRectIntersectsRect(projectileRect, targetRects))
{
    NSLog(@"ha ha Collision detected");
}

CGRect projectileRects = CGRectMake(projectile.position.x - (projectile.contentSize.width/2), 
                                   projectile.position.y - (projectile.contentSize.height/2), 
                                   projectile.contentSize.width, 
                                  projectile.contentSize.height);
CGRect targetRect = CGRectMake(_monkey.position.x - (_monkey.contentSize.width/2), 
                               _monkey.position.y - (_monkey.contentSize.height/2), 
                               _monkey.contentSize.width, 
                              _monkey.contentSize.height);
if (CGRectIntersectsRect(projectileRects, targetRect)) {
    NSLog(@"@@@@@@@@@@@@@@@@@@@@@@@@@@@@");             
}

}

-(void)spriteMoveFinished:(id)sender {

//NSLog(@"spriteMoveFinished");
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];

if (sprite.tag == 1) { 
    [_targets removeObject:sprite];



} else if (sprite.tag == 2) { 
    [_projectiles removeObject:sprite];
}

}

-(void)addTarget {

projectile = [CCSprite spriteWithFile:@"egg.png" rect:CGRectMake(0, 0, 10, 10)];
projectile.position = ccp(_bear.position.x,_bear.position.y-20);
projectile.tag=2;
[self addChild:projectile];

CGPoint realDest = ccp(_bear.position.x, _bear.position.y - 380);

int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;

// Move projectile to actual endpoint
[projectile runAction:[CCSequence actions:
                       [CCMoveTo actionWithDuration:actualDuration position:realDest],
                       [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
                       nil]];

// Add to projectiles array
projectile.tag = 2;
[_projectiles addObject:projectile];

}

-(void) registerWithTouchDispatcher
{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {

CGPoint touchLocation = [self convertTouchToNodeSpace:touch];


if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation)) 
{
    if (![_walkMonkey isDone]) {
        [_monkey runAction:_walkMonkey];
    }


}
else {

}

return YES;

}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {

CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation)) 
{

    [_monkey stopAction:_walkMonkey];

}

}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {       

    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

    CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
    oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];

    CGPoint translation = ccpSub(touchLocation, oldTouchLocation);    

    if (translation.x > 3) {

        _monkey.flipX=YES;
    }
    else if (translation.x < -3){
        _monkey.flipX=NO;

    }

    if(CGRectContainsPoint(CGRectMake(40,0,240,50),touchLocation)) 
    {

        CGPoint newPos = ccpAdd(translation,_monkey.position);
        if(newPos.x >= 320 || newPos.x <= 20)
        {
            NSLog(@"monkey not walking");
        }
        else {
            newPos.y = 100;
            _monkey.position = newPos;
        }

    }

}

解决方案

You should use the built-in functionality:

CGRect projectileRect = [projectile boundingBox];
CGRect targetRects = [_monkey boundingBox];

if (CGRectIntersectsRect(projectileRect, targetRects))
{
    NSLog(@"ha ha Collision detected");
}

The boundingBox method takes a couple more things into account, for example if the node is positioned relative to its parent.

Also note that prefixing variables with an underscore is considered bad practice in Objective-C. Apple reserves variable names with leading underscores for their internal libraries. If you really need to classify instance variables, one common way is to suffix them with an underscore.

这篇关于Cocos2d游戏中的碰撞检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆