2011-06-29 110 views
1

這裏是我的移動攝像機代碼:XNA 4.0 CameraPosition問題

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下面有紅色squigglies並給出錯誤:

字段初始化器不能引用非靜態字段,方法或屬性。

回答

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; 

    }