XNA - 如何编辑另一个类的矩形边界

时间:2011-11-03 21:22:05

标签: c# windows xna

所以我遇到这个问题,我的主游戏类文件中有一个名为StarBounds的变量。该文件现在名为MainGameClass.cs,名称空间为StarCatcher。

我已经创建了一个类来检测鼠标是否悬停在StarBounds变量上然后单击。点击后,我想从另一个名为GameFunctions.cs的类中编辑StarBounds变量。

我能做点像...... MainGameClass mgc = new MainGameClass();

触发悬停和点击事件的时候,我可以输入而不会出错: mgc.StarBounds =新矩形(0,0,0,0);

但在实际游戏中它并没有改变。而且我在做“mgc.StarBounds = new rectangle(0,0,0,0);”时有时会出错。说它没有对象参考。

1 个答案:

答案 0 :(得分:0)

我认为这很可能只是一个范围问题。引起异常是因为mgc为null。确保GameFunctions尚未声明MainGameClass的本地副本,并且正在引用预先存在的实例。否则,请为StarBounds使用静态变量,如示例中所示。例如,

public class MainGameClass {
    public static Rectangle StarBounds;

    public void HandleInput () {
        // if GameFunctions.ClickedWithinStarBounds(mouse)
        // GameFunctions.OnClickStarBounds()
    }
}

public class GameFunctions {
    public static void ClickedWithinStarBounds(MouseState mouse) {
        // create a rectangle around the mouse (ie. cursor) for the detection area
        // return left mouse button is down or pressed && IsWithinStarBounds
    }

    public static bool IsWithingStarBounds(Rectangle area) {
        return (MainGameClass.StarBounds.Intersects(area);
    }

    public static void OnClickStarBounds() {
        MainGameClass.StarBounds = Rectangle.Empty;
    }
}