我有一个Cocos2D iPhone应用程序,需要一组CGRect覆盖在图像上来检测它们中的触摸。下面的“数据”是一个保存从XML文件解析的值的类。 “delegateEntries”是一个NSMutableArray,包含多个“数据”对象,从另一个名为“条目”的NSMutableArray中提取,该条目驻留在应用程序委托中。全局NSMutableArray似乎没有价值
由于一些奇怪的原因,我可以在init函数中没有问题地获得这些值,但是进一步向下查询类,我尝试获取这些值,并且应用程序崩溃而没有错误消息(作为示例,我放入了通过“populateFieldsForTouchedItem”方法访问这些数据的“ccTouchBegan”方法)。
任何人都可以看到为什么这些值不能从其他方法访问?在dealloc之前没有对象被释放。提前致谢!
@synthesize clicked, delegate, data, image, blurImage, normalImage, arrayOfRects, delegateEntries;
- (id)initWithTexture:(CCTexture2D *)aTexture {
if((self=[super initWithTexture:aTexture])) {
arrayOfRects = [[NSMutableArray alloc] init];
delegateEntries = [[NSMutableArray alloc] init];
delegate = (InteractivePIAppDelegate *)[[UIApplication sharedApplication] delegate];
delegateEntries = [delegate entries];
data = [delegateEntries objectAtIndex:0];
NSLog(@"Assigning %@", [[delegateEntries objectAtIndex:0] backgroundImage]);
NSLog(@"%@ is the string", [[data sections] objectAtIndex:0]);
//CGRect rect;
NSLog(@"Count of array is %i", [delegateEntries count]);
//collect as many items as there are XML entries
for(int i=0; i<[delegateEntries count]; i++) {
if([[delegateEntries objectAtIndex:i] xPos]) {
NSLog(@"Found %i items", i+1);
[arrayOfRects addObject:[NSValue valueWithCGRect:CGRectMake([[[delegateEntries objectAtIndex:i] xPos] floatValue], [[[delegateEntries objectAtIndex:i] yPos] floatValue], [[[delegateEntries objectAtIndex:i] xBounds] floatValue], [[[delegateEntries objectAtIndex:i] yBounds] floatValue])]];
} else {
NSLog(@"Nothing");
}
}
blurImage = [[NSString alloc] initWithString:[data backgroundBlur]];
NSLog(@"5");
normalImage = [[NSString alloc] initWithString:[data backgroundImage]];
clicked = NO;
}
return self;
}
然后:
- (void)populateFieldsForTouchedItem:(TouchedRect)touchInfo
{
Data *touchDatum = [[Data alloc] init];
touchDatum = [[self delegateEntries] objectAtIndex:touchInfo.recordNumber];
NSLog(@"Assigning %@", [[[self delegateEntries] objectAtIndex:touchInfo.recordNumber] backgroundImage]);
rect = [[arrayOfRects objectAtIndex:touchInfo.recordNumber] CGRectValue];
image = [[NSString alloc] initWithString:[[touchDatum sections] objectAtIndex:0]];
[touchDatum release];
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
TouchedRect touchInfo = [self containsTouchLocation:touch];
NSLog(@"Information pertains to %i", touchInfo.recordNumber);
if (!touchInfo.touched && !clicked) { //needed since the touch location changes when zoomed
NSLog(@"NOPE");
return NO;
}
[self populateFieldsForTouchedItem:touchInfo];
NSLog(@"YEP");
return YES;
}
谢谢查克!这个和ccTouchBegan运行后保留“touchDatum”的结合解决了我的问题。我会进一步测试以确保,但看起来不错。感谢您的洞察! – diatrevolo 2010-05-19 18:10:04