检测 Cocos2d-iphone 上是否触摸了特定精灵

2023-12-12

我正在按照 Ray 的教程制作一个简单的 iPhone 游戏(此处:http://goo.gl/fwPi),并决定我希望敌人在被触碰时被消灭。

我最初的方法是在触摸位置生成一个小的 CCSprite 精灵,然后使用 CGRectMake 创建所述精灵的边界框以检测敌人精灵是否被触摸。就像雷对射弹/敌人所做的那样。但当然,我的方法行不通,我无法把自己从这个洞里挖出来。

这是相关的代码片段。任何帮助表示赞赏:



- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    // Choose one of the touches to work with
    UITouch *touch = [touches anyObject];
    CGPoint location = [self convertTouchToNodeSpace: touch];
    location = [[CCDirector sharedDirector] convertToGL:location];
    CCSprite *touchedarea = [CCSprite spriteWithFile:@"Icon-72.png" rect:CGRectMake(location.x, location.y, 2, 2)];
    touchedarea.tag = 2;
    [self addChild:touchedarea];
    [_touchedareas addObject:touchedarea];

}



- (void)update:(ccTime)dt {

    NSMutableArray *touchedareasToDelete = [[NSMutableArray alloc] init];
    for (CCSprite *touchedarea in _touchedareas) {
        CGRect touchedareaRect = CGRectMake(
                                           touchedarea.position.x, 
                                           touchedarea.position.y, 
                                           touchedarea.contentSize.width, 
                                           touchedarea.contentSize.height);

        NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
        for (CCSprite *target in _targets) {
            CGRect targetRect = CGRectMake(
                                           target.position.x - (target.contentSize.width/2), 
                                           target.position.y - (target.contentSize.height/2), 
                                           target.contentSize.width, 
                                           target.contentSize.height);

            if (CGRectIntersectsRect(touchedareaRect, targetRect)) {
                [targetsToDelete addObject:target];             
            }                       
        }

        for (CCSprite *target in targetsToDelete) {
            [_targets removeObject:target];
            [self removeChild:target cleanup:YES];                                  
        }

        if (targetsToDelete.count > 0) {
            [touchedareasToDelete addObject:touchedarea];
        }
        [targetsToDelete release];
    }

    for (CCSprite *touchedarea in touchedareasToDelete) {
        [_touchedareas removeObject:touchedarea];
        [self removeChild:touchedarea cleanup:YES];
    }
    [touchedareasToDelete release];
}
  

这看起来是一种非常困难的方法。我自己编码时间不长,但也许以下内容可能对您有所帮助。 假设你有一个名为敌人的 nsmutable 数组,并且在创建敌人时将新的敌人对象添加到该数组中。敌人对象将是一个 ccnode,并且其中有一个名为 _enemySprite 的 ccsprite 然后进行触摸

 -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {

    NSSet *allTouches = [event allTouches];
    UITouch * touch = [[allTouches allObjects] objectAtIndex:0];
    //UITouch* touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location]; 

    int arraysize = [enemies count];
    for (int i = 0; i < arraysize; i++) {


        if (CGRectContainsPoint( [[[enemies objectAtIndex:i] _enemySprite] boundingBox], location)) {

            //some code to destroy ur enemy here


        }
    }
    //  NSLog(@"TOUCH DOWN");

}

希望这可以帮助

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

检测 Cocos2d-iphone 上是否触摸了特定精灵 的相关文章

  • 将小箭头添加到 iPhone TableView 单元格中单元格的右侧

    这应该很简单 我有一个带有 TableView 的 iPhone 应用程序 如何将经典的小箭头添加到每个单元格的右侧 只需设置相应的附件类型的财产UITableViewCell cell accessoryType UITableViewC
  • _ 和 self 之间的区别。在 Objective-C 中

    使用下划线和使用selfObjective C 中调用时的关键字 property 财产申报 property weak nonatomic NSString myString Calling synthesize关于物业 synthesi
  • 比较具有相同值但顺序不同的两个数组

    我有 2 个 nsarray 具有相同的值但顺序不同 NSArray array1 0 1 2 3 NSArray array2 2 3 1 0 我需要一种方法来确定两个数组是否具有不同顺序的相同值 Kind of BOOL isSameV
  • CoreBluetooth广告检测时间

    这个问题早在10月份就已经讨论过here https stackoverflow com questions 12866551 corebluetooth connection setup time varies quite a bit 1
  • MPMediaItemPropertyAssetURL 仅针对 iPhone 5s 返回 null

    我一直在使用以下代码从 MPMediaItemPickerController 返回的 MPMediaItem 对象中提取资产 url 以便我可以将音乐文件从用户 iPhone itunes 音乐库复制到文档文件夹进行处理 但在 iPhon
  • nspredicate 数组中的多个项目

    如何使用NSPredicate满足多个条件 NSEntityDescription entity NSEntityDescription entityForName MessageData inManagedObjectContext ma
  • 如何在松开按钮后立即看到新的视频层?

    我有一个应用程序 用户可以按住按钮来拍摄视频 然而 当他们这样做时 带有视频播放的新图层不会立即出现 相反 有一个非常短的延迟 在用户松开按钮后 您可以看到相机仍然显示相机所看到的内容 延迟结束后 视频立即显示并开始播放 但是 我怎样才能使
  • 如何在 iPhone 应用程序中使用正则表达式以 , (逗号)分隔字符串

    我必须读取包含三列的 csv 文件 在解析 csv 文件时 我得到了这种格式的字符串克里斯托弗 巴斯 为心爱的国家哭泣 期末论文 电子邮件受保护 cdn cgi l email protection 我想将三列的值存储在一个数组中 所以我使
  • 使用 PhoneGap + iPhone 上传文件

    据我所知 PhoneGap 应用程序大部分 如果不是全部 都是 HTML5 CSS JavaScript iPhone 本身不提供上传文件的控件 PhoneGap是否提供任何允许用户上传文件的机制 图片 视频 以 iPhone 为例 我知道
  • UITableView 最后一个单元格被 UITabBarController 隐藏

    我面临一个奇怪的问题 我有一个UITableView在最初的UIViewController of the UITabBarController 起初一切都很好 但是当我更改选项卡并返回到第一个选项卡时 最后一个单元格UITableView
  • GCD 和线程

    我想了解一些有关 GCD 和线程的知识 我的视图控制器中有一个 for 循环 它要求我的模型执行一些异步网络请求 因此 如果循环运行 5 次 模型将发出 5 个网络请求 考虑到我正在使用 NSURLConnection 的 sendAsyn
  • iPhone SDK - orkut API

    我曾使用 gData 库从 iPhone 中的 Google 联系人中检索联系人信息 同样 我将与 Orkut 合作 有没有像 gData 这样适用于 Orkut 的库 如果是这样 请给我链接 提前致谢 对于 orkut 我们有 orkut
  • UITableViewCell 的 viewDidAppear

    我通常使用viewDidAppear方法在视图完成出现后在视图上执行一些 UI 操作 我在各种情况下使用了此方法 它非常有用 但是 我需要在视图上进行一些 UI 更改UITableViewCell当它完成出现后 SDK中是否有任何可用的方法
  • 从视频生成缩略图 - ios7

    我用这个作为参考 从视频 URL 或 iPhone SDK 中的数据获取缩略图 https stackoverflow com a 6027285 1145339 该方法使用 MPMoviePlayerController 类而不是 AVF
  • &(与符号)和 && 或 | 之间的区别(管道)和||在 Objective-C 中?

    我想知道Objective C是否关心我是写 还是 我相信一个与号 会或应该导致如果左侧已经为假 则右侧将不会被评估 这适用于 Objective C 吗 Yes 这些运算符在 C 和 Objective C 中的功能相同 就像在 C 中一
  • 方向改变时重新定位控件

    我知道自动布局可用于在方向改变时使尺寸和位置保持一致 当方向改变时 是否可以完全改变布局 例如 请查看下面的纵向模式下简单登录屏幕的线框 现在 如果我旋转设备 我想完全重新定位控件 这种事情可以用自动布局来完成吗 如果没有 我该怎么办 谢谢
  • rightBarButtonItem 信息按钮,右侧没有空格

    我有一个UIViewController设置为在其右侧显示一个信息按钮UINavigationItem像这样 UIButton infoButton UIButton buttonWithType UIButtonTypeInfoLight
  • Swift 相当于 Objective-C FourCharCode 单引号文字(例如 'TEXT')

    我正在尝试在 Swift 中复制一些 Objective C cocoa 一切都很好 直到我遇到以下情况 Set a new type and creator unsigned long type TEXT unsigned long cr
  • 如何以编程方式设置设备(UI)方向?

    希望屏幕 UI 上的所有内容都能够从横向左向右旋转 反之亦然 我该怎么做呢 这是私人的吗 我知道 BOOL shouldAutorotateToInterfaceOrientation UIInterfaceOrientation inte
  • 无法从 iOS 中的框架访问 .nib(XIB) 文件

    我已经从现有的代码库中创建了一个框架 并尝试在新的代码库中使用它 这很好用 但是当我尝试访问属于我的框架包的一部分的 nib 文件时 我的应用程序崩溃了 这是我用来访问视图控制器 XIB 文件的代码 testViewController c

随机推荐