检测 Cocos2d-iphone 上是否触摸了特定精灵
我正在按照 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]; }
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]; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来是一种非常困难的方法。我自己编码时间不长,但也许以下内容可能对您有所帮助。
假设你有一个名为敌人的 nsmutable 数组,并且在创建敌人时将新的敌人对象添加到该数组中。敌人对象将是一个 ccnode,并且其中有一个名为 _enemySprite 的 ccsprite
然后触摸
希望这有帮助
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
hope this helps
另一种方法是计算触摸位置和你的精灵之间的距离。如果触摸距离你的一个精灵足够近,你可以杀死它。像这样的事情..
Another way of doing it is that calculating distance between touch position and your sprites.. If touch is close enough to one of your sprites, you can kill it.. Something like this..