Mark_Davies

Hi i am making a simple pool game and have my balls in an array (each ball runs through a class to set the properties. i have a Texture2D property to set the texture so i can set the white ball colour and yellow and red ball colour. however i have set up the array and try to draw but it will not accept the array property as a texture it keeps saying it is of null value even though i have set each of the balls here is the code below as you may understand a little more.

//ball class for each ball

public class balls

{

public Texture2D tex;

public float x;

public float y;

public Vector2 v = new Vector2();

public float r = 12.5f;

public float m = 10;

}

//array of balls

balls[] pBalls = new balls[] {new balls(), new balls(), new balls(), new balls(), new balls(),

new balls(), new balls(), new balls(), new balls(), new balls(), new balls(),

new balls(), new balls(), new balls(), new balls(), new balls(), new balls()};

//setting the ball sprite batch and texture for a red ball

sBalls = new SpriteBatch(this.graphics.GraphicsDevice);

ContentManager BallsLoader = new ContentManager(Services);

tBalls = BallsLoader.Load<Texture2D>("RedAlpha") as Texture2D;

pBalls[0].tex = tWBall;

for (int i = 1; i < 16; i++)

{

pBalls[ i].tex = tBalls;//space there for forum use only

}

tWBall is white ball same code as above but whit texture and different variable names where needed.

//sprite batch for balls b.tex is the texture that is set but this is where the error keeps showing

foreach (balls b in pBalls)

{

sBalls.Begin(SpriteBlendMode.AlphaBlend);

sBalls.Draw(b.tex, new Rectangle((int)b.x, (int)b.y, 25, 25), Color.White);

sBalls.End();

}i hope the code is clear enough for you all. if you have any questions or would like the full cs code file let me know and i will send it to you.

Regards

Mark



Re: XNA Game Studio Express loading textures on array

Glenn Wilson

I think I have it right....

you want to have an array of textures loaded, here is a simple method that I am using.

Declare your array under the main dec' for the contant manager

private Texture2d[] textureArray;

then when loading the content trough the pipline...

this.textureArray = new Texture2d[5];
this.textureArray[0] = content.load<Texture2d>("Ball_01");
this.textureArray[1] = content.load<Texture2d>("Ball_02");
this.textureArray[2] = content.load<Texture2d>("Ball_03");
this.textureArray[3] = content.load<Texture2d>("Ball_04");
this.textureArray[4] = content.load<Texture2d>("Ball_05");

Hope it helps.






Re: XNA Game Studio Express loading textures on array

Mark_Davies

hey thanks for the reply glenn however i dont think you full understand i have an array of 15 elements and each array contains a new version of a class which controls the individual part of the ball. in the class one variable is a texture2d variable and i set the texture to the one it should be however it looks as if the draw property is not seeing it as a valid argument to put in where the texture variable should be placed (it looks as if the array property is null where do i need to set the texture for it to work do you think i am doing it in LoadGraphicsContent method at the mo).

regards
Mark




Re: XNA Game Studio Express loading textures on array

Calvin Bell

Are you sure it's the texture that XNA is argueing about It doesnt look like you are setting any of the balls' XY coordinates when you create it. Just from looking at it, it looks like the textures are fine... maybe I'm not seeing something here.





Re: XNA Game Studio Express loading textures on array

Mark_Davies

yea i am setting everything else correctly. i no this as when i change the property to a texture and not the texure property of an array element it works fine.
regards
Mark




Re: XNA Game Studio Express loading textures on array

Arek Bal

Do it this way and all will be fine.
Fields:
balls[] pBalls;
Init:

pBalls = new balls[] {new balls(), new balls(), new balls(), new balls(), new balls(),

new balls(), new balls(), new balls(), new balls(), new balls(), new balls(),

new balls(), new balls(), new balls(), new balls(), new balls(), new balls()};






Re: XNA Game Studio Express loading textures on array

Fluxtah

Your ball class

public class Ball
{
  public Texture2D Texture;
  public float PositionX;
  public float PositionY;
  public float Radius = 12.5f;
  public float Mass = 10.0f;
}

Creating an array of 16 balls and setting the same texture for all of them.

    Ball[] balls = new Ball[16];

    for (int i = 0; i < balls.Length; i++)
    {
      ballsIdea = new Ball();
      ballsIdea.Texture = (Texture2D)_ContentManager.Load<Texture2D>("RedAlpha");
    }


This is far from ideal, but its a start, you would obviously make your balls array a member variable of your Game class, you may wish to expose the Ball members as public properties rather than public fields (although I must admit I do not do this, only for business apps!).





Re: XNA Game Studio Express loading textures on array

Mark_Davies

hi arek doing that just comes up with many errors (i removed my previous array set obviously)
regards
Mark




Re: XNA Game Studio Express loading textures on array

Arek Bal

Allright...new Project >> Windows Application >> one button and label...double click on button...and paste it into code editor....it works.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
balls[] pBalls;
//class
class balls
{
public string text = "word";
}
//main
public Form1()
{
pBalls = new balls[]{new balls(),new balls()};
InitializeComponent();
}
//
private void button1_Click(object sender, EventArgs e)
{
label1.Text = pBalls[1].text;
}
}
}
Edit1: Hmm I wonder....so did you just done as Fluxtah said...or what (He done it good,. but gave no answer where the problem is)
Maybe it's to late for me.




Re: XNA Game Studio Express loading textures on array

Mark_Davies

ignore that if you saw it i done it thanks for the help dude apprechiate it
and thank you everyone that posted
regards
Mark




Re: XNA Game Studio Express loading textures on array

Calvin Bell

So are you at least going to tell us what was wrong with it





Re: XNA Game Studio Express loading textures on array

Mark_Davies

yea sorry lol well i chaged the thing arek told me to do and then dropped a element from the array and added a few more numbers in the foreach loop (so it could go through the correct number of elements) and then bobs your uncle it just desided to work lol. i now i just need to find out how i can get my cue to center on me white ball image.
regards
mark




Re: XNA Game Studio Express loading textures on array

Calvin Bell

Wow, sounds like quite a journey.







Re: XNA Game Studio Express loading textures on array

Mark_Davies

yea calvin wish me luck lol.
regards
mark