항상 ARC를 사용하지만 cocos2d 템플릿은 ARC를 사용하지 않으므로 수동 참조 횟수를 사용해야합니다.
목표는 라벨 두 개가있는 메뉴를 만드는 것입니다. 레이블을 클릭하면 스프라이트가있는 이미지가 표시됩니다. 이미지를 클릭하면 메뉴로 돌아가서 다시 선택할 수 있습니다.메뉴와 스프라이트가있는 Cocos2D 응용 프로그램과 EXC_BAD_ACCESS가 충돌합니다.
-(id) init
{
if((self=[super init]))
{
CCMenuItemLabel* item1, *item2;
CCLabelTTF* label1= [CCLabelTTF labelWithString: @"Shark Icon" fontName: @"Arial" fontSize: 30], *label2;
label2= [CCLabelTTF labelWithString: @"Cocos2D Icon" fontName: @"Arial" fontSize: 30];
label1.color= ccRED;
label2.color= ccRED;
[label1 retain];
[label2 retain];
item1=[CCMenuItemLabel itemWithLabel: label1 block:^(id sender)
{
NSLog(@"Clicked shark icon");
[self removeChild: menu cleanup: NO];
shark=[CCSprite spriteWithFile: @"shark.jpeg"];
[shark setPosition: CGPointMake(150, 200)];
[self addChild: shark];
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate: self priority: 0 swallowsTouches: YES];
}];
item2= [CCMenuItemLabel itemWithLabel: label2 block:^(id sender)
{
NSLog(@"Clicked cocos2d icon");
[self removeChild: menu cleanup: NO];
icon=[CCSprite spriteWithFile: @"icon.png"];
[icon setPosition: CGPointMake(150, 200)];
[self addChild: icon];
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate: self priority: 0 swallowsTouches: YES];
}];
[item1 retain];
[item2 retain];
menu=[CCMenu menuWithItems: item1,item2, nil];
[menu alignItemsVertically];
[self addChild: menu];
}
return self;
}
- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate: self];
[self removeChild: shark cleanup: NO];
[self addChild: menu];
return YES;
}
는 무엇 일 :
이는 CCLayer 클래스입니다 나는 "상어 아이콘"(또는 "적인 Cocos2D 아이콘")을 클릭 한 후 상어의 이미지는 내가 클릭하면 내가 EXC_BAD_ACCESS를 얻을 나타납니다 :
EXC_BAD_ACCESS (code=1, address= 0x70Baafc8)
나는 모든 주소를 (메뉴, 항목 1 등) 인쇄를 시도하고, 항목 중 아무도 심지어 0x00000008 같은 잘못된 주소를 얻을이 address.Sometimes이 없습니다.
편집
난 그냥 유지 메뉴로 문제를 해결할 것입니다,하지만 난 왜 이해하지 : 메뉴는 이미 유지 :
: 나는 좀비를 사용하는 경우@property (nonatomic, retain) CCSprite* shark;
@property (nonatomic, retain) CCSprite* icon;
@property (nonatomic, retain) CCMenu* menu;
내가이 얻을
*** -[CCMenu tag]: message sent to deallocated instance 0x7c71a10
그래서 메뉴가 좀비이지만 보유 속성이 유지되지 않아야합니까?
이상한 점은 나는 상어와 아이콘, 그냥 메뉴를 유지할 필요가 없다는 것입니다.
ccTouchBegan에 브레이크 포인트를 설정하여 코드가 충돌하는 줄을 알 수 있습니다. – Chakalaka
아이가 충돌 할 때 메뉴를 추가 할 때 메뉴를 유지하여이 문제를 해결할 수 있습니다 ** 그러나 ** 이유는 이해하지 못했습니다. 속성 메뉴는 비 원자이고, 보유하고, 이미 유지되어야합니다. –
네,하지만이 속성을 사용하는 경우에만! 포인터 만 사용하십시오. 그래서 self.menu = – Chakalaka