Detecting if a specific sprite was touched on Cocos2d-iphone(检测是否在 Cocos2d-iphone 上触摸了特定的精灵)
问题描述
我正在按照 Ray 的教程制作一个简单的 iPhone 游戏(此处:http://goo.gl/fwPi) ,并决定我希望在敌人被触摸时将其消灭.
我最初的方法是在触摸位置生成一个小的 CCSprite 精灵,然后使用 CGRectMake 创建所述精灵的边界框,以检测是否触摸了敌人的精灵.就像雷对射弹/敌人所做的那样.但是,当然,我的做法是行不通的,我不能把自己从这个洞里挖出来.
这是相关的代码片段.任何帮助表示赞赏:
<上一页>- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {//选择要使用的触摸之一UITouch *touch = [触摸任何对象];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)];toucharea.tag = 2;[self addChild:touchedarea];[_touchedareas addObject:touchedarea];}-(无效)更新:(ccTime)dt {NSMutableArray *touchedareasToDelete = [[NSMutableArray alloc] init];for (CCSprite *touchedarea in _touchedareas) {CGRect touchareaRect = CGRectMake(toucharea.position.x,toucharea.position.y,toucharea.contentSize.width,toucharea.contentSize.height);NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];for (CCSprite *target in _targets) {CGRect 目标矩形 = CGRectMake(target.position.x - (target.contentSize.width/2),target.position.y - (target.contentSize.height/2),目标.contentSize.width,目标.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 发布];}for (CCSprite *touchedarea in touchareasToDelete) {[_touchedareas removeObject:touchedarea];[self removeChild:touchedarea cleanup:YES];}[touchedareasToDelete 释放];}这看起来是一个非常困难的方法.我自己编码的时间不长,但也许以下内容可能会对您有所帮助.假设您有一个名为敌人的 nsmutablearray,并且您在创建新的敌人对象时将其添加到该数组中.敌人对象将是一个 ccnode 并在其中有一个名为 _enemySprite 的 ccsprite然后触摸
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{NSSet *allTouches = [事件 allTouches];UITouch * touch = [[allTouches allObjects] objectAtIndex:0];//UITouch* touch = [触摸任何对象];CGPoint location = [touch locationInView: [touch view]];location = [[CCDirector sharedDirector] convertToGL:location];int arraysize = [敌人数量];for (int i = 0; i < 数组大小; i++) {if (CGRectContainsPoint([[[enemies objectAtIndex:i] _enemySprite] boundingBox], location)) {//一些代码在这里摧毁你的敌人}}//NSLog(@"TOUCH DOWN");}
希望对你有帮助
I was following Ray`s tutorial for making a simple iPhone game (here: http://goo.gl/fwPi) , and decided that i wanted the enemies to be eliminated when they get touched.
My initial approach was to spawn a small CCSprite sprite on the touch location, then use CGRectMake to create a bounding box of said sprite to detect if the enemy sprite was touched. Much like Ray does with the projectile/enemy. But of course, my way of doing it isnt working and i cant dig myself out of this hole.
Here is the relevant code snippet. Any help is appreciated:
- (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]; }
That looks like a very difficult way to go about doing it. I havent been coding long myself but maybe the following might help you. lets say u have a nsmutablearray called enemies and you add the new enemy object to this array when ever you create one. enemy object would be a ccnode and have a ccsprite within it called _enemySprite then do the touch
-(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");
}
hope this helps
这篇关于检测是否在 Cocos2d-iphone 上触摸了特定的精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检测是否在 Cocos2d-iphone 上触摸了特定的精灵
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01