Reputation: 11
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
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:
I apologize if I underestimate your knowledge of the subject.
Upvotes: 2
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