我有一個叫做遊戲類,它看起來有點像這樣:釋放autorelease對象爲早?
@implementation GamePlay
-(id)init{
if((self = [super init])){
self.isAccelerometerEnabled = YES;
//ship defined in header
ship = [Ship shipWithParentNode:self];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
float imageHeight = [ship spriteContentSize].height;
[ship setSpritePosition:CGPointMake(screenSize.width*0.5, imageHeight*0.5)];
}
return self;
}
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
CGPoint pos = [ship spritePosition];
pos.x = acceleration.x * 10;
[ship setSpritePosition:pos];
}
@end
因此,船舶類看起來是這樣的:
@implementation Ship
+(id)shipWithParentNode:(CCNode *)parentNode{
return [[[Ship alloc] initWithParentNode:parentNode] autorelease];
}
-(id)initWithParentNode:(CCNode *)parentNode{
if((self = [super init])){
sprite = [CCSprite spriteWithFile:@"ship.png"];
SpriteLoader *spriteLoader = [SpriteLoader sharedSpriteLoader];
[spriteLoader addTextureAtlas:@"Spites_default.plist"];
CCAnimation *spriteAnimation = [CCAnimation animationWithFrames:[spriteLoader getSpriteFramesWithName:@"ship-" andAmount:[NSNumber numberWithInt:5]] delay:0.08f];
CCAnimate *anim = [CCAnimate actionWithAnimation:spriteAnimation];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:anim];
[sprite runAction:repeat];
[parentNode addChild:sprite z:0 tag:SHIP];
}
return self;
}
-(void)setSpritePosition:(CGPoint)point{
sprite.position = point;
}
-(CGPoint)spritePosition{
return sprite.position;
}
-(CGSize)spriteContentSize{
return sprite.contentSize;
}
@end
遊戲的初始化船舶罰款並繼續修改CCSprite內船舶,並在模擬一切都很好。當在設備上運行它時,我們會得到加速計輸入。在加速度計方法中,第一行因程序不再存在而使程序崩潰。如果我看在調試器與加速計方法我看到下面的第一行設置一個斷點: 自=(遊戲*)
- >船=(船舶*)0x004701e0
如果我再繼續運行該程序,我得到這個消息: 消息發送到釋放實例0x4701e0
但我不知道爲什麼它已被釋放...並大概0x004701e0 == 0x4701e0?
非常感謝, 本