Ben*_*Ben 2 c# xna android object
我正在用C#和XNA制作游戏,它将针对Android和Windows Phone 7,7.1,8等移动设备.它将使用一个名为Minigame的对象来确定玩家将要玩的当前游戏(设置一个小游戏)在农场,另一个在城市,等).创建小游戏的代码设置如下.
public class Game1 : Microsoft.Xna.Framework.Game
{
//This is a global variable
Minigame minigame;
...
//This method loads the minigame when the player chooses one from the main menu
//It is loaded using an instance of a class that inherits from the Minigame class
void LoadMinigame(int number)
{
if (number == 0)
{
minigame = new MinigameOne();
}
else if (number == 1)
{
minigame = new MinigameTwo();
}
else
{
minigame = new MinigameThree();
}
}
}
Run Code Online (Sandbox Code Playgroud)
小游戏总是使用对象的"新"实例加载,但是当小游戏完成并且玩家选择另一个时,永远不会有任何形式的删除.C#是否希望我做类似以下的事情,或者我是否可以一次又一次地使用"new"而不会找到删除变量的方法(即下面相同的代码,减去删除命令)?
minigame = new MinigameOne();
//Do something with the minigame
delete minigame; //I don't think this command exists in C#, but is there an equivalent?
minigame = new MinigameTwo();
//Do something again
delete minigame;
minigame = new MinigameThree();
Run Code Online (Sandbox Code Playgroud)
删除对对象的所有引用后,它就有资格进行垃圾回收.通过覆盖保存的引用minigame,您将删除对前一个迷你对象的引用.只要没有其他引用,一段时间之后垃圾收集器就会吞噬它.
我建议您阅读"垃圾收集的基本原理",以便更好地了解对象如何达到其生命周期的终点.
| 归档时间: |
|
| 查看次数: |
94 次 |
| 最近记录: |