XNA 4.0 CameraPosition Question
Here is my code for moving the camera:
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;
}
The thing is camTurn and CamForwardBack have red squigglies under them and give the error:
A field intializer cannot reference the non-static field, method, or property.
Try to initialise the variables in the constructor first.
Like:
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;
}
精彩评论