Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting classes in XNA to 'talk' to eachother
    primarykey
    data
    text
    <p>I have two classes in XNA.</p> <p><strong>maingame.cs</strong> and <strong>player.cs</strong></p> <p>In my <strong>maingame.cs</strong> I have a rectangle that is drawn wherever the mouse is, I use this for clicking.</p> <p>My <strong>player.cs</strong> has a rectangle for the players sprite.</p> <p>I Have No idea how to make these two classes 'talk' to eachother so that I can have something like </p> <pre><code>if (ButtonState.Pressed == mouseState.LeftButton &amp;&amp; mouseRectangle.Intersects(playerRectangle)) { //draw something } </code></pre> <p>The problem is that playerRectangle is in the <strong>Player.CS</strong> and the mouseRectangle is in the <strong>maingame.CS</strong></p> <p>How do I get these two to talk to eachother? I've been Googling for hours now with no luck.</p> <p>Player.CS looks like</p> <pre><code>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); } </code></pre> <p>}</p> <p>}</p> <p>I'm trying to make it so that my mouse rectangle in MainGame.CS is able to click it by doing</p> <pre><code> if (ButtonState.Pressed == mouseState.LeftButton &amp;&amp; mouseRectangle.Intersects(playerBattleshipRectangle)) { playerBattleship.Draw(spriteBatch); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload