以下是移动相机的代码:
float camTurn = 0.0f;
float camForwardBack = 0.0f;
// Set the position of the model in world space, and set the rotation.
Vector3 modelPosition = new Vector3(-200.0f, -175.0f, 10050.0f);
float modelRotation = 0.0f;
// Set the position of the camera in world space, for our view matrix.
Vector3 cameraPosition = new Vector3(camTurn, 0.0f, camForwardBack);
public void Movement()
{
if (Keyboard.GetState().IsKeyDown(Keys.W))
camForwardBack = camForwardBack + 1;
else if (Keyboard.GetState().IsKeyDown(Keys.S))
camForwardBack = camForwardBack - 1;
else if (Keyboard.GetState().IsKeyDown(Keys.A))
camTurn = camTurn - 1;
else if (Keyboard.GetState().IsKeyDown(Keys.D))
camForwardBack = camForwardBack + 1;
}
事情是camTurn和CamForwardBack在它们下面有红色波浪形并给出错误:
字段初始化程序无法引用非静态字段,方法或属性。
答案 0 :(得分:1)
首先尝试初始化构造函数中的变量。
像:
public class MyCam
{
float camTurn = 0.0f;
float camForwardBack = 0.0f;
// Set the position of the model in world space, and set the rotation.
Vector3 modelPosition = new Vector3(-200.0f, -175.0f, 10050.0f);
float modelRotation = 0.0f;
// Set the position of the camera in world space, for our view matrix.
Vector3 cameraPosition = new Vector3(camTurn, 0.0f, camForwardBack);
public MyCam()
{
camTurn = 0;
camForwardBack = 0;
}
public void Movement()
{
if (Keyboard.GetState().IsKeyDown(Keys.W))
camForwardBack = camForwardBack + 1;
else if (Keyboard.GetState().IsKeyDown(Keys.S))
camForwardBack = camForwardBack - 1;
else if (Keyboard.GetState().IsKeyDown(Keys.A))
camTurn = camTurn - 1;
else if (Keyboard.GetState().IsKeyDown(Keys.D))
camForwardBack = camForwardBack + 1;
}