问题出在我的班级中,呈现需要一些值的级别来渲染侧滚动,这个值即可进入玩家类......当我做类似的事情时:
//Inside the render class
//This is global variable
Player player = new Player;
//This is inside some method that renders.
public void rendering method()
{
int i = player.myInt;
}
Int i始终为零..每次按下右键时,我都会在播放器类中更新myInt。
我也试过制作一个返回我需要的值的方法:
//Inside the player class
public int testing()
{
return myValue;
}
这也无法正常工作..
在我的Player类中,我在构造函数中有变量,如:
public Player()
{
myValue = 0;
}
然后在播放器类中的一个更新方法中,调用它来寻找我有的按键:
public void Update()
{
if(key left was pressed)
myValue--;
}
答案 0 :(得分:1)
确保使用正确的格式。
例如:
class Player: Form
{
public static int myInt = 0; //This is where the next screen should be.
/// <summary>
/// Constructor.
/// </summary>
public Player()
{
}
/// <summary>
/// Changes MyInt. Increments myInt so the next corod
/// </summary>
private void ButtonPressed()
{
myInt++;
//Note, you need to get the screen size and mode it with this to prevent setting the location off screen.
}
}
class Render
{
private Player player; //Class variable so it persists between calls
/// <summary>
/// Constructor. Sets player.
/// </summary>
public Render()
{
Location = new Point(Player.myInt, Player.myInt); //Set my coordinate
}
/// <summary>
/// Shows the value of this.player.MyInt in a message box.
/// </summary>
public void rendering_method()
{
int i = Player.myInt;
MessageBox.Show("The value is: " + i);
}
}
如果这不能解决您的问题,那么我们需要更多信息。 如果你能把两个受影响的班级放在一起,那将是最好的。
更新为使用构造函数设置播放器位置。 根据原始问题中的更新内容进行更新。什么是对象层次结构?玩家是否会创建渲染类?