让XNA中的课程彼此“交谈”

时间:2012-02-16 11:55:20

标签: c# xna

我在XNA中有两个类。

maingame.cs 和 的 player.cs

在我的 maingame.cs 中,我在鼠标所在的位置绘制了一个矩形,我使用它来点击。

我的 player.cs 有一个玩家精灵的矩形。

我不知道如何让这两个班级“互相交谈”,以便我可以拥有类似的东西

if (ButtonState.Pressed == mouseState.LeftButton && mouseRectangle.Intersects(playerRectangle))
            {
                //draw something
            }

问题是playerRectangle位于 Player.CS 中,而mouseRectangle位于 maingame.CS

如何让这两个人互相交谈?我一直在谷歌搜索几个小时没有运气。

Player.CS看起来像

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace PlanetDefence
{
    class Battleship
{
//Textures
Texture2D playerBattleshipTexture;

//Rectangles
 Rectangle playerBattleshipRectangle;

//Integers

public Battleship(Texture2D newPlayerBattleshipTexture, Rectangle newPlayerBattleshipRectangle)
{
    playerBattleshipTexture = newPlayerBattleshipTexture;
    playerBattleshipRectangle = newPlayerBattleshipRectangle;

    newPlayerBattleshipRectangle = new Rectangle(100, 100, 100, 100);
}

public void Update()
{

}

public void Draw(SpriteBatch spriteBatch)
{
    //Draw the player Battleship
    spriteBatch.Draw(playerBattleshipTexture, playerBattleshipRectangle, Color.White);
}

}

}

我正在尝试使其在MainGame.CS中的鼠标矩形能够通过执行来单击它

    if (ButtonState.Pressed == mouseState.LeftButton && mouseRectangle.Intersects(playerBattleshipRectangle))
    {
        playerBattleship.Draw(spriteBatch);
    }

2 个答案:

答案 0 :(得分:2)

我不确定我是否在这里说明了这一点,但你需要在你的战舰类中使用公共存取器作为你的矩形。如果您不明白这意味着什么,那么您需要阅读面向对象编程的基础知识。但是,现在,如果您修改Battleship类的代码来说明

public Rectangle playerBattleshipRectangle;

...然后你可以使用

从你的maingame.cs对玩家对象的引用中访问它
player.playerBattleshipRectangle

(假设你的玩家是战舰类。如果不是那么你需要在你的问题中更清楚,并发布你的玩家所在类别的类源代码。你说“player.cs”但发布战舰类 - 这是什么?如果文件名是player.cs,但是类名实际上是战舰,你应该更改其中一个,以便它们是相同的。练习好的,易于理解的类和变量名称;尽可能具有描述性太罗嗦了。)

默认情况下,如果在类中的成员字段之前不包含范围修饰符(public,private,protected,internal ...),则它们将设置为private,而其他类不可访问。

您还可以使用“属性”来更好地控制对类成员字段的访问。例如,

private int health;
public int Health
{
    get { return health; }
    set { health = MathHelper.Clamp(health, 0, 100); }
}

此代码包含一个私有(只有这个类可以访问它)定义一个对象的健康状况。它还为其他类提供了一种公共方式,以“看到”该对象的健康状况并改变其健康状况。正如您在'set'部分中看到的,当另一个类设置此对象的运行状况时,它会自动夹在0到100之间。您也可以完全省略'set',其他类不会改变运行状况,他们只能看到它

这是C#中面向对象编程的所有基础知识。如果你不了解它,我强烈建议你从基础开始。如果不了解范围,属性,继承,对象实例和引用,则无法成功创建游戏,仅举几例。

本文中一些相关定义的快速摘要:

  • 字段 - 又名变量;作为“成员”存储在类中的基本特征。即'health'或'boundingBox'。
  • property - 字段的访问器,用于定义外部类可以查看和修改它的方式。
  • instance - 存储在内存中的“真实”对象。一个类只定义一个对象的行为;它必须成为实际存在的对象的实例。像战舰这样的类可以用来在战争中制造无限的战舰实例。当你说Battleship player = new Battleship()时,你正在创建一个叫做“玩家”的战舰实例。
  • 私人 - 在字段之前放置'private'意味着只有这个类可以看到它。
  • 公开 - 在字段之前放置'public'意味着所有类都可以看到它。
  • protected - 在字段之前放置'protected'仅表示此类,并且从此类继承的任何类都可以看到它。
  • internal - 在字段之前放置'internal'意味着只有此类命名空间中的类才能看到它。

如果我低估了你对这个问题的了解,我很抱歉。

答案 1 :(得分:1)

其中一个类需要保持对另一个的引用。

通过这样的引用,它可以调用方法,设置属性等。

您可以通过在构造函数中传入实例或作为属性来执行此操作。

public Maingame(Player player)
{
  // use the Player instance
}

要在构造函数之外访问它,您应该使用一个字段并在构造函数(或属性设置器中,无论您决定使用哪个)中指定它:

Player myPlayer;
public Maingame(Player player)
{
  myPlayer = player;
  // use the myPlayer field in the class
}

为避免紧密耦合,您可能需要使用events进行调查。

相关问题