Algalogica
Algalogica

Reputation: 11

Getting classes in XNA to 'talk' to eachother

I have two classes in XNA.

maingame.cs and player.cs

In my maingame.cs I have a rectangle that is drawn wherever the mouse is, I use this for clicking.

My player.cs has a rectangle for the players sprite.

I Have No idea how to make these two classes 'talk' to eachother so that I can have something like

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

The problem is that playerRectangle is in the Player.CS and the mouseRectangle is in the maingame.CS

How do I get these two to talk to eachother? I've been Googling for hours now with no luck.

Player.CS looks like

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);
}

}

}

I'm trying to make it so that my mouse rectangle in MainGame.CS is able to click it by doing

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

Upvotes: 0

Views: 2012

Answers (2)

A-Type
A-Type

Reputation: 1148

I'm not sure if I'm stating the obvious here, but you need a public accessor for your Rectangle in your Battleship class. If you don't understand what that means then you need to do some reading on the basics of object-oriented programming. For now, though, if you modify the code of your Battleship class to say

public Rectangle playerBattleshipRectangle;

... you can then access it from your maingame.cs' reference to the player object using

player.playerBattleshipRectangle

(assuming your player is a Battleship class. If it's not then you need to be more clear in your question and post the class source code for whatever class your player is. You say "player.cs" but post the class Battleship-- which is it? If the filename is player.cs but the class name is actually battleship you should change one of them so that they are the same. Practice good, easy to understand class and variable names; be as descriptive as possible without being too wordy).

By default, if you include no scope modifier (public, private, protected, internal...) before your member fields in a class, then they are set to private and not accessible by other classes.

You can also use Properties to have more control over access to a class' member fields. For instance,

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

this code contains a private (only this class can access it) definition of the health of an object. It also provides a public way for other classes to 'see' the health of that object and change this health. As you can see in the 'set' section, when another class sets the health of this object it is automatically clamped between 0 and 100. You could also omit 'set' entirely and no other class could change the health, they could only see it.

This is all the basics of object-oriented programming in C#. I strongly encourage you to start from the basics if you don't know them. You cannot successfully create a game without understanding scope, properties, inheritance, object instances and references, just to name a few.

A quick summary of some relevant definitions from this post:

  • field - aka a variable; a basic trait stored in a class as a 'member'. i.e., 'health' or 'boundingBox'.
  • property - an accessor for a field which is useful for defining how outside classes can see and modify it.
  • instance - a 'real' object which is stored in memory. a class only defines the behavior of an object; it must be made into an instance for the object to actually exist. A class like Battleship can be used to make unlimited instances of battleships in memory. When you say Battleship player = new Battleship(), you are creating an instance of Battleship called 'player'.
  • private - placing 'private' before a field means only this class can see it.
  • public - placing 'public' before a field means all classes can see it.
  • protected - placing 'protected' before a field means only this class and any classes inherited off of this class can see it.
  • internal - placing 'internal' before a field means only classes in this class' namespace can see it.

I apologize if I underestimate your knowledge of the subject.

Upvotes: 2

Oded
Oded

Reputation: 499002

One of the classes needs to hold a reference to the other.

With such a reference, it can call methods on it, set properties etc.

You can do this by passing in an instance in the constructor or as a property.

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

To access it outside of the constructor, you should use a field and assign it in the constructor (or property setter, whichever you decide to use):

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

To avoid tight coupling, you may want to investigate using events.

Upvotes: 1

Related Questions