离开视图控制器,退出游戏

时间:2014-09-26 19:00:59

标签: ios class memory-management

我正在制作游戏,当用户按下菜单VC中的按钮时,游戏VC会打开并在viewDidLoad我有设置游戏的方法(ergo,创建{{1的实例) }););创造一副扑克牌,创建玩家,为玩家添加牌等等。现在我想要取消/退出游戏的可能性,显然我有一个按钮。当我按下该按钮时,菜单VC会再次显示,但Game类仍然存在。正如我在日志中看到的那样,Ai玩家甚至还在玩。

我的问题是我如何取消游戏并“解除分配”Game类并清除玩家阵列,清除牌组等等。据我所知,我无法“解除分配”对象,因为ARC中不允许这样做。

我知道如何改变观点,但只需要一种方法来退出游戏。

关于Game类的更多信息;

Game

因此,在gameViewController中使用了@implementation Game { int _activePlayerPosition; NSArray *activePlayersArray; //Player *activePlayer; } +(Game *) game { static Game *game = nil; if(game == nil) { game = [[super allocWithZone:nil] init]; } return game; } -(id) init { if((self = [super init])){ } return self; } -(void) setupGame { NSLog(@"setup game"); _deckCard = [[DeckCard alloc]init]; _tableCard = [[TableCard alloc]init]; _players = [NSMutableDictionary dictionaryWithCapacity:4]; _deckCards = _deckCard.cardsInDeck; _tableCards = _tableCard.cardsOnTable; //If game is single player, start single player game. if([Game game].isSinglePlayerGame) { [self startSinglePlayerGame]; } _activePlayerId = 0; //Check playabality of all playercards in Game. NSArray *playersArray = [[[Game game] players] allKeys]; for(int x = 0; x <[playersArray count];x++){ id key = [playersArray objectAtIndex:x]; id player = [[[Game game] players] objectForKey:key]; [player isPlayerAllowedToPlay]; } } 类的实例,其中包含所有其他创建的对象(数组,播放器等)。

那我该如何退出游戏?

谢谢!

1 个答案:

答案 0 :(得分:0)

感谢Crimson Chris和Tommy,他们让我走上了正确的道路。我改变了下面的单例类,并添加了一个重置​​为nil的方法。

static Game *game = nil;

+(Game *) game {
if(game == nil) {
    game = [[super allocWithZone:nil] init];
}
return game;
}

+ (void)resetGame {
game = nil;
}

谢谢大家!

相关问题