Visual Studio の C# で、比較的基本的なテキスト ベースの RPG を作成しようとしています。クラスのメソッドをフォームのボタンクリックに反応させようとしてハングアップしました。クリックされたボタンに基づいて、あるメソッドから別のメソッドに移動したい。
プログラムの基本的なレイアウトを紹介します。
frmMain と cls_EVENTS があります。もっと複雑になるでしょうが、それは私が実際に動作するようになるまでの時間です。
frmMain を、プレーヤーがプログラムとやり取りする場所にしたいと考えています。テキスト スクリーン、ボタン、ステータス メーターなど、楽しい機能がたくさんあります。テキスト画面の下には 6 つのボタン (それぞれ btn1 - btn6 とラベル付けされています) があり、現在のシーン/イベントで利用可能なアクションを反映するために、テキストを変更する予定です (それを調べて、右に行き、あなたと彼の母親であることをその男に伝えてください)。昨夜夕食を食べた)。
メイン メニューを「作成」するためのコントロールは cls_EVENTS にあります。これは主に、メイン メニュー ボタンを呼び出すだけでプレイヤーをメイン メニューに戻すことができるようにするためです (frmMain からコードをコピーして貼り付けるのではなく)。方法)。
public partial class frmMain : Form
{
//creates a static copy of the form that references to this form, I believe.
public static frmMain MainForm { get; private set; }
public frmMain()
{
InitializeComponent();
// sets the static form as a copy of this form named MainForm.
frmMain.MainForm = this;
// calls the MainMenu() method in the cls_EVENTS class.
cls_EVENTS.MainMenu();
}
public void btn1_Click(object sender, EventArgs e)
{
}
//Same thing all the way down to btn6_Click
//And a couple of functions for manipulating the controls on the form...
//(enabling buttons, pushing text to the textbox, etc)
私の cls_EVENTS は、実際にイベントをコーディングしたい場所です。テキスト ボックス内のテキストを変更する MainMenu イベントと同様に、現在のオプションを反映するようにボタンのテキストを変更し、不要なボタンを無効にします。
public class cls_EVENTS
{
/*the Main menu, solely here for 'conveinence' (so I can have a Main Menu button on my form)*/
public static void MainMenu()
{
//clears the text screen
frmMain.MainForm.ScreenTextClear();
//Enables the first button, disables the rest.
frmMain.MainForm.ButtonEnable(true, false, false, false, false, false);
//indent = true, write text to screen.
frmMain.MainForm.ScreenText(true, "Welcome to the alpha of this game.");
//test to make sure it ADDS TO the text rather then overwriting.
frmMain.MainForm.ScreenText(true, "Hi");
//changes the six button texts to the following six strings
frmMain.MainForm.ButtonText("New Game", "Load Game", "About", "---", "---", "---");
//problem area
//when player clicks button one
//start the NewGame() method
//when player clicks button two, after I eventually code such a function.
//start the LoadGame() function
//etc.
}
Public void NewGame()
{
//etc.
}
}
Sleepメソッドを含むループを使用して、各ボタンが異なる数を追加する変数をチェックしようとしました(intSelectedが0である限り、ループは継続し、btn1は1を追加するなど)。
しかし、それが原因でプログラムがハングアップしました。
それに、自分のやりたいことをやるには、面倒でプロらしくない方法だと思います。私はプロではないかもしれませんが、私にも基準があります。
ボタンが押されるまでプログラムの実行を一時停止する方法を知っている人はいますか? または、やりたいことをやり遂げる方法についてより良いアイデアがありますか? これらのボタンが行うことは常に変化するため、単純に関数をクリック イベントにコーディングすることはできません。
C# は私が得意とする言語ではありません。私はもともと VB.Net でこれを行っていましたが、同じ問題に遭遇し、C# はもう少し柔軟であると信じるようになりました。私が漠然と知っている唯一の C 言語は C++ であり、少なくとも 1 年半前は C++ のクラスに参加していました。私は少しさびているので、「ばかげた」コーディングを許してください。