我正在制作一个迷你点击游戏,它不是什么大事,但我在启用我的按钮时出现问题,但我可以禁用它。我还在学习,我认为可以提出这样的愚蠢问题。 :d
这是我的代码:
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 Diamond_Clicker
{
public partial class Form1 : Form
{
private int clicks = 0;
private int counter = 1;
public Form1()
{
InitializeComponent();
}
private void myDiamond_MouseUp(object sender, MouseEventArgs e)
{
myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond Clicker\\diamond.png");
}
private void myDiamond_MouseDown(object sender, MouseEventArgs e)
{
myDiamond.Image = Image.FromFile("C:\\Matej Dodevski\\Semos\\C#\\Diamond Clicker\\diamondMouseUp.png");
clicks++;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
}
private void timer1_Tick(object sender, EventArgs e)
{
clicks++;
}
private void timer1_Tick_1(object sender, EventArgs e)
{
counter++;
clicks = clicks + 1;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
clicks = clicks - 50;
DiamondsScore.Text = "Diamonds: " + clicks.ToString();
timer1.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
if (clicks > 5)
{
button1.Enabled = true;
}
else
button1.Enabled = false;
}
}
}
答案 0 :(得分:1)
Load Event旨在执行一次,就在表单显示在屏幕上之前。通常,此事件是您进行某种初始化的地方。
您需要做的是将该代码放入函数中:
private void UpdateButton()
{
if (clicks > 5)
button1.Enabled = true;
else button1.Enabled = false;
// This function can be reduced to one line of code:
// button1.Enabled = clicks > 5;
}
然后你需要在你的button1_click函数,timer1_tick函数,mousedown函数和你的timer1_tick_1函数结束时调用这个函数。基本上,进入任何可以改变点击变量的函数。