SKScene 无法释放内存,导致内存增长有限

2023-11-30

我已经为此苦苦挣扎了好几天,由于某种原因,我的 SKScenes 没有正确释放,这导致内存增长有限,因为每次我退出并进入场景时,内存都会跳跃。这意味着游戏进行 10 轮后应用程序崩溃。据我所知,经过多次检查后,我没有任何保留周期或对场景本身的强引用,虽然我知道纹理被缓存并保存在内存中,但一旦预加载,内存就不应该每次都增加。

这就是我在视图控制器中设置 skview 和第一个场景的方式:

-(void)loadStartScreen{

    SKView *theView = (SKView *) self.view;
    theView.showsFPS = YES;
    theView.showsNodeCount = YES;
    //Sprite Kit applies additional optimizations to improve rendering performance
    theView.ignoresSiblingOrder = YES;

    // Create and configure the scene.
    MainMenuScene *theScene = [MainMenuScene sceneWithSize:theView.bounds.size];
    theScene.scaleMode = SKSceneScaleModeAspectFill;
    theScene.backgroundColor = [UIColor grayColor];

    // Present the scene
    [theView presentScene:theScene];

这是我的 MainMenuScene 中用于创建并移动到下一个场景的代码:

 SKScene *theScene;
    SKTransition *theTransition;
    switch (theTag.intValue) {
        case 0: // start game
            // stop music
            [[appDelegate musicPlayer]stop];
            theScene  = [[GameLevelScene alloc] initWithSize:self.view.bounds.size];
            // transition type
            theTransition = [SKTransition fadeWithDuration:1.0];
            break;
        case 1: // settings
            theScene  = [[SettingsScene alloc] initWithSize:self.view.bounds.size];
            // transition type
            theTransition = [SKTransition flipHorizontalWithDuration:1.0];
            break;
        case 2: // iap
            theScene  = [[IAPScene alloc] initWithSize:self.view.bounds.size];
            // transition type
            theTransition = [SKTransition flipHorizontalWithDuration:1.0];
            break;
        case 3: // unlocks screen
            NSLog(@"scene is %@",self.view.scene);
             theScene  = [[deletmet alloc] initWithSize:self.view.bounds.size];
            // transition type
            theTransition = [SKTransition flipHorizontalWithDuration:1.0];
            NSLog(@"scene is after %@",self.view.scene);
            break;
        case 4: // level complete
            theScene  = [[LevelCompleteScene alloc] initWithSize:self.view.bounds.size];
            // transition type
            theTransition = [SKTransition flipHorizontalWithDuration:1.0];
            break;
        case 5: // cheats
            theScene  = [[CheatsScene alloc] initWithSize:self.view.bounds.size];
            // transition type
            theTransition = [SKTransition flipHorizontalWithDuration:1.0];
            break;

        default:
            break;
    }

现在我知道你应该使用“[self.view PresentScene:]”,因为 SkView 保存场景,但因为没有任何效果,我一直在尝试了解为什么场景似乎没有取消分配。当我尝试按下案例 2 的按钮时,我得到了生成的 NSlog:

[self presentScene:theScene];

NSLOG 结果 =“IAP SCENE DEALLOC”,但用户不会进入下一个场景。

[self presentScene:nil];

NSLOG 结果 =“IAP SCENE DEALLOC”,但用户不会进入下一个场景。

[self.view presentScene:theScene];

即使当前 MainMenuScene 有一个用于 dealloc 的 NSLOG,也没有 nslog 结果

[self.view presentScene:nil];

NSLOG 结果 = IAP SCENE DEALLOC 并且屏幕变灰

现在这一切都非常奇怪,因为当我确实收到 DEALLOC NSLOG 消息时,它应该为我提供当前场景(又名 MainMenuScene)的释放的 NSLOG,而不是它应该加载的场景。

难道我做错了什么?我是否错误地使用了 SKView,任何帮助将不胜感激,因为我已经阅读了各种帖子,所有这些都没有帮助。

  • 更新:这是我在之间来回移动的每个场景的代码,请注意缺少操作或对自身的任何强引用:

主菜单:

#import "MainMenuScene.h"
#import "SKScene+SceneUtils.h"
#import "GameLevelScene.h"
#import "SettingsScene.h"
#import "CreditsScene.h"
#import "IAPScene.h"
#import "UnlocksScene.h"
#import "LevelCompleteScene.h"
#import "CheatsScene.h"
#import "AppDelegate.h"
#import "UserDetails.h"
#import "ItemBannerLabel.h"
#import "ItemWindow.h"
#import "TextureList.h"
#import "TextureLoader.h"

@interface MainMenuScene(){

    SKSpriteNode *backgroundImage;
    SKSpriteNode *topBar;
    SKSpriteNode *bottomBar;
    SKSpriteNode *ladybirds;
    SKLabelNode *title;
    SKLabelNode *subtitle;
    SKLabelNode *coffee;
    ItemWindow *settingsWin;
    ItemWindow *iapWin;
    ItemWindow *unlocksWin;
    AGSpriteButton *startButton;
    AGSpriteButton *continueButton;
    AGSpriteButton *settingsButton;
    AGSpriteButton *iapButton;
    AGSpriteButton *unlocksButton;
    SKEmitterNode *theParticles;
    //SKAction *delay;
    //AppDelegate *appDelegate;
}

@end

@implementation MainMenuScene


#pragma mark - Scene Appears
-(void)didMoveToView:(SKView *)view {

    // setup UI
    [self createUI];
    // setup view
    [self setupView];
}
-(void)willMoveFromView:(SKView *)view{

}

#pragma mark - CreateUI
-(void)createUI{

    // scene size
    self.scene.size = [[TextureList sharedManager]returnTextureSize:@"kMMBg"];
    // background
    self.scene.backgroundColor = [SKColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0];

    // masked background
    backgroundImage = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:kMMBg] size:[[TextureList sharedManager]returnTextureSize:@"kMMBg"]];
    backgroundImage.position = screenCenter;
    backgroundImage.zPosition = self.zPosition+1;
    [self addChild:backgroundImage];

    ladybirds = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:kMMLadybirds] size:[[TextureList sharedManager]returnTextureSize:kMMLadybirds]];
    ladybirds.position = CGPointMake(CGRectGetMaxX(self.frame)-ladybirds.frame.size.width/2, screenCenter.y);
    ladybirds.zPosition = bottomBar.zPosition+5;
    [self addChild:ladybirds];

    // buttons
    startButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMMStartBtn] color:[UIColor clearColor] size:[[TextureList sharedManager]returnTextureSize:kMMStartBtn]];
    [startButton setLabelWithText:@"" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:nil];
    startButton.position = CGPointMake(ladybirds.position.x-ladybirds.frame.size.width/4, ladybirds.position.y-ladybirds.frame.size.height/16);
    startButton.zPosition = ladybirds.zPosition+1;
    [startButton addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:0] forControlEvent:AGButtonControlEventTouchUpInside];
    [self addChild:startButton];

    // emitter
    theParticles = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"FlowerPetalParticle" ofType:@"sks"]];
    theParticles.zPosition = backgroundImage.zPosition+1;
    theParticles.position = CGPointMake((CGRectGetMinX(self.scene.frame)),startButton.position.y);
    theParticles.particlePositionRange = CGVectorMake(0.0, CGRectGetMaxY(self.frame)-topBar.frame.size.height-bottomBar.frame.size.height);
    if ([[[UserDetails sharedManager]userDevice]isEqualToString:@"ipad"]) {
        theParticles.particleLifetime = 8.0;
        theParticles.particleScale = 0.5;
        theParticles.particleScaleRange = 0.2;
    }
    [self addChild:theParticles];

    topBar = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:[[TextureList sharedManager]returnTextureSize:kMMTopBar]];
    topBar.position = CGPointMake(screenCenter.x, CGRectGetMaxY(self.frame)-topBar.frame.size.height/2);
    topBar.zPosition = theParticles.zPosition+1;
    [self addChild:topBar];

    bottomBar = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:[[TextureList sharedManager]returnTextureSize:kMMBottomBar]];
    bottomBar.position = CGPointMake(screenCenter.x, CGRectGetMinY(self.frame)+bottomBar.frame.size.height/2);
    bottomBar.zPosition = theParticles.zPosition+1;
    [self addChild:bottomBar];

    settingsWin = [[ItemWindow alloc]initWithImageNamed:kMMCreditsBtn withLabel:@"SETTINGS" setLabelTop:NO];
    settingsWin.theLabel.fontColor = [UIColor blackColor];
    settingsWin.theLabel.fontSize = 15;
    settingsWin.position = CGPointMake(CGRectGetMinX(self.frame)+settingsWin.frame.size.width/2+20, CGRectGetMinY(self.frame)+settingsWin.frame.size.height/2+25);
    settingsWin.zPosition = bottomBar.zPosition+1;
    [self addChild:settingsWin];

    settingsButton = [[AGSpriteButton alloc]initWithTexture:nil color:nil size:CGSizeMake(settingsWin.size.width*2, settingsWin.size.height*2)];
    [settingsButton setLabelWithText:@"" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:nil];
    settingsButton.position = settingsWin.position;
    settingsButton.zPosition = settingsWin.zPosition+1;
    [settingsButton addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:1] forControlEvent:AGButtonControlEventTouchUpInside];
    [self addChild:settingsButton];

    iapWin = [[ItemWindow alloc]initWithImageNamed:kMMIapBtn withLabel:@"SHOP" setLabelTop:NO];
    iapWin.theLabel.fontColor = [UIColor blackColor];
    iapWin.theLabel.fontSize = 15;
    iapWin.position = CGPointMake(settingsWin.position.x+iapWin.frame.size.width+30, settingsWin.position.y);
    iapWin.zPosition = bottomBar.zPosition+1;
    [self addChild:iapWin];

    iapButton = [[AGSpriteButton alloc]initWithTexture:nil color:nil size:CGSizeMake(iapWin.size.width*2, iapWin.size.height*2)];
    [iapButton setLabelWithText:@"" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:nil];
    iapButton.position = iapWin.position;
    iapButton.zPosition = iapWin.zPosition+1;
    [iapButton addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:2] forControlEvent:AGButtonControlEventTouchUpInside];
    [self addChild:iapButton];

    unlocksWin = [[ItemWindow alloc]initWithImageNamed:kMMUnlockBtn withLabel:@"UNLOCKS" setLabelTop:NO];
    unlocksWin.theLabel.fontColor = [UIColor blackColor];
    unlocksWin.theLabel.fontSize = 15;
    unlocksWin.position = CGPointMake(iapWin.position.x+unlocksWin.frame.size.width+30, iapWin.position.y);
    unlocksWin.zPosition = bottomBar.zPosition+1;
    [self addChild:unlocksWin];

    unlocksButton = [[AGSpriteButton alloc]initWithTexture:nil color:nil size:CGSizeMake(unlocksWin.size.width*2, unlocksWin.size.height*2)];
    [unlocksButton setLabelWithText:@"" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:nil];
    unlocksButton.position = unlocksWin.position;
    unlocksButton.zPosition = unlocksWin.zPosition+1;
    [unlocksButton addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:3] forControlEvent:AGButtonControlEventTouchUpInside];
    [self addChild:unlocksButton];

    // Labels
    title = [[SKLabelNode alloc]initWithFontNamed:kFontName];
    if ([[[UserDetails sharedManager]userDevice]isEqualToString:@"iphone4"]) {
        title.fontSize = 60;
    }
    else{
    title.fontSize = 75;
    }
    title.fontColor = [UIColor blackColor];
    title.position = CGPointMake(topBar.frame.size.width/4, topBar.position.y-10);
    title.zPosition = topBar.zPosition+1;
    title.text = @"FLOWERS";
    [self addChild:title];

    subtitle = [[SKLabelNode alloc]initWithFontNamed:kFontName];

    if ([[[UserDetails sharedManager]userDevice]isEqualToString:@"iphone4"]) {
        subtitle.fontSize = 24;
    }
    else{
        subtitle.fontSize = 30;
    }
    subtitle.fontColor = [UIColor grayColor];
    subtitle.position = CGPointMake(title.position.x, title.position.y-title.frame.size.height/2-10);
    subtitle.zPosition = topBar.zPosition+1;
    subtitle.text = @"THE BEAUTIFUL MEADOW";
    [self addChild:subtitle];

    AGSpriteButton *testButton3 = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMMIapBtn] color:[UIColor clearColor] size:[[TextureList sharedManager]returnTextureSize:kMMIapBtn]];
    [testButton3 setLabelWithText:@"" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:nil];
    testButton3.position = CGPointMake(bottomRight.x-testButton3.frame.size.width/1.5, bottomRight.y+settingsButton.frame.size.height/1.5);
    testButton3.zPosition = interfaceLayer;
    [testButton3 addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:4] forControlEvent:AGButtonControlEventTouchUpInside];
    [self addChild:testButton3];

    AGSpriteButton *testButton4 = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMMIapBtn] color:[UIColor clearColor] size:[[TextureList sharedManager]returnTextureSize:kMMIapBtn]];
    [testButton4 setLabelWithText:@"" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:nil];
    testButton4.position = CGPointMake(testButton3.position.x-testButton4.frame.size.width, bottomCenter.y+settingsButton.size.height/1.5);
    testButton4.zPosition = interfaceLayer;
    [testButton4 addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:5] forControlEvent:AGButtonControlEventTouchUpInside];
    [self addChild:testButton4];

    if ([[[UserDetails sharedManager]userDevice] isEqualToString:@"ipad"]) {

        title.fontSize = 105;
        title.position = CGPointMake(topBar.frame.size.width/6+20, topBar.position.y+10);
        subtitle.position = CGPointMake(title.position.x, title.position.y-title.frame.size.height/2-10);
        subtitle.fontSize = 41;

        settingsWin.theLabel.fontSize = 25;
        iapWin.theLabel.fontSize = 25;
        unlocksWin.theLabel.fontSize = 25;

        settingsWin.position = CGPointMake(CGRectGetMinX(self.frame)+settingsWin.frame.size.width/2+20, CGRectGetMinY(self.frame)+settingsWin.frame.size.height+25);
        iapWin.position = CGPointMake(settingsWin.position.x+iapWin.frame.size.width+30, settingsWin.position.y);
        unlocksWin.position = CGPointMake(iapWin.position.x+unlocksWin.frame.size.width+30, settingsWin.position.y);
    }
}

#pragma mark - Setup View
-(void)setupView{
    // setup music players slightly quieter music now
    /*appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [self fadeVolumeIn:[appDelegate musicPlayer] toVolume:0.45];
    [[appDelegate soundFxPlayer]setVolume:0.25];
    [[appDelegate soundFxPlayer]play];

    // no continue if the user has not progressed past level 1
    if ([[UserDetails sharedManager]userCurrentLevel] <= 1) {
        continueButton.userInteractionEnabled = NO;
        continueButton.hidden = YES;
    }
    else{
        continueButton.userInteractionEnabled = YES;
        continueButton.hidden = NO;
    }*/
}

#pragma mark - Interaction
-(void)buttonPressed:(NSNumber*)theTag{

    SKScene *theScene;
    SKTransition *theTransition;
    switch (theTag.intValue) {
        case 0: // start game
            // stop music
            //[[appDelegate musicPlayer]stop];
            theScene  = [[GameLevelScene alloc] initWithSize:self.view.bounds.size];
            // transition type
            theTransition = [SKTransition fadeWithDuration:1.0];
            break;
        case 1: // settings
            theScene  = [[SettingsScene alloc] initWithSize:self.view.bounds.size];
            // transition type
            theTransition = [SKTransition flipHorizontalWithDuration:1.0];
            break;
        case 2: // iap
            theScene  = [[IAPScene alloc] initWithSize:self.view.bounds.size];
            // transition type
            theTransition = [SKTransition flipHorizontalWithDuration:1.0];
            break;
        case 3: // unlocks screen
            theScene  = [[UnlocksScene alloc] initWithSize:self.view.bounds.size];
            // transition type
            theTransition = [SKTransition flipHorizontalWithDuration:1.0];
            break;
        case 4: // level complete
            theScene  = [[LevelCompleteScene alloc] initWithSize:self.view.bounds.size];
            // transition type
            theTransition = [SKTransition flipHorizontalWithDuration:1.0];
            break;
        case 5: // cheats
            theScene  = [[CheatsScene alloc] initWithSize:self.view.bounds.size];
            // transition type
            theTransition = [SKTransition flipHorizontalWithDuration:1.0];
            break;

        default:
            break;
    }

    // play sound
    [self menuButtonPressed];
    [self.view presentScene:theScene transition:theTransition];

}

@end

设置屏幕:

#import "SettingsScene.h"
#import "SKScene+SceneUtils.h"
#import "AGSpriteButton.h"
#import "ItemBannerLabel.h"
#import "TextureList.h"
#import "UnlockController.h"
#import "UserDetails.h"
#import "TutorialFlowerTarget.h"
#import "CreditsScene.h"

@interface SettingsScene(){

    AGSpriteButton *backButton;
    AGSpriteButton *resetButton;
    AGSpriteButton *resetTutorialsButton;
    AGSpriteButton *creditsButton;
    ItemBannerLabel *titleLabel;
    SKLabelNode *copyrightLabel;
    UIAlertView *resetGameAlert;
    UIAlertView *resetTutAlert;
    SKScene *theScene;
    SKTransition *theTransition;
    SKSpriteNode *menuBg;
}

@end

@implementation SettingsScene

#pragma mark - SCENE APPEARS
-(void)didMoveToView:(SKView *)view {

    // setup UI
    [self createUI];
}

#pragma mark - CREATE UI
-(void)createUI{

    // background
    self.scene.backgroundColor = [UIColor whiteColor];

    menuBg = [[SKSpriteNode alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDBg] color:nil size:[[TextureList sharedManager]returnTextureSize:@"kMDBg"]];
    menuBg.position = screenCenter;
    menuBg.zPosition = self.zPosition+1;
    [self addChild:menuBg];

    // labels
    titleLabel = [[ItemBannerLabel alloc]initWithBgImageNamed:kMDTitle withLabel:@"GAME SETTINGS" withfont:kFontName withSize:kFontSizeMDTitle];
    titleLabel.position = CGPointMake(topLeft.x+titleLabel.frame.size.width/2+10,topLeft.y-titleLabel.frame.size.height/2-10);
    titleLabel.zPosition = interfaceLayer;
    [self addChild:titleLabel];

    copyrightLabel = [SKLabelNode labelNodeWithFontNamed:kFontName];
    copyrightLabel.text = [NSString stringWithFormat:@"COPYRIGHT © 2015 RICHARD ACHERKI"];
    copyrightLabel.fontSize = kFontSizeMDSmall;
    copyrightLabel.fontColor = [UIColor blackColor];
    copyrightLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
    copyrightLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
    copyrightLabel.position = CGPointMake(bottomCenter.x, bottomCenter.y+copyrightLabel.frame.size.height);
    copyrightLabel.zPosition = interfaceLayer;
    [self addChild:copyrightLabel];

    // buttons
    if ([[[UserDetails sharedManager]userDevice]isEqualToString:@"ipad"]) {
        resetTutorialsButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:CGSizeMake([[TextureList sharedManager]returnTextureSize:kMDButton].width*4, [[TextureList sharedManager]returnTextureSize:kMDButton].height*2)];
        [resetTutorialsButton setLabelWithText:@"RESET TUTORIALS" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
        resetTutorialsButton.position = CGPointMake(screenCenter.x, titleLabel.position.y-titleLabel.frame.size.height/2-resetTutorialsButton.frame.size.height/2-30);
        resetTutorialsButton.zPosition = menuBg.zPosition+1;
        [resetTutorialsButton addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:0] forControlEvent:AGButtonControlEventTouchUpInside];
    }
    else{
        resetTutorialsButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:CGSizeMake([[TextureList sharedManager]returnTextureSize:kMDButton].width*2, [[TextureList sharedManager]returnTextureSize:kMDButton].height)];
        [resetTutorialsButton setLabelWithText:@"RESET TUTORIALS" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
        resetTutorialsButton.position = CGPointMake(screenCenter.x, titleLabel.position.y-titleLabel.frame.size.height/2-resetTutorialsButton.frame.size.height/2-30);
        resetTutorialsButton.zPosition = menuBg.zPosition+1;
        [resetTutorialsButton addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:0] forControlEvent:AGButtonControlEventTouchUpInside];
    }
    [self addChild:resetTutorialsButton];

    if ([[[UserDetails sharedManager]userDevice]isEqualToString:@"ipad"]) {
        resetButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:CGSizeMake([[TextureList sharedManager]returnTextureSize:kMDButton].width*4, [[TextureList sharedManager]returnTextureSize:kMDButton].height*2)];
        [resetButton setLabelWithText:@"RESET GAME" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
        resetButton.position = CGPointMake(resetTutorialsButton.position.x,resetTutorialsButton.position.y-resetTutorialsButton.frame.size.height-20);
        resetButton.zPosition = menuBg.zPosition+1;
        [resetButton addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:1] forControlEvent:AGButtonControlEventTouchUpInside];
    }
    else{
        resetButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:CGSizeMake([[TextureList sharedManager]returnTextureSize:kMDButton].width*2, [[TextureList sharedManager]returnTextureSize:kMDButton].height)];
        [resetButton setLabelWithText:@"RESET GAME" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
        resetButton.position = CGPointMake(resetTutorialsButton.position.x,resetTutorialsButton.position.y-resetTutorialsButton.frame.size.height-20);
        resetButton.zPosition = menuBg.zPosition+1;
        [resetButton addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:1] forControlEvent:AGButtonControlEventTouchUpInside];
    }
    [self addChild:resetButton];

    if ([[[UserDetails sharedManager]userDevice]isEqualToString:@"ipad"]) {
        creditsButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:CGSizeMake([[TextureList sharedManager]returnTextureSize:kMDButton].width*4, [[TextureList sharedManager]returnTextureSize:kMDButton].height*2)];
        [creditsButton setLabelWithText:@"CREDITS" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
        creditsButton.position = CGPointMake(resetButton.position.x,resetButton.position.y-resetButton.frame.size.height-20);
        creditsButton.zPosition = menuBg.zPosition+1;
        [creditsButton addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:2] forControlEvent:AGButtonControlEventTouchUpInside];
    }
    else{
        creditsButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:CGSizeMake([[TextureList sharedManager]returnTextureSize:kMDButton].width*2, [[TextureList sharedManager]returnTextureSize:kMDButton].height)];
        [creditsButton setLabelWithText:@"CREDITS" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
        creditsButton.position = CGPointMake(resetButton.position.x,resetButton.position.y-resetButton.frame.size.height-20);
        creditsButton.zPosition = menuBg.zPosition+1;
        [creditsButton addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:2] forControlEvent:AGButtonControlEventTouchUpInside];
    }
    [self addChild:creditsButton];    

    backButton = [[AGSpriteButton alloc]initWithTexture:[SKTexture textureWithImageNamed:kMDButton] color:nil size:[[TextureList sharedManager]returnTextureSize:kMDButton]];
    [backButton setLabelWithText:@"BACK" andFont:[UIFont fontWithName:kFontName size:kFontSizeButton] withColor:[UIColor whiteColor]];
    backButton.position = CGPointMake(bottomRight.x-backButton.frame.size.width/2-10, bottomRight.y+backButton.frame.size.height/2+10);
    backButton.zPosition = menuBg.zPosition+1;
    [backButton addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:3] forControlEvent:AGButtonControlEventTouchUpInside];
    [self addChild:backButton];

}

#pragma mark - BUTTON PRESSED
-(void)buttonPressed:(NSNumber*)theTag{

    if (theTag.intValue == 0) { // reset tutorials
        resetTutAlert = [[UIAlertView alloc]initWithTitle:@"RESET TUTORIALS" message:@"Are you sure you want to reset tutorials?\nThis will cause all tutorials to show again when playing the game." delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
        [resetTutAlert show];
    }
    else if (theTag.intValue == 1) { // reset game
        resetGameAlert = [[UIAlertView alloc]initWithTitle:@"RESET THE GAME" message:@"Are you sure you want to wipe your game progression?\nThis will remove all unlocks, scores and level progression." delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
        [resetGameAlert show];
    }
    else if (theTag.intValue == 2) { // credits menu
        // play sound
        [self menuButtonPressed];
        theScene = [[CreditsScene alloc] initWithSize:self.view.bounds.size];
        theTransition = [SKTransition flipHorizontalWithDuration:1.0];
        [self.view presentScene:theScene transition:theTransition];
    }
    else if (theTag.intValue == 3) { // main menu
        // play sound
        [self menuButtonPressed];
        // scene to move to
        theScene  = [[MainMenuScene alloc] initWithSize:self.view.bounds.size];
        // transition type
        theTransition = [SKTransition flipHorizontalWithDuration:1.0];
        [self.view presentScene:theScene transition:theTransition];
    }

}

#pragma mark - RESET TUTORIALS
-(void)resetTutorials{

    for (id theObject in [[UserDetails sharedManager]userTutorials]) {
        TutorialFlowerTarget *theTut= [[[UserDetails sharedManager]userTutorials]objectForKey:theObject];
        [theTut setTriggered:NO];
    }
    [[UserDetails sharedManager]saveData];
}

#pragma mark - RESET UNLOCKS
-(void)resetUnlocks{

    [[UnlockController sharedManager]resetGame];
}

我可能是错的,但我怀疑你的按钮类是你的冒犯者。你打电话时...

[testButton4 addTarget:self selector:@selector(buttonPressed:) withObject:[NSNumber numberWithInt:5] forControlEvent:AGButtonControlEventTouchUpInside];

你过关了self就是那个场景。在按钮类中的方法..

-(void)addTarget:(id)target selector:(SEL)selector withObject:(id)object forControlEvent:(AGButtonControlEvent)controlEvent
{
    //check whether selector is already saved, otherwise it will get called twice

    if (marrSelectors == nil)
    {
        marrSelectors = [NSMutableArray new];
    }

    NSMutableDictionary *mdicSelector = [[NSMutableDictionary alloc]init];

    [mdicSelector setObject:target forKey:@"target"];
    [mdicSelector setObject:[NSValue valueWithPointer:selector] forKey:@"selector"];

    if (object)
    {
        [mdicSelector setObject:object forKey:@"object"];
    }

    [mdicSelector setObject:[NSNumber numberWithInt:controlEvent] forKey:@"controlEvent"];

    [marrSelectors addObject:mdicSelector];
}

注意这个..

[mdicSelector setObject:target forKey:@"target"];

目标是您的场景,它被扔进字典中,然后被扔进数组中。因此,从理论上讲,该按钮现在对您的场景有很强的引用,并且您的场景也对该按钮有引用。

为了测试这个理论,在调用之前将所有按钮设置为零

[self.view presentScene:theScene transition:theTransition];

如果我是对的,那应该会打破彼此之间的强引用。希望这有帮助,这就是问题所在。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SKScene 无法释放内存,导致内存增长有限 的相关文章

随机推荐

  • 如何在httppost/httpget请求中设置标头

    我有一个像 post H Authorization Token token 2hwhdh443hdhfh43j3jdej3j3 这样的网络服务 如何在 httpPost get 请求中设置此标头 我的代码在这里 HttpClient cl
  • 是否可以为 .NET 编写函子接口?

    函数式语言通常具有Functor类型 接口 在 NET 中 Functor 接口是可通过泛型类型实现的接口 T a 有一个名为 fmap 的函数 该函数从容器类型 A 到不同的类型 B 并返回一个容器类型为 B 的对象 例如一个类型List
  • 使用 boost::spirit 读取空值

    我想将 CSV 读入结构中 struct data std string a std string b std string c 但是 我想读取空字符串以确保所有值都位于正确的位置 我将该结构调整为 boost fusion 因此以下工作有
  • 如何设置 Visual Studio Code 来编译 C++ 代码?

    微软的视觉工作室代码编辑器相当不错 但它没有默认支持构建 C 项目 我如何配置它来执行此操作 构建任务是特定于项目的 要创建新项目 请在 Visual Studio Code 中打开一个目录 Following the instructio
  • 使用 jQuery 解析远程内容的最佳实践是什么?

    在 jQuery ajax 调用检索整个 XHTML 文档之后 从结果字符串中选择特定元素的最佳方法是什么 也许有一个库或插件可以解决这个问题 jQuery 只能选择字符串中存在的 XHTML 元素 如果 W3C 规范中的 div 中通常允
  • 使用 Retrofit 将 json 结构转换为数组

    我遇到了 Retrofit 和 Trakt tv API 中丑陋的 json 对象的麻烦 season 1 episodes 1 true 2 true 3 false 4 false 5 false 6 false 7 false epi
  • 直接从 JSON 文件获取数据帧?

    首先 我要感谢所有为 Stackoverflow 和 R 做出贡献的人 我是那些不太擅长编程的 R 用户之一 但勇敢地尝试在工作中使用它 所以下面的问题可能是微不足道的 问题就在这里 我需要将 JSON 格式的文件导入到 R library
  • 滚动视图中的多个文本视图

    我有一项活动以文本形式显示步行方向 我有一组 TextView 其中 5 个包含 步骤 x 其中 x 是步骤号 另外 5 个 TextView 包含实际说明 问题是最后一个 TextView 离开屏幕 所以我想让这组 TextView 滚动
  • elasticsearch 2.0 父子孙子

    我的操作 父级 国家 地区 子级 分支机构 孙级 员工 PUT company 映射 分支 父 类型 国家 员工 父 类型 分支 我想添加一个孙子employee2 父级是分支 PUT company employee2 mapping e
  • 结构名称是否指向第一个元素?

    我发现了一些类似的问题 但没有一个有多大帮助 结构名称是否指向结构的第一个元素 类似于数组 struct example int foo int bar struct example e e foo 5 e bar 10 printf d
  • 隐藏提交按钮直到表单有效

    我对 Jquery 相当陌生 所以这可能是一个简单的问题 但是有没有办法隐藏表单上的提交按钮 直到验证所有字段 验证需要是 键入时 的解决方案 基本上我有 3 个字段 名字 姓氏和电子邮件 我希望提交按钮保持隐藏状态 直到填写了两个 名称
  • Ogre/Mogre:相机两点透视

    我正在显示一个场景 其中有一些立方体 相机采用的是透视法 一切都很好 但我希望垂直线是平行的 两点透视 http en wikipedia org wiki Perspective graphical 两点透视 从正面观察立方体时 我想要的
  • 如何将多列放入kivy RecycleView中?

    我想将 csv 表的数据放入 kivy recycleview 中 如果我为 kv 中的标签分配固定文本 我设法用一行插入多列 但我无法让它用字典列表中的数据填充标签 这是到目前为止我用来测试这个概念的代码 from kivy app im
  • 如何将存储过程的输出返回到sql server中的变量中

    我想在 SQL Server 中执行存储过程并将输出分配给变量 它返回单个值 这取决于您想要返回的信息的性质 如果它是单个整数值 则可以使用return陈述 create proc myproc as begin return 1 end
  • #include 检测到错误。请在 Visual Studio 代码中更新您的 includePath

    我正在尝试在 Visual Studio Code 中编译 C 文件如何使用 C C 扩展并添加包含路径到配置但我收到此错误 include errors detected Please update your includePath Sq
  • 使用javascript,如何在单击表格单元格时获取其背景颜色?

    我想要弹出一个警报 每当我单击它时 它都会显示表格单元格的背景 我似乎无法找到或弄清楚如何获取背景颜色 我的表格单元格如下所示 td 0 td 我的 selectCell 函数如下所示 function selectCell e alert
  • 如何在 C# 中将消息框显示为系统模式?

    如何在 C 中将消息框显示为系统模式 例如vbModal在 Visual Basic 6 中 使用 C MessageBoxIndirect 包装器进行高级 MessageBoxing
  • 将 Spring Security 与 SiteMinder 集成

    如何将 Spring Security 与 SiteMinder 集成以接收用户和角色 我有一个在内存中使用 Spring Security 的项目设置 我想使用它来接受具有用户和角色的 SiteMinder 标头 如果SiteMinder
  • 尝试使用 Scrapy 抓取 LinkedIn 时出现 999 响应

    我正在尝试使用 Scrapy 框架从 LinkedIn 中提取一些信息 我知道他们对尝试抓取其网站的人非常严格 因此我在 settings py 中尝试了不同的用户代理 我还指定了较高的下载延迟 但它似乎仍然立即阻止了我 USER AGEN
  • SKScene 无法释放内存,导致内存增长有限

    我已经为此苦苦挣扎了好几天 由于某种原因 我的 SKScenes 没有正确释放 这导致内存增长有限 因为每次我退出并进入场景时 内存都会跳跃 这意味着游戏进行 10 轮后应用程序崩溃 据我所知 经过多次检查后 我没有任何保留周期或对场景本身