Game Development Community

C# issue

by Anthony Ratcliffe · in Technical Issues · 05/25/2008 (12:33 pm) · 15 replies

What i'm trying to do is print a random number between 1-6 in a lbl called lblrool but it seems to mess up and i cant see why


private void button2_Click(object sender, EventArgs e)
{
Random randNum = new Random();

randNum.Next(1, 6);
}

private void label3_Click(object sender, EventArgs e)
{

}

private void lblRool_Click(object sender, EventArgs e)
{
lblRool.Text = "damage taken +RanNum+ "!";
}

#1
05/25/2008 (12:41 pm)
You make it so you have to click the label. Do you not want the text to appear when the button is clicked?
Im no expert but I think it should be something like this (at a glance)

Random randNum = new Random();
int rand;

private void button2_Click(object sender, EventArgs e)
{
int rand = randNum.Next(1, 6);
}

private void label3_Click(object sender, EventArgs e)
{

}

private void lblRool_Click(object sender, EventArgs e)
{
lblRool.Text = "damage taken" +rand+ "!";
}

If you do want the text to appear when the button is clicked then just gimme a shout.

Hope it helps

EDIT: Code errors, re-write
#2
05/25/2008 (2:44 pm)
I had to rearrange the code a bit but you were pretty much spot on m8 thanks
#3
05/25/2008 (3:02 pm)
Oo new problem insted of decreasing in value it reseting health then taking away

private void button2_Click(object sender, EventArgs e)
{
int health;

int total;

health = 50;

Random randNum = new Random();

int rand = randNum.Next(1, 6);


total = health - rand;
lblRool.Text = "damage taken " + rand + "!";
lblhealth2.Text = "Health " +total+ " !";
}

i thought using a new varible total i thought it would continue to decrease from total insted of resetting
#4
05/25/2008 (3:14 pm)
Instead of using total use:

health = health - rand

or if you mean that every time you click it resets the health to 50 then put "int health" and "health = 50" outside the click function so that it doesnt reset every time you click.
#5
05/25/2008 (3:37 pm)
Oo new problem insted of decreasing in value it reseting health then taking away

private void button2_Click(object sender, EventArgs e)
{
int health;

int total;

health = 50;

Random randNum = new Random();

int rand = randNum.Next(1, 6);


total = health - rand;
lblRool.Text = "damage taken " + rand + "!";
lblhealth2.Text = "Health " +total+ " !";
}

i thought using a new varible total i thought it would continue to decrease from total insted of resetting
#6
05/25/2008 (3:42 pm)
If i place

int health,health2;
health = 50;
health2 = 50;

out of the button function it calls errors
#7
05/25/2008 (4:12 pm)
If i could access your code i could probably help you better but i quickly did a mock up v.similar to your program and it works on the same principle, incrementally decreasing health without the need for the total variable:

public partial class Form1 : Form
    {
        Random randNum = new Random();
        int rand;
        int health = 50;

        public Form1()
        {
            InitializeComponent();
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            rand = randNum.Next(1,6);
            health = health - rand;
            lblNum.Text = "damage taken " + rand + "!";
            lblhealth.Text = health.ToString();
        }
    }

Hope it helps :)
#8
05/25/2008 (5:32 pm)
Sorry can i ask why u made health into a string when dealing with numbers?
#9
05/25/2008 (5:43 pm)
Heres the full code for you there error i'm getting is

Error 1 Invalid token '=' in class, struct, or interface member declaration C:\Users\Ant\Documents\Visual Studio 2008\Projects\Game\Game\Form1.cs 19 21 Game

Error 2 Invalid token '=' in class, struct, or interface member declaration C:\Users\Ant\Documents\Visual Studio 2008\Projects\Game\Game\Form1.cs 20 20 Game





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Game
{



public partial class Form1 : Form
{
int health2;
int health;
health2 = 50;
health = 50;

Random randNum2 = new Random();
Random randNum = new Random();

public Form1()
{
InitializeComponent();


}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void pbxUser_Click(object sender, EventArgs e)
{

}

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
pbxUser.Image = Image.FromFile("1.jpg");
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
pbxUser.Image = Image.FromFile("2.jpg");
}

private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
pbxUser.Image = Image.FromFile("3.jpg");
}

private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void button2_Click(object sender, EventArgs e)
{





int rand = randNum.Next(1, 6);


health2 = health2 - rand;
lblRool.Text = "Attack For " + rand + "!";
lblhealth2.Text = health2.ToString();

if (health2 == 0)
{
lblresult.Text = "You Lose ";
timer1.Enabled = false;
}

else if (rand == 1)
{
pbxroll.Image = Image.FromFile("a.jpg");
}
else if (rand == 2)
{
pbxroll.Image = Image.FromFile("b.jpg");
}
else if (rand == 3)
{
pbxroll.Image = Image.FromFile("c.jpg");
}
else if (rand == 4)
{
pbxroll.Image = Image.FromFile("d.jpg");
}
else if (rand == 5)
{
pbxroll.Image = Image.FromFile("e.jpg");
}
else if (rand == 6)
{
pbxroll.Image = Image.FromFile("f.jpg");
}


}

private void label3_Click(object sender, EventArgs e)
{

}

private void lblRool_Click(object sender, EventArgs e)
{

}

private void timer1_Tick(object sender, EventArgs e)
{


int rand2 = randNum2.Next(1, 7);


health = health - rand2;
lblRool2.Text = "Damage Taken " + rand2 + "!";
lblhealth.Text = health.ToString();

if (health == 0)
{
lblresult.Text = "You Win ";
timer1.Enabled = false;
}




}

private void lblRool2_Click(object sender, EventArgs e)
{

}

private void lblresult_Click(object sender, EventArgs e)
{

}

private void pbxroll_Click(object sender, EventArgs e)
{

}
}
}
#10
05/26/2008 (2:12 am)
I made it into a string because it wasn't accepting it as an integer. Could you email me the project files? That way I can read it easier and i can see what is hapening and process it in my head.

I can't make any promises but i'll do my best. My email is: msimps_uk hotmail com
#11
05/26/2008 (10:53 am)
Ok another problem my if statment doesnt seem to work

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Game
{



public partial class Form1 : Form
{
int health2;
int health;



Random randNum2 = new Random();
Random randNum = new Random();


public Form1()
{

health2 = 50;
health = 50;

if (health < 1)
{
lblresult.Text = "You Lose ";
timer1.Enabled = false;
}
else if (health2 < 1)
{
lblresult.Text = "You Win ";
timer1.Enabled = false;
}
InitializeComponent();


}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void pbxUser_Click(object sender, EventArgs e)
{

}

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
pbxUser.Image = Image.FromFile("1.jpg");
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
pbxUser.Image = Image.FromFile("2.jpg");
}

private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
pbxUser.Image = Image.FromFile("3.jpg");
}

private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void button2_Click(object sender, EventArgs e)
{





int rand = randNum.Next(1, 6);


health2 = health2 - rand;
lblRool.Text = "Attack For " + rand + "!";
lblhealth2.Text = health2.ToString();


if (rand == 1)
{
pbxroll.Image = Image.FromFile("a.jpg");
}
else if (rand == 2)
{
pbxroll.Image = Image.FromFile("b.jpg");
}
else if (rand == 3)
{
pbxroll.Image = Image.FromFile("c.jpg");
}
else if (rand == 4)
{
pbxroll.Image = Image.FromFile("d.jpg");
}
else if (rand == 5)
{
pbxroll.Image = Image.FromFile("e.jpg");
}
else if (rand == 6)
{
pbxroll.Image = Image.FromFile("f.jpg");
}


}

private void label3_Click(object sender, EventArgs e)
{

}

private void lblRool_Click(object sender, EventArgs e)
{

}

private void timer1_Tick(object sender, EventArgs e)
{


int rand2 = randNum2.Next(1, 7);


health = health - rand2;
lblRool2.Text = "Damage Taken " + rand2 + "!";
lblhealth.Text = health.ToString();





}

private void lblRool2_Click(object sender, EventArgs e)
{

}

private void lblresult_Click(object sender, EventArgs e)
{

}

private void pbxroll_Click(object sender, EventArgs e)
{

}
}
}


btw the other problem is fixed however when it reachs 0 it goes into negative numbers
#12
05/28/2008 (11:21 am)
Which if statement and what error do you get.
#13
05/28/2008 (4:14 pm)
The when health less than 1 stop timer display messge u win or lose
#14
05/28/2008 (8:24 pm)
The problem your having is caused because you are setting health to equal 50 right before the if statement. Therefore health will never be below one. You just need to initialize the variables in another location.
#15
05/28/2008 (8:51 pm)
Try setting your health in your main program as Viktor sugested, but make sure the health varibles are static, that way the information in the health varibles stays the same no matter which branch of code you are in, then you can do things like health =- rand, which will take the number in health, subtract the random number then put that number back into health. I hope that helps.

Casey