[英]C# label string to int conversion error
I am making a very small RPG game in C# to practice some skill (and to have fun!). 我正在用C#制作一个非常小的RPG游戏,以练习一些技能(并玩得开心!)。 I have gotten pretty far with the images, buttons and such.
我对图像,按钮等已经走得很远了。 My issue is that I am being thrown an error when trying to convert my label strings into integers to be compared for my
attackingPhase()
method. 我的问题是,在尝试将标签字符串转换为要与我的
attackingPhase()
方法进行比较的整数时,我被抛出错误。
Here is my code and a screenshot of the error. 这是我的代码和错误的屏幕截图。
I believe my code is correct but I can not figure out as to why the error is being thrown. 我相信我的代码是正确的,但我无法弄清楚为什么会引发该错误。
Thank you for all help. 感谢您的所有帮助。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RPG
{
public partial class Form2 : Form
{
private Form1 mainForm = null;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
pictureBox1.Image = mainForm.MyPictureBoxEnemy.Image;
pictureBox2.Image = mainForm.MyPictureBoxHero.Image;
lbl_Health_Value_Enemy.Text = "100";
lbl_Health_Value_Hero.Text = "100";
}
public void attackingPhase()
{
Random rnd = new Random();
int enemy_damage = rnd.Next(1, 25);
int hero_damage = rnd.Next(2, 15);
var enemyHealth = Convert.ToInt32(lbl_Health_Value_Enemy);
var heroHealth = Convert.ToInt32(lbl_Health_Value_Hero);
if((enemyHealth & heroHealth) > 0)
{
enemyHealth = enemyHealth - enemy_damage;
heroHealth = heroHealth - hero_damage;
} else
{
MessageBox.Show("DEAD");
}
lbl_Health_Value_Enemy.Text = enemyHealth.ToString();
lbl_Health_Value_Hero.Text = heroHealth.ToString();
}
private void btnAttack_Click(object sender, EventArgs e)
{
attackingPhase();
}
}
}
You need to convert the Text property of a label, 您需要转换标签的Text属性,
var enemyHealth = Convert.ToInt32(lbl_Health_value_Enemy.Text);
var heroHealth = Convert.ToInt32(lbl_Health_Value_Hero.Text);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.