Cocos2d 游戏中的碰撞检测?

2024-01-28

我正在尝试检测碰撞 of two sprites按照以下方式...但是当我尝试运行游戏时没有发生碰撞...我可能做错了什么?

- (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"); 
        }                       

}

射弹精灵从屏幕顶部到底部进行动画处理monkey精灵在底部从左到右进行动画,射弹穿过猴子,但日志没有被调用???

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

    }

}

您应该使用内置功能:

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

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

boundingBox 方法还考虑了更多因素,例如节点是否相对于其父节点定位。

另请注意,在 Objective-C 中,使用下划线作为变量前缀被认为是不好的做法。 Apple 为其内部库保留带有前导下划线的变量名称。如果确实需要对实例变量进行分类,一种常见的方法是在它们后面添加下划线。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Cocos2d 游戏中的碰撞检测? 的相关文章

随机推荐

  • 是否可以创建一些 IGrouping 对象

    I have List
  • 无法导入“D”:FLASK_APP

    from flask import Flask app Flask name app route def hello world return Hello World 我是烧瓶新手 我编写了这个基本代码并将其保存在 D Cat vs Dog
  • 原始类型、无界通配符和有界通配符

    我有一个简单的问题如下 这是关于整个问题的简单示例 List a new ArrayList List
  • 通过连续的字符串替换来提高循环的性能?

    我有 html 文本 我想更改 ouml 事物到真正的字符 如 等 否则 xml 包不接受它 所以我写了一个小函数来循环替换表 link1 http www w3schools com tags ref entities asp link2
  • 了解控制台应用程序中的 .net Core 依赖注入

    控制台应用程序不像网络应用程序那样将启动文件与配置服务一起使用 我正在努力理解依赖注入的关键概念 请注意以下示例无法编译 这是我认为它应该如何工作的基本示例 请指出任何非常规或错误的内容 static void Main string ar
  • SQL 将列转换为逗号分隔的行

    如果名称相同 我尝试将用户名字段组合成逗号分隔的字符串 电流输出 由于 Name Admin 有 4 个用户链接到它 我试图显示为 电子邮件受保护 cdn cgi l email protection 电子邮件受保护 cdn cgi l e
  • 如何从地址调用不同的合约?

    在 Solidity 以太坊 中 人们需要合约地址来调用该合约 contract KittyInterface function getKitty uint256 id external view returns bool isGestat
  • 从 8 位转换为 1 字节

    我有一个 8 位的字符串 我想将其转换为 1 个字节 我不确定为什么我的功能无法正常工作 我将 8 位存储到 8 个无符号字符的数组中 到目前为止 这是我的方法 unsigned int bitsToBytes unsigned char
  • 设置默认区域 - 避免在网站上的每个链接上使用 `, new {area = ""}`

    此代码位于母版页内 li a href gt Main site link a li li a href gt Area link a li 所有链接都运行良好 直到我转到区域链接 当我去那里时 主要区域的所有路线都不起作用 为了解决这个问
  • 无法在 nunit 测试中打开 sqlconnection

    我有一个奇怪的问题 我无法弄清楚 我试图围绕一些数据库代码编写一些集成测试 但我的单元测试因奇怪的异常而失败 在控制台应用程序下正常运行代码效果很好 public static class DatabaseManager public st
  • 在 MATLAB 中在轴外添加图例而不重新缩放

    我在 MATLAB 中有一个 GUI 其中预先放置了一组轴 我使用图例的位置属性将其放置在轴的右侧 但是 通过这样做 轴会重新缩放 以便轴 图例占据轴的原始宽度 有什么办法可以避免重新调整大小吗 Example x 0 1 10 y sin
  • 哈希迭代不返回子目录内容

    我有一个方法可以查找给定父目录的子目录 我将父目录存储在哈希中 然后将哈希作为参数传递 我试图将子目录的内容收集到一个数组中 然后将其输出到报告中 我遇到了一个问题 数组的内容仅将目录存储在哈希的最后一个值中 我很快意识到内容在循环的每次迭
  • NUnit 无法构建测试 - 未发现测试

    我正在研究selenium网络驱动程序项目 我能够在中构建测试Test Explorer并执行 重建解决方案时 我立即收到以下错误 Unit Adapter 3 2 0 0 Test discovery starting NUnit VS
  • 缩小 Octave / gnuplot

    我在 Windows 下使用 Octave 和 gnuplot 我可以使用鼠标右键进行放大 但如何缩小用户界面呢 I found 纳布尔上的这篇文章 http old nabble com zoom td16353082 html 紧迫p带
  • 向 UITableViewCell 添加边距

    I am trying to achieve a view I mocked out on sketch I ve replicated it on Android cause I m really good on that platfor
  • Delphi - Graphics32,绘制抗锯齿圆角矩形

    如何使用 Graphics32 绘制抗锯齿圆角矩形 我设法在 bitmap32 画布上使用 TPolygon 制作了一个普通矩形 但我找不到任何绘制圆角的参考 希望有一些代码 function GetRoundedFixedRectangl
  • 致命:提交时无法解析 HEAD 错误

    每当我尝试提交工作时 都会收到此错误 fatal could not parse HEAD 如果我想保留我的更改 该怎么办 你知道什么分行吗HEAD应该指向 是吗master Run git symbolic ref HEAD refs h
  • Concourse:通过 HTTP 请求触发作业

    我正在尝试使用 Git 服务器上的 Web 挂钩触发 Concourse 作业 按照此Github 上的问题 https github com concourse concourse issues 331我找到了一个端点定义 https g
  • 谁能告诉我为什么我的算法是错误的?

    我正在研究单源最短路径问题 我对 bfs 进行了修改 可以解决该问题 该算法运行时间为 O 2E 次 我只是不明白为什么它是错误的 一定是这样 否则 dijstra 不会是最有效的算法 def bfs modified G src des
  • Cocos2d 游戏中的碰撞检测?

    我正在尝试检测碰撞 of two sprites按照以下方式 但是当我尝试运行游戏时没有发生碰撞 我可能做错了什么 void update ccTime dt CGRect projectileRect CGRectMake project