在一种形式到另一种形式之间移动-C#
我目前正在制作一个视频游戏,并在楼上和楼下之间的一所房子里对动作进行编码。我将PictureBoxs与IntersectWith事件结合使用,在表单之间进行转换 上楼的过渡代码:在一种形式到另一种形式之间移动-C#,c#,methods,load,transition,C#,Methods,Load,Transition,我目前正在制作一个视频游戏,并在楼上和楼下之间的一所房子里对动作进行编码。我将PictureBoxs与IntersectWith事件结合使用,在表单之间进行转换 上楼的过渡代码: if(picPlayer.Bounds.IntersectsWith(picUpstairsTransition.Bounds)) { MapFrmHouseUpstairs upstairs = new MapFrmHouseUpstairs();
if(picPlayer.Bounds.IntersectsWith(picUpstairsTransition.Bounds))
{
MapFrmHouseUpstairs upstairs = new MapFrmHouseUpstairs();
this.Hide();
upstairs.ShowDialog();
}
返回楼下的转换代码:
if(picPlayer.Bounds.IntersectsWith(picGoDownstairs.Bounds))
{
MapFrmHouse goDownstairs = new MapFrmHouse();
this.Hide();
goDownstairs.ShowDialog();
picPlayer.Location = new Point(497, 103);
}
我的问题是,当球员进入房子,他开始在前面。当他试图从楼上回来时,角色被移回前面而不是楼梯底部。我是否可以在MapFrmHouse中创建一个方法,例如:
public void fromDownstairs{picPlayer.Location = new Point(x,y);}
下楼的时候叫它 首先,你最好使用游戏引擎来设计像Unity这样的游戏,它更简单、更有趣。这对你来说可能是一项学习任务,我不怪你:) 您可以通过多种方式在Windows窗体中的窗体之间发送信息,其中两种方式似乎可以满足您的需要:
MapFrmHouse
类中名为picPlayer
的图片框的修饰符属性从private
设置为public
,然后您可以完全执行您提到的操作:
if(picPlayer.Bounds.IntersectsWith(picGoDownstairs.Bounds))
{
MapFrmHouse goDownstairs = new MapFrmHouse()
{
picPlayer.Location = new Point(x,y);
};
this.Hide();
goDownstairs.ShowDialog();
}
对于第二种方法,您应该在MapFrmHouse
中创建一个重载构造函数,并在其中接受一个点
值,然后将其设置为您的值,如下所示:
public MapFrmHouse(Point p)
{
InitializeComponent();
picPlayer.Location = p;
}
if(picPlayer.Bounds.IntersectsWith(picGoDownstairs.Bounds))
{
MapFrmHouse goDownstairs = new MapFrmHouse(new Point(x,y));
this.Hide();
goDownstairs.ShowDialog();
}
然后在代码中使用它,如下所示:
public MapFrmHouse(Point p)
{
InitializeComponent();
picPlayer.Location = p;
}
if(picPlayer.Bounds.IntersectsWith(picGoDownstairs.Bounds))
{
MapFrmHouse goDownstairs = new MapFrmHouse(new Point(x,y));
this.Hide();
goDownstairs.ShowDialog();
}
我希望这有帮助:)当然有。如果你真的想使用单独的表单,就把东西放在构造函数中,然后从那里开始。在构造函数中,你的意思是放在MapFrmHouse(MethodInvoker)中,因为这会带来一个错误。第一个解决方案仍然将我的角色放在房子的前面。我已确保将picPlayer设置为公共修改器。还有其他原因吗?也许你在另一个地方重新设置了。暂停执行并逐步检查代码,查看是否再次设置。