ARC被踢我的ARC需要帮助王氏引用 - ARC + 2d的科科斯
我安装茯苓2D和不高兴看到,因为我想用ARC。我遇到了一个我似乎不太明白的问题。
仪器说我有5个引用我的GraphicsManager对象,但我只能得出其中4个。我需要创建并发布其中的许多。
类“RowManager”(CCLayer)调用类“GraphicsManager”(CCNode)来创建这些对象。 RowManager调用它的方法,通过选择移动的对象: [自时间表:@selector(行:)区间:0.01]
最后,当物体到达一个点,方法“row1Finished”叫谁再创造在调用对象到(死)移除它自己的孩子之前,再次引用该对象。
我可以让所有三个动画停止,让对象从RowManager中分离并消失,但dealloc方法仍然不会调用它自己。我究竟做错了什么?
要转换我的非圆弧cocos2d的模板,我也跟着指示操作:
http://www.learn-cocos2d.com/2012/04/enabling-arc-cocos2d-project-howto-stepbystep-tutorialguide/
/////////////////
@interface RowManager : CCLayer {
GraphicsManager *graphicsObject;
}
@implementation RowManager
-(id) init
{
if((self=[super init])) {
graphicsObject = [[GraphicsManager alloc] initWithBoatType:@"flatboat" withWord:@"life" usingCharacter:@"Puchai" orientation:@"up" visible:YES];
graphicsObject.tag = 21;
graphicsObject.position = ccp(offset,size.height/4.1);
[self addChild: graphicsObject];
[self schedule:@selector(row1:) interval:.01];
[self schedule:@selector(rotate:) interval:6];
// etc…
-(void)row1:(ccTime)dt{
// scroll the object across the screen
graphicsObject = (GraphicsManager *)[self getChildByTag:21]; // reference: for the row method
graphicsObject.position = ccp(graphicsObject.position.x-1, graphicsObject.position.y);
if (graphicsObject.position.x < 0 - graphicsObject.boatWidth){
[self row1Finished];
}
}
-(void)rotate:(ccTime)dt {
//waggle the object a bit
graphicsObject = (GraphicsManager *)[self getChildByTag:21]; // reference 2: for the rotate method
[graphicsObject runAction:[CCSequence actions:
[CCRotateBy actionWithDuration:1 angle:.8],
[CCRotateBy actionWithDuration:2 angle:-.8],
[CCRotateBy actionWithDuration:1 angle:-.8],
[CCRotateBy actionWithDuration:2 angle:.8],
nil]];
}
-(void) row1Finished {
graphicsObject = (GraphicsManager *)[self getChildByTag:21]; // reference 3: to reference the referenced
[self unschedule:@selector (row1:)];
[self unschedule:@selector(rotateBoat:)];
[graphicsObject die]; // call within object class to remove references
[self removeChild:graphicsObject cleanup:YES];
graphicsObject = nil;
}
/////////////////////
in Graphics Manager Class:
///////////////////
@interface GraphicsManager : CCNode {...}
@property CGFloat boatWidth;
…etc
@implementation GraphicsManager
@synthesize boatWidth;
- (void) die {
[self unschedule:@selector(puchaiRow:)]; // reference 4 animation
[self stopAllActions];
[self removeAllChildrenWithCleanup:YES]; // removes sub objects created in class
NSLog(@"Died");
}
- (void)dealloc
{
CCLOG(@"Graphics Manager dealloc: %@", self);
}
-(id)initWithBoatType:(NSString *)boatType orientation:(NSString *)orientation
{
self = [super init];
if (self){
if ([orientation isEqual: @"upstream"]){
flatBoat = [CCSprite spriteWithFile:@"flatBoat.png"];
flatBoat.anchorPoint = ccp(0,0);
self.boatWidth = [flatBoat boundingBox].size.width;
[self addChild:flatBoat];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"puchai.plist"];
puchai = [CCSprite spriteWithSpriteFrameName:@"puchai1.png"];
puchai.anchorPoint = ccp(0,0);
puchai.position = ccp(20,30);
puchai.tag = 4;
[self addChild:puchai];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"cube.plist"];
cube = [CCSprite spriteWithSpriteFrameName:@"cube1.png"];
cube.scale = .80;
cube.tag = 56;
cube.visible = NO;
cube.position = ccp(150,45);
[self addChild:cube];
cube = [CCSprite spriteWithSpriteFrameName:@"cube2.png"];
cube.scale = .80;
cube.position = ccp(-1 +150,[cube boundingBox].size.height +45);
cube.tag = 57;
cube.visible = NO;
[self addChild:cube];
[self schedule:@selector(puchaiRow:) interval:3]; // reference 4 animation
flatBoatWing = [CCSprite spriteWithFile:@"flatBoatWing.png"];
flatBoatWing.anchorPoint = ccp(0,0);
flatBoatWing.position = ccp(25,17);
[self addChild:flatBoatWing];
}
///etc
-(void)puchaiRow:(ccTime)dt{
puchai = (CCSprite *)[self getChildByTag:4];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"puchai.plist"];
NSMutableArray *frames = [[NSMutableArray alloc]init];
for (int i = 1; i<=8; i++) {
NSString *frameName = [NSString stringWithFormat:@"puchai%i.png",i ];
[frames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:frameName]];
}
CCAnimation *a = [CCAnimation animationWithSpriteFrames:frames delay:1.0f/5.0f];
[puchai runAction:[CCAnimate actionWithAnimation:a]];
////////// 这里是僵尸:历史输出。
# Address Category Event Type RefCt Timestamp Size Responsible Caller
0 0x13ea95e0 GraphicsManager Malloc 1 00:02.748.439 208 -[RowManager init]
1 0x13ea95e0 GraphicsManager Retain 2 00:02.748.447 0 -[GraphicsManager initWithBoatType:withWord:usingCharacter:orientation:visible:]
2 0x13ea95e0 GraphicsManager Release 1 00:02.748.449 0 -[GraphicsManager initWithBoatType:withWord:usingCharacter:orientation:visible:]
3 0x13ea95e0 GraphicsManager Retain 2 00:02.841.543 0 -[CCScheduler scheduleSelector:forTarget:interval:paused:repeat:delay:]
4 0x13ea95e0 GraphicsManager Retain 3 00:02.841.572 0 ccArrayAppendObjectWithResize
5 0x13ea95e0 GraphicsManager Retain 4 00:03.477.669 0 -[RowManager row1:] <--- AHA!!!!
6 0x13ea95e0 GraphicsManager Retain 5 00:03.482.438 0 -[RowManager row1Finished]
7 0x13ea95e0 GraphicsManager Release 4 00:03.482.439 0 -[RowManager row1Finished]
8 0x13ea95e0 GraphicsManager Release 3 00:03.482.480 0 -[CCScheduler removeHashElement:]
9 0x13ea95e0 GraphicsManager Release 2 00:03.483.435 0 ccArrayRemoveObjectAtIndex
10 0x13ea95e0 GraphicsManager Release 1 00:03.483.436 0 -[RowManager row1Finished]
您是否知道分配工具的“对象历史记录”功能?它会告诉你该对象的每个保留和释放的代码行,因此你可以分辨谁持守它。 – 2013-04-08 23:52:29
感谢您输入杰西,我在看僵尸版本...分配仪器暗示 负责的来电者:[RowManager init] 所以我猜RowManager init调用是被保留的对象。愚蠢的问题时间:如何在init运行并完成后取消分配对象?我没有做的只是在row1Finished分配graphicsObject到零? – 2013-04-09 04:58:55
是的,你的init方法看起来很好。 – 2013-04-09 12:01:05