After a quick play, I've got my player sprite moving across the screen by itself. I'll simply have to write the appropriate methods for controlling it.
How to do read and write to a text file from resource?
I have already created and populated the text file, and I have added it to my program. I'm just having trouble figuring out how to access it.
edit:
Code:
/// <summary>
/// Loads the high scores from a resource file
/// </summary>
public void LoadScores()
{
StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(SpaceInvaders.Properties.Resources.hiscores));
string line;
string[] fileArray;
while (!reader.EndOfStream)
{
line = reader.ReadLine();
fileArray = line.Split(','); // splits the information from the csv file by commas
string name = fileArray[0];
int score = int.Parse(fileArray[1]);
Score s = new Score(name, score);
scoresList.Add(s);
listBoxScores.Items.Add(s.GetScore());
Debug.WriteLine(s.GetScore());
}//while
reader.Close();
}
This is my code as it is, I'm being thrown this error in the output window:
A first chance exception of type 'System.ArgumentNullException' occurred in mscorlib.dll
My thinking is the file not being read in correctly.
Where do I find the keydown/press/up for the arrow keys? Do I need some sort of listener or can I just have an event on my form that triggers when an arrow key is pressed?
Posts: 811
Threads: 80
Joined: Oct 2011
Staff roles: Coding Dev
Try this Crackpunch
In Form1() or whatever your constructor is called
this.KeyDown += HandleKeyDown;
void HandleKeyDown (object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up) {
//do stuff here
}
}
Also for your high scores use a file instead of a resource, you can't write to a resource. Also make sure that scoresList has been set somewhere. So in Form1() Or Form1_Load etc. make sure there is a line that says scoresList = new List<string>();
Add me on skype at crazy.ichiru if you want any more help. I've got enough experience
The key presses still aren't working. I'm thinking it might have something to do with my listbox on the form, as I can see it selecting things while I'm actually trying to steer my space ship.