2014-01-05 61 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

相关问题