2012-04-14 44 views
0

我想在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. 
    } 
} 

我使用循環與它睡眠方法來檢查每個按鈕將不同數量的增加(只要intSelected爲0的變量嘗試,循環會繼續,BTN1將增加1等等)

但是,這導致程序掛起。

另外,我覺得這將是一個混亂,不專業的做我想做的事情的方式。雖然我可能不是專業人士,但我有標準。

有沒有人知道一種方法來暫停程序執行,直到按下按鈕?或者對如何完成我想完成的任務有更好的想法?我不能簡單地將函數編碼到點擊事件中,因爲這些按鈕所做的會不斷變化。

我會繼續說C#不是我強大的語言。我最初是在VB.Net中完成這項工作,但遇到了同樣的問題,並導致相信C#更加靈活。我甚至熟悉的唯一C語言是C++,而我的課程至少在一年半以前。所以,請原諒'笨'的編碼,因爲我是生鏽的小

+0

也許嘗試:'btn1_Click(this,EventArgs.Empty)' – 2012-04-14 03:16:36

+0

不像它看起來現在,給了我錯誤。將其添加到cls_EVENTS文件。但添加frmMain.MainForm。到btn1_Click開始擺脫它的錯誤,然後用frmMain.MainForm替換'this'也擺脫了它的錯誤;但最終預計會得到分號。 – Joseph 2012-04-14 03:31:17

回答

0

經過一些更多的實驗和更多的Google搜索之後,我想我可能已經找到了它。

public static void MainMenu() 
{ 
    frmMain.MainForm.ScreenTextClear(); 

    frmMain.MainForm.ButtonEnable(true, false, false, false, false, false); 
    frmMain.MainForm.ScreenText(true, "Welcome to the alpha of this game."); 
    frmMain.MainForm.ScreenText(true, "Hi"); 
    frmMain.MainForm.ButtonText("New Game", "Load Game", "About", "---", "---", "---"); 

    // Potential solution. 
    frmMain.MainForm.btn1.Click += delegate(object sender, EventArgs e) 
     { 
      NewGame(); 
     };  
} 

這似乎是在做我想做的事情。文本加載,按鈕激活,然後點擊它們啓動適當的方法。上升重複。