SKScene过渡,内存释放

时间:2014-04-22 20:24:47

标签: ios objective-c memory-management sprite-kit skspritenode

好的,我已经阅读了iOS游戏书,我在很多网站上搜索了我的问题,虽然我发现很多人都有这个问题,但我还没有找到解决方案。

我正在构建一个游戏,我从SKScene过渡到另一个SKScene。即使我从一个简单的SKScene(如下例所示)转换为一个空的SKScene,内存也不会被解除分配。我听说我需要删除对我的SKScene的任何强引用,但我不相信我的代码如下:

#import "LaunchScene.h"
#import "EmptyScene.h"

@implementation LaunchScene
{
    float _scaleForDevice;
}

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {

        _scaleForDevice = 0.5;

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            _scaleForDevice = 0.208335;
        }

        SKSpriteNode *bg;

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && IS_WIDESCREEN) {
            bg = [SKSpriteNode spriteNodeWithImageNamed:@"launchBackground-568h"];
        } else {
            bg = [SKSpriteNode spriteNodeWithImageNamed:@"launchBackground"];
        }

        bg.position = CGPointMake(CGRectGetMidX(self.frame),
                                  CGRectGetMidY(self.frame));
        bg.zPosition = 10;
        bg.name = @"launchBackground";
        [self addChild:bg];

        for (int i = 0; i < 5; i++) {
            SKSpriteNode *launch = [SKSpriteNode spriteNodeWithImageNamed:[NSString stringWithFormat:@"launch%d",i]];
            launch.position = CGPointMake(self.size.width, 0);
            launch.anchorPoint = CGPointMake(0.5, 0.5);
            launch.zPosition = 1000+i;
            launch.name = [NSString stringWithFormat:@"launch%d",i];
            [launch setScale:_scaleForDevice];
            [self addChild:launch];

            if (i == 0 || i == 2) {
                SKAction* rotate = [SKAction rotateByAngle:-RadiansToDegrees(360) duration:10000*(i+1)];
                [launch runAction:rotate];
            } else if (i == 1) {
                SKAction* rotate = [SKAction rotateByAngle:RadiansToDegrees(360) duration:10000*(i+1)];
                [launch runAction:rotate];
            }
        }

        SKSpriteNode *mainMenuBackground;

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && IS_WIDESCREEN) {
            mainMenuBackground = [SKSpriteNode spriteNodeWithImageNamed:@"mainMenuBackground-568h"];
        } else {
            mainMenuBackground = [SKSpriteNode spriteNodeWithImageNamed:@"mainMenuBackground"];
        }

        mainMenuBackground.position = CGPointMake(CGRectGetMidX(self.frame),
                                  CGRectGetMidY(self.frame));
        mainMenuBackground.zPosition = 5;
        mainMenuBackground.name = @"mainMenuBackground";
        [self addChild:mainMenuBackground];

        [SKActionEffects fadeOutAndRemove:bg duration:2];

        SKSpriteNode *mainMenuGround;

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && IS_WIDESCREEN) {
            mainMenuGround = [SKSpriteNode spriteNodeWithImageNamed:@"mainMenuGround-568h"];
        } else {
            mainMenuGround = [SKSpriteNode spriteNodeWithImageNamed:@"mainMenuGround"];
        }

        mainMenuGround.position = CGPointMake(self.size.width, 0);
        mainMenuGround.zPosition = 500;
        mainMenuGround.anchorPoint = CGPointMake(1, 1);
        mainMenuGround.name = @"mainMenuGround";
        [self addChild:mainMenuGround];

        SKAction *waitMainMenuGround = [SKAction waitForDuration:1];

        SKAction *moveMainMenuGround = [SKAction moveToY:(self.size.height)/3 duration:0.3];
        moveMainMenuGround.timingMode = SKActionTimingEaseInEaseOut;

        SKAction *shakeMainMenuGround = [SKAction runBlock:^{
            [SKActionEffects shakeSprite:mainMenuGround toDirection:1];
        }];

        SKAction *shrinkLaunch = [SKAction runBlock:^{
            for (int i = 0; i < 5; i++) {
                SKSpriteNode *launch = (SKSpriteNode*)[self childNodeWithName:[NSString stringWithFormat:@"launch%d",i]];
                [SKActionEffects disappearAndRemove:launch];
            }
        }];

        SKAction *presentMainMenu = [SKAction runBlock:^{
            //Create and configure the scene.
            EmptyScene * scene = [[EmptyScene alloc] initWithSize:self.size];
            scene.scaleMode = SKSceneScaleModeAspectFill;

            // Present the scene.
            [self.view presentScene:scene];
        }];

        SKAction *group = [SKAction sequence:@[waitMainMenuGround, moveMainMenuGround, shakeMainMenuGround, shrinkLaunch, waitMainMenuGround, presentMainMenu]];

        [mainMenuGround runAction:group];
    }
    return self;
}

@end

我创建了一个就是那样的EmptyScene。它的init中没有任何内容。

#import "EmptyScene.h"

@implementation EmptyScene
-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {

    }
    return self;
}

@end

当我从一开始只运行EmptyScene时,我的记忆大约是25mb。当我首先运行LaunchScene时,内存会变为95mb,当它转换到EmptyScene时,人们会期望内存下降到25mb,它仍然是95mb。知道为什么以及我能做些什么来解决这个问题?

0 个答案:

没有答案