2014-01-05 65 views
0

我正在製作使用XNA的遊戲,遊戲的一部分涉及收集滴劑。我具有低於此代碼檢測字符和項目之間的交叉:即使條件得到滿足,BackColor也不會改變

//Intersection Code, If the character intersects with the item while the item is showing, run below 
if (alive && charRange.Intersects(itemRect)) 
{ 
    alive = false; //stop showing the item 
    Inv.ItemGot(); //Call the ItemGot class, which adds the item to the inventory screen 
} 

另一類包含ItemGot()方法,以及用於該代碼是下面:

public void ItemGot() 
{ // Called from the ItemList class... 
    // Sets the background color to black when called 
    btnItems[0] = new Panel(); 
    btnItems[0].BackColor = Color.Black; 
} 

基本上,字符相交時對於項目矩形,btnItems[0]的顏色應該從CornflowerBlue(我之前設置)變爲Black。但是,調用方法時顏色不會更改,我不知道爲什麼。我的代碼似乎是正確的,我已經有同行確認了我。

+0

你在'ItemGot()'中設置了一個斷點來確認它被調用嗎? – Andrew

+0

@Andrew是的,因爲btnItems [0]被填充,ItemGot()被調用 – demiZe

+0

你在'ItemGot()'中初​​始化'btnItem [0]'。 'btnItem [0]'等於'panel 1'嗎? – user3093781

回答

0

您正在創建一個new Panel,但從不使用它。很可能你不想創建一個新的,因爲你說的顏色已經設置爲CornFlowerBlue

因此,如果您已經有Panel設置...請確保將其添加到btnItems列表中。這樣你就可以通過btnItems[0]訪問它。

+0

我擺脫了'btnItems [0] = new Panel();'但現在我得到一個NullReferenceException。事情是,我已經在構造函數中初始化面板數組。 – demiZe

+0

我在我的答案中添加了詳細信息。 – Andrew

相關問題