Objective c 如何在运行时获取sprite的标记值
我有20个精灵。所有20个精灵都添加到可变数组中。当我触摸20个精灵中的任何一个时,我想要触摸的精灵的标签值 帮我写代码。Objective c 如何在运行时获取sprite的标记值,objective-c,xcode,cocoa-touch,cocos2d-iphone,sprite,Objective C,Xcode,Cocoa Touch,Cocos2d Iphone,Sprite,我有20个精灵。所有20个精灵都添加到可变数组中。当我触摸20个精灵中的任何一个时,我想要触摸的精灵的标签值 帮我写代码。 提前谢谢。这应该很简单。由于您没有提供任何代码,我将假装它看起来像这样 for (CCSprite *aSprite in arrayOfSprites){ if ([self screenPosition:touchPosition intersectedWithSprite: aSprite]){ NSInteger tagForSprite =
提前谢谢。这应该很简单。由于您没有提供任何代码,我将假装它看起来像这样
for (CCSprite *aSprite in arrayOfSprites){
if ([self screenPosition:touchPosition intersectedWithSprite: aSprite]){
NSInteger tagForSprite = aSprite.tag; // do what you want with this value
}
}
TouchPositionIntersectedWithPrite是一种检查实际碰撞并返回布尔值的方法
-(id) init{
if( (self=[super init])){
self.isTouchEnabled = YES;
/* to do */
}}
- (void) registerWithTouchDispatcher{
/* to do */}
- (BOOL) containsTouchLocation:(UITouch *)touch
{
return YES;
}
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
//e.g. mArrSprites - your NSMutableArray with 20 sprites
for(CCSprite * sprite in mArrSprites){
if (CGRectContainsPoint(sprite.boundingBox, location)){ //check contains sprite rect your touch
int tag = sprite.tag; //GET tag of sprite
}
}
}
谢谢你的帮助,它对我很有用。在什么情况下它不起作用?方法是否在您期望的时候被调用?创建精灵时是否设置了标记属性,检查碰撞的方法是否正确(您必须自己实现)?获取正确的数据吗?获取IntersectedWithPrite“Method not found”(未找到方法)的异常,您必须自己创建该方法,具体取决于您希望如何处理检测<代码>-(BOOL)屏幕位置:(ccp)位置与prite相交:(CCSprite*)prite然后您必须使用例如CGRectContainsPoint(如@gaRik建议)来决定是否返回YES或NO…我在使用代码后运行时遇到异常。“未找到实例方法TouchDispatcher(返回类型默认为id)”。