我正在使用XNA制作游戏,游戏的一部分涉及收集滴。我有下面的代码检测角色和项目之间的交集:
//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 :(得分:0)
您正在创建new Panel
,但从不使用它。您可能不想创建新的颜色,因为您说颜色已设置为CornFlowerBlue
。
因此,如果您已经设置了Panel
,请确保将其添加到btnItems
列表中。这样您就可以通过btnItems[0]
。